Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Sidebars are one of the most complex components to build. They are central to any application and often contain a lot of moving parts.

The Starwind Sidebar is composable, themeable, and customizable.

Info

The Starwind Sidebar is extremely similar to the shadcn/ui sidebar component.

Installation

The command also adds the sidebar color tokens to your CSS file when they are missing.

Usage

Wrap the sidebar and the page content with SidebarProvider. The trigger can live in the page header or another control region.

---
import { Sidebar, SidebarContent, SidebarProvider, SidebarTrigger } from "@/components/starwind/sidebar";
---
<SidebarProvider>
<Sidebar>
<SidebarContent>Navigation</SidebarContent>
</Sidebar>
<main>
<SidebarTrigger />
<slot />
</main>
</SidebarProvider>

Composition

A Sidebar layout starts with SidebarProvider. Add only the groups, menus, actions, and supporting parts that the application needs.

SidebarProvider
├── Sidebar
│ ├── SidebarHeader
│ ├── SidebarContent
│ │ └── SidebarGroup
│ │ ├── SidebarGroupLabel
│ │ ├── SidebarGroupAction
│ │ └── SidebarGroupContent
│ │ └── SidebarMenu
│ │ └── SidebarMenuItem
│ │ ├── SidebarMenuButton
│ │ ├── SidebarMenuAction
│ │ ├── SidebarMenuBadge
│ │ └── SidebarMenuSub
│ │ └── SidebarMenuSubItem
│ │ └── SidebarMenuSubButton
│ ├── SidebarFooter
│ └── SidebarRail
├── SidebarInset
└── SidebarTrigger
Sidebar Structure

Basic Layout

Wrap your application with SidebarProvider and place Sidebar alongside your main content:

---
import {
Sidebar,
SidebarContent,
SidebarProvider,
SidebarTrigger,
} from "@/components/starwind/sidebar";
import AppSidebar from "@/components/app-sidebar.astro";
---
<SidebarProvider>
<AppSidebar />
<main>
<SidebarTrigger />
<slot />
</main>
</SidebarProvider>

Creating a Sidebar Component

src/components/app-sidebar.astro
---
import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarHeader,
} from "@/components/starwind/sidebar";
---
<Sidebar>
<SidebarHeader />
<SidebarContent>
<SidebarGroup />
<SidebarGroup />
</SidebarContent>
<SidebarFooter />
</Sidebar>

With Menu Items

Use SidebarMenu, SidebarMenuItem, and SidebarMenuButton to create navigation menus. SidebarMenuButton supports sm, md, and lg sizes and defaults to md:

With Inset Variant

When using the inset variant, wrap your main content in SidebarInset:

---
import {
Sidebar,
SidebarInset,
SidebarProvider,
} from "@/components/starwind/sidebar";
---
<SidebarProvider>
<Sidebar variant="inset">
{/* Sidebar content */}
</Sidebar>
<SidebarInset>
<main>{/* Main content */}</main>
</SidebarInset>
</SidebarProvider>

Persist The Sidebar State

Set persistOpen on SidebarProvider to save the desktop open state. It is false by default. When it is on, the sidebar saves "true" or "false" each time the desktop state changes. On the next page load, the saved value replaces defaultOpen. The mobile sheet state is not saved.

  • persistenceStorage sets where the state is saved. The default is "localStorage". Use "cookie" when your server must read the state. It has no effect while persistOpen is false.
  • persistenceKey sets the storage key or cookie name. The default is starwind-sidebar-open.
  • persistenceMaxAge sets the cookie lifetime in seconds. The default is 604800, which is 7 days. The cookie uses path=/, so all pages on your site share it.
  • A controlled open prop ignores the saved value. It is not available in Astro.

To make the first paint show the saved state, use persistenceStorage="cookie". Then read the cookie on the server and pass the result to defaultOpen. The page must render on demand. In Astro, set prerender = false and use a server adapter. If you set persistenceKey, read the cookie with that name.

src/pages/dashboard.astro
---
import { Sidebar, SidebarContent, SidebarProvider, SidebarTrigger } from "@/components/starwind/sidebar";
export const prerender = false;
const defaultOpen = Astro.cookies.get("starwind-sidebar-open")?.value !== "false";
---
<SidebarProvider defaultOpen={defaultOpen} persistOpen persistenceStorage="cookie">
<Sidebar>
<SidebarContent>Navigation</SidebarContent>
</Sidebar>
<main>
<SidebarTrigger />
</main>
</SidebarProvider>

A static page cannot read cookies. It renders defaultOpen, and the Runtime applies the saved state when the page script starts, so the sidebar can change after the first paint. The server cannot read localStorage. A page that reads the cookie is different for each visitor, so send Vary: Cookie or Cache-Control: private when a shared cache or CDN stores it.

API Reference

Styled Component API

These props are added or materially changed by the installed styled component. Standard HTML attributes remain available through the inherited interfaces noted below. Expand a prop to see named type definitions and framework-specific imports. Follow the Primitive and Runtime links for lower-level behavior props.

Sidebar

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
persistenceKey string —
Description
Sets the storage key used for persisted state.
Classification
Primitive override
Primitive prop
sidebar.Provider.persistenceKey
persistenceStorage "localStorage" | "cookie" | false —
Description
Selects where persisted state is stored.
Classification
Primitive override
Primitive prop
sidebar.Provider.persistenceStorage

