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

Sidebar Primitive

Use Sidebar for navigation that stays beside the main content. Its trigger can expand or collapse the panel.

Workspace

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>

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

Prop Type Default Toggle
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:sidebar-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • data-default-open
  • starwind:sidebar-change
  • setOpen
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:sidebar-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • data-default-open
  • starwind:sidebar-change
  • setOpen
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.

  • mobileOpen
  • defaultMobileOpen
  • onMobileOpenChange
Astro

Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.

  • mobileOpen
  • defaultMobileOpen
  • starwind:sidebar-mobile-change
  • setMobileOpen
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-open
  • starwind:sidebar-mobile-change
  • setMobileOpen
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.

  • mobileOpen
  • defaultMobileOpen
  • onMobileOpenChange
Astro

Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.

  • mobileOpen
  • defaultMobileOpen
  • starwind:sidebar-mobile-change
  • setMobileOpen
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-open
  • starwind:sidebar-mobile-change
  • setMobileOpen
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:sidebar-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • data-default-open
  • starwind:sidebar-change
  • setOpen
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.

  • mobileOpen
  • defaultMobileOpen
  • onMobileOpenChange
Astro

Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.

  • mobileOpen
  • defaultMobileOpen
  • starwind:sidebar-mobile-change
  • setMobileOpen
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-open
  • starwind:sidebar-mobile-change
  • setMobileOpen

Events

Event Callback Value Toggle
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

State Type Props Toggle
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:sidebar-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:sidebar-change, and call setOpen for later updates.

  • data-default-open
  • starwind:sidebar-change
  • setOpen
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.

  • mobileOpen
  • defaultMobileOpen
  • onMobileOpenChange
Astro

Use mobileOpen or defaultMobileOpen for initial state, listen for starwind:sidebar-mobile-change, and call setMobileOpen for later updates.

  • mobileOpen
  • defaultMobileOpen
  • starwind:sidebar-mobile-change
  • setMobileOpen
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-open
  • starwind:sidebar-mobile-change
  • setMobileOpen

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-sidebar-provider - Marks the Provider part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Provider part.
data-mobile-open - Reflects the mobile open state on the Provider part.
Metadata
Attribute Value Description
data-default-open - Reflects the default open prop on the Provider part.
data-default-mobile-open - Reflects the default mobile open prop on the Provider part.
data-keyboard-shortcut - Reflects the keyboard shortcut prop on the Provider part.
data-mobile-query - Reflects the mobile query prop on the Provider part.
data-persist-open - Reflects the persist open prop on the Provider part.
data-persistence-key - Reflects the persistence key prop on the Provider part.
data-persistence-storage - Reflects the persistence storage prop on the Provider part.
data-persistence-max-age - Reflects the persistence max age prop on the Provider part.

The visible sidebar panel for Sidebar.

Default element
div
Discovery hook
data-sw-sidebar
Role
-

Props

Prop Type Default Toggle
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
Attribute Value Description
data-sw-sidebar - Marks the Sidebar part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Sidebar part.
data-collapsible - Reflects the collapsible state on the Sidebar part.
Metadata
Attribute Value Description
data-collapsible-mode - Reflects the collapsible mode prop on the Sidebar part.
data-variant - Reflects the variant prop on the Sidebar part.
data-side - Reflects the side prop on the Sidebar part.

Trigger

The control that opens, closes, or targets the Sidebar content.

Default element
button
Discovery hook
data-sw-sidebar-trigger
Role
-

Props

Prop Type Default Toggle
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
Attribute Value Description
data-sw-sidebar-trigger - Marks the Trigger part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Trigger part.

Rail

A compact control for resizing or toggling Sidebar.

Default element
button
Discovery hook
data-sw-sidebar-rail
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-sidebar-rail - Marks the Rail part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Rail part.

A menu-style button rendered inside Sidebar.

Default element
button
Discovery hook
data-sw-sidebar-menu-button
Role
-

Props

Prop Type Default Toggle
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
Attribute Value Description
data-sw-sidebar-menu-button - Marks the Menu Button part so Starwind Runtime can find it.
State
Attribute Value Description
data-sidebar-state - Reflects the sidebar state on the Menu Button part.

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

OptionLifecycle
defaultMobileOpenconstructor-only
defaultOpenconstructor-only
keyboardShortcutconstructor-only
mobileOpensetter-backed
mobileQueryconstructor-only
onMobileOpenChangeconstructor-only
onOpenChangeconstructor-only
opensetter-backed
persistOpenconstructor-only
persistenceKeyconstructor-only
persistenceMaxAgeconstructor-only
persistenceStorageconstructor-only

Runtime Setters

Method Target Description
setOpen state: open Opens or closes Sidebar from Runtime code.
setMobileOpen state: mobileOpen Opens or closes the mobile Sidebar panel from Runtime code.
Component Relationship
Sidebar Direct primitive

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.