Sidebar Primitive
Use Sidebar for navigation that stays beside the main content. Its trigger can expand or collapse the panel.
Your project content appears here.
Anatomy
Use the Astro primitive adapter to render Sidebar anatomy with the Runtime wiring included.
---import { Sidebar } from "@starwind-ui/astro/sidebar";---
<Sidebar.Provider> <Sidebar.Sidebar> <Sidebar.MenuButton>Dashboard</Sidebar.MenuButton> </Sidebar.Sidebar> <Sidebar.Trigger>Toggle sidebar</Sidebar.Trigger> <Sidebar.Rail /></Sidebar.Provider>Use the React primitive adapter when Sidebar state participates in React rendering.
import { Sidebar } from "@starwind-ui/react/sidebar";
export function Example() { return ( <Sidebar.Provider> <Sidebar.Sidebar> <Sidebar.MenuButton>Dashboard</Sidebar.MenuButton> </Sidebar.Sidebar> <Sidebar.Trigger>Toggle sidebar</Sidebar.Trigger> <Sidebar.Rail /> </Sidebar.Provider> );}Use the Vue components to build Sidebar.
<script setup lang="ts">import Sidebar from "@starwind-ui/vue/sidebar";</script>
<template> <Sidebar.Provider> <Sidebar.Sidebar> <Sidebar.MenuButton>Dashboard</Sidebar.MenuButton> </Sidebar.Sidebar> <Sidebar.Trigger>Toggle sidebar</Sidebar.Trigger> <Sidebar.Rail /> </Sidebar.Provider></template>Use the Svelte components to build Sidebar.
<script lang="ts"> import { Sidebar } from "@starwind-ui/svelte/sidebar";</script>
<Sidebar.Provider> <Sidebar.Sidebar> <Sidebar.MenuButton>Dashboard</Sidebar.MenuButton> </Sidebar.Sidebar> <Sidebar.Trigger>Toggle sidebar</Sidebar.Trigger> <Sidebar.Rail /></Sidebar.Provider>Render the Sidebar data-sw-* contract yourself, then initialize createSidebarController.
<div data-sw-sidebar-provider> <div data-sw-sidebar> <button data-sw-sidebar-menu-button>Dashboard</button> </div> <button data-sw-sidebar-trigger type="button">Toggle sidebar</button> <button data-sw-sidebar-rail type="button" tabindex="-1"></button></div>
<script type="module"> import { createSidebarController } from "@starwind-ui/runtime/sidebar";
const root = document.querySelector("[data-sw-sidebar-provider]"); if (root) { createSidebarController(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
API Reference
Provider
Provides shared Sidebar state to nested parts.
- Default element
- div
- Discovery hook
- data-sw-sidebar-provider
- Role
- -
Props
open boolean -
- Description
- Controls whether Sidebar is open.
- Kind
- control
- Targets
- provider
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for state changes.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
opendefaultOpenstarwind:sidebar-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
data-default-openstarwind:sidebar-changesetOpen
defaultOpen boolean true
- Description
- Sets whether Sidebar starts open.
- Kind
- control
- Targets
- provider
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for state changes.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
opendefaultOpenstarwind:sidebar-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
data-default-openstarwind:sidebar-changesetOpen
mobileOpen boolean -
- Description
- Controls the mobile open state for Sidebar.
- Kind
- control
- Targets
- provider
- Full type
- boolean
- React
Use mobileOpen for controlled state and defaultMobileOpen for default state, and onMobileOpenChange for state changes.
mobileOpendefaultMobileOpenonMobileOpenChange
- Astro
Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
mobileOpendefaultMobileOpenstarwind:sidebar-mobile-changesetMobileOpen
- Runtime / HTML
Use data-default-mobile-open for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
data-default-mobile-openstarwind:sidebar-mobile-changesetMobileOpen
defaultMobileOpen boolean false
- Description
- Controls the default mobile open state for Sidebar.
- Kind
- control
- Targets
- provider
- Full type
- boolean
- React
Use mobileOpen for controlled state and defaultMobileOpen for default state, and onMobileOpenChange for state changes.
mobileOpendefaultMobileOpenonMobileOpenChange
- Astro
Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
mobileOpendefaultMobileOpenstarwind:sidebar-mobile-changesetMobileOpen
- Runtime / HTML
Use data-default-mobile-open for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
data-default-mobile-openstarwind:sidebar-mobile-changesetMobileOpen
keyboardShortcut string "b"
- Description
- Configures the keyboard shortcut option for the Provider part.
- Kind
- option
- Targets
- provider
- Full type
- string
mobileQuery string "(max-width: 767.98px)"
- Description
- Configures the mobile query option for the Provider part.
- Kind
- option
- Targets
- provider
- Full type
- string
persistOpen boolean false
- Description
- Configures the persist open option for the Provider part.
- Kind
- option
- Targets
- provider
- Full type
- boolean
persistenceKey string "starwind-sidebar-open"
- Description
- Configures the persistence key option for the Provider part.
- Kind
- option
- Targets
- provider
- Full type
- string
persistenceStorage SidebarPersistenceStorage "localStorage"
- Description
- Configures the persistence storage option for the Provider part.
- Kind
- option
- Targets
- provider
- Full type
- SidebarPersistenceStorage
persistenceMaxAge number 604800
- Description
- Configures the persistence max age option for the Provider part.
- Kind
- option
- Targets
- provider
- Full type
- number
onOpenChange (open: boolean, details: SidebarOpenChangeDetails) => void -
- Description
- Runs when Sidebar opens or closes.
- Kind
- callback
- Targets
- provider
- Full type
- (open: boolean, details: SidebarOpenChangeDetails) => void
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for state changes.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
opendefaultOpenstarwind:sidebar-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
data-default-openstarwind:sidebar-changesetOpen
onMobileOpenChange (open: boolean, details: SidebarMobileOpenChangeDetails) => void -
- Description
- Runs when on mobile open change changes for Sidebar.
- Kind
- callback
- Targets
- provider
- Full type
- (open: boolean, details: SidebarMobileOpenChangeDetails) => void
- React
Use mobileOpen for controlled state and defaultMobileOpen for default state, and onMobileOpenChange for state changes.
mobileOpendefaultMobileOpenonMobileOpenChange
- Astro
Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
mobileOpendefaultMobileOpenstarwind:sidebar-mobile-changesetMobileOpen
- Runtime / HTML
Use data-default-mobile-open for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
data-default-mobile-openstarwind:sidebar-mobile-changesetMobileOpen
Events
openChange onOpenChange open: boolean
- Description
- Fires when Sidebar opens or closes.
- DOM event
- starwind:sidebar-change
- Details type
- SidebarOpenChangeDetails
- Timing
- -
- Cancelable
- No
mobileOpenChange onMobileOpenChange open: boolean
- Description
- Fires when the mobile panel opens or closes for Sidebar.
- DOM event
- starwind:sidebar-mobile-change
- Details type
- SidebarMobileOpenChangeDetails
- Timing
- -
- Cancelable
- No
State
open boolean open / defaultOpen
- Description
- Tracks whether Sidebar is open.
- Initial attribute
- data-default-open
- Runtime getter
- getOpen
- Runtime setter
- setOpen
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for state changes.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
opendefaultOpenstarwind:sidebar-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.
data-default-openstarwind:sidebar-changesetOpen
mobileOpen boolean mobileOpen / defaultMobileOpen
- Description
- Tracks whether the mobile Sidebar panel is open.
- Initial attribute
- data-default-mobile-open
- Runtime getter
- getMobileOpen
- Runtime setter
- setMobileOpen
- React
Use mobileOpen for controlled state and defaultMobileOpen for default state, and onMobileOpenChange for state changes.
mobileOpendefaultMobileOpenonMobileOpenChange
- Astro
Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
mobileOpendefaultMobileOpenstarwind:sidebar-mobile-changesetMobileOpen
- Runtime / HTML
Use data-default-mobile-open for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.
data-default-mobile-openstarwind:sidebar-mobile-changesetMobileOpen
Data Attributes
Runtime hooks
State
Metadata
Sidebar
The visible sidebar panel for Sidebar.
- Default element
- div
- Discovery hook
- data-sw-sidebar
- Role
- -
Props
side "left" | "right" "left"
- Description
- Sets the preferred side for Sidebar content.
- Kind
- attribute
- Targets
- sidebar
- Full type
- "left" | "right"
variant "sidebar" | "floating" | "inset" "sidebar"
- Description
- Selects the visual variant for the Sidebar part.
- Kind
- attribute
- Targets
- sidebar
- Full type
- "sidebar" | "floating" | "inset"
collapsible "offcanvas" | "icon" "offcanvas"
- Description
- Allows all Sidebar items to be collapsed.
- Kind
- attribute
- Targets
- sidebar
- Full type
- "offcanvas" | "icon"
Data Attributes
Runtime hooks
State
Metadata
Trigger
The control that opens, closes, or targets the Sidebar content.
- Default element
- button
- Discovery hook
- data-sw-sidebar-trigger
- Role
- -
Props
asChild boolean false
- Description
- Merges behavior onto your child element instead of rendering the default Trigger element.
- Kind
- rendering
- Targets
- trigger, menuButton
- Full type
- boolean
Data Attributes
Runtime hooks
State
Rail
A compact control for resizing or toggling Sidebar.
- Default element
- button
- Discovery hook
- data-sw-sidebar-rail
- Role
- -
Data Attributes
Runtime hooks
State
Menu Button
A menu-style button rendered inside Sidebar.
- Default element
- button
- Discovery hook
- data-sw-sidebar-menu-button
- Role
- -
Props
asChild boolean false
- Description
- Merges behavior onto your child element instead of rendering the default Menu Button element.
- Kind
- rendering
- Targets
- trigger, menuButton
- Full type
- boolean
Data Attributes
Runtime hooks
State
Runtime API
- Factory
createSidebarController- Import
@starwind-ui/runtime/sidebar- Root hook
- provider
data-sw-sidebar-provider - Option props
- defaultMobileOpen, defaultOpen, keyboardShortcut, mobileOpen, mobileQuery, onMobileOpenChange, onOpenChange, open, persistOpen, persistenceKey, persistenceMaxAge, persistenceStorage
Option Lifecycles
| Option | Lifecycle |
|---|---|
| defaultMobileOpen | constructor-only |
| defaultOpen | constructor-only |
| keyboardShortcut | constructor-only |
| mobileOpen | setter-backed |
| mobileQuery | constructor-only |
| onMobileOpenChange | constructor-only |
| onOpenChange | constructor-only |
| open | setter-backed |
| persistOpen | constructor-only |
| persistenceKey | constructor-only |
| persistenceMaxAge | constructor-only |
| persistenceStorage | constructor-only |
Runtime Setters
Related Styled Components
Changelog
View version history v1.2.0 7 releases
v1.2.0
- In Vue and Svelte, `useSidebarContext()` now returns `null` outside `Sidebar.Provider`, as in React. Before, it threw an error. If you call `useSidebarContext()`, check for `null` before you read its fields.
- In Vue and Svelte, `Sidebar.Sidebar`, `Sidebar.Trigger`, `Sidebar.Rail`, and `Sidebar.MenuButton` now render outside a Provider in the expanded state, with `aria-expanded="false"` on the trigger and rail.
- In Svelte, the `SidebarContextValue` type no longer has `isMobile` or `setMobileOpen()`. It now has the same members as the React type.
- If you called `setMobileOpen()`, use `bind:mobileOpen` on `Sidebar.Provider` to open or close the mobile sidebar. If you read `isMobile`, use your own media query, such as `matchMedia()` with the query that you pass to `mobileQuery`.
- In Vue, this Primitive now uses the same `asChild` child handling as the other Vue Primitives. Its public API is unchanged. An invalid `asChild` child now gives one shorter error message.
- 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.
v1.1.0
- In Astro, an attribute that you set on a Sidebar part now wins over the part'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, the Primitive `Sidebar.MenuButton` no longer takes `href`. Use its `child` snippet to render a link. The Styled `SidebarMenuButton` keeps `href`.
v1.0.2
- 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.
v1.0.1
- Fixed React Sidebar getting out of sync with saved state and handling nested mobile sheets incorrectly.
- Reduced repeated JavaScript when Astro Sidebar is used with other Starwind components.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
v0.1.0
- First release. SidebarProvider holds the expanded, collapsed, and mobile states, and it keeps the controls and panels in step.