Sidebar

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
collapsible "offcanvas" | "icon" | "none" "offcanvas"
Description
Selects how the component can collapse.
Classification
Primitive override
Primitive prop
sidebar.Sidebar.collapsible

Sidebar Group Label

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
asChild boolean false
Description
Preserves the Sidebar Group Label recipe on its existing wrapper while rendering the supplied child inside it.
Classification
Wrapper prop

Sidebar Menu Button

Inherits a attributes. Omits `type`.

Inherits button attributes.

Contains the following additional props:

Prop Type Default Toggle details
asChild boolean false
Description
Merges the component behavior and props into its child element.
Classification
Wrapper prop
isActive boolean false
Description
Applies the active visual state.
Classification
Wrapper prop
size "sm" | "md" | "lg" "md"
Description
Selects the component's visual size.
Classification
Styled variant
tooltip string —
Description
Provides collapsed-sidebar tooltip text for this menu button.
Classification
Wrapper prop
variant "default" | "outline" "default"
Description
Selects the component's visual variant.
Classification
Styled variant

Sidebar Menu Action

Inherits Button props.

Contains the following additional props:

Prop Type Default Toggle details
showOnHover boolean false
Description
Reveals the action when its containing item is hovered.
Classification
Wrapper prop

Sidebar Menu Skeleton

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
showIcon boolean false
Description
Shows the component's generated icon.
Classification
Wrapper prop
width string —
Description
Sets the component width.
Classification
Wrapper prop

Sidebar Menu Sub Button

Inherits a attributes.

Contains the following additional props:

Prop Type Default Toggle details
isActive boolean false
Description
Applies the active visual state.
Classification
Wrapper prop
size "sm" | "md" "md"
Description
Selects the component's visual size.
Classification
Styled variant

Primitive And Runtime API

Use these references when you need the lower-level behavior APIs behind Sidebar.

Primitive API

Runtime API

Sidebar primitive
createSidebarController from @starwind-ui/runtime/sidebar

Changelog

View version history v2.3.0 7 releases

v2.3.0

  • Sidebar controls, rail, and reading-relative spacing follow the current direction.
  • When starwind add installs Sidebar, it now adds any missing sidebar color tokens to your CSS file. Colors that you set stay as they are.
  • A click during the Sidebar tooltip close animation now goes to the page below the tooltip. Before, the closing tooltip caught the click, so a quick click on a control under it did nothing.
  • Sidebar tooltips now stay on screen when their trigger is at the edge of the screen, for example on a phone. Before, part of a tooltip could go off screen. Now the tooltip moves along its trigger until it is inside the viewport padding. It stays next to the trigger and keeps its side.
  • In Vue, this component now needs Vue 3.5.21 or newer.
  • Internal code cleanup, such as unused code and import order. This cleanup does not change behavior.

v2.2.0

  • In Astro, an attribute that you set on Sidebar and its parts now wins over the component’s own default for the same attribute, such as role or aria-label.
  • In Svelte, a defined open or mobileOpen that you pass when SidebarProvider mounts now stays under your control, as in React and Vue. To change it, use bind: or update your state in the change callback. A form reset keeps it. If you pass undefined at mount, SidebarProvider keeps its own state.
  • In Svelte, a defined value on SidebarInput also stays under your control, and the Styled SidebarMenuButton keeps href.
  • In Vue, SidebarRail keeps an aria-label that you set, and SidebarMenuButton with tooltip keeps data-slot="sidebar-menu-button".
  • In Vue, a Boolean prop that you leave out, such as persistenceStorage on SidebarProvider, no longer becomes false. The default of the underlying component applies.

v2.1.3

  • In Astro, a Sidebar that starts collapsed no longer opens for a moment when the page loads. Before, a SidebarProvider with defaultOpen={false} showed the full sidebar first and then animated it closed, so the page content moved. Now the Sidebar is collapsed from the first paint. This also works with collapsible="offcanvas".
  • In Astro, you can now show the state that persistOpen saved on first paint. Read the cookie on the server in a page that renders on demand, and pass it to defaultOpen.
  • The Astro Sidebar and SidebarProvider no longer put the same attribute on an element two times. For example, the Sidebar had two data-state attributes.
  • Render the Astro Sidebar inside its SidebarProvider slot. If you render it before the provider, for example with Astro.slots.render, it can show the wrong state until the page script runs.
  • If one page has more than one SidebarProvider with different defaultOpen values, an Astro Sidebar can still show open until the page script runs.

v2.1.2

  • Fixed Svelte headings appearing disabled when they also open a collapsible section. Tooltips remain available in the collapsed sidebar.
  • Fixed React Sidebar getting out of sync with saved state and handling nested mobile sheets incorrectly.

v2.1.1

  • The default export in index.ts now has a name, SidebarParts, so framework tools show a clear name.

v2.1.0

  • Renamed the standard SidebarMenuButton size from default to md. Omitting size keeps the same medium styling.

v2.0.0

  • Sidebar now gets its behavior from Starwind Runtime. Runtime handles the provider, collapsed, and mobile states and the controls.
  • See the Sidebar Primitive for the unstyled parts and the behavior API.