Skip to main content

Starwind UI v3.0 beta is now available! Read the migration guide

Sidebar Primitive

Sidebar is a Starwind Runtime primitive in the presence-disclosure-control contract family.

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>

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
defaultOpen boolean true
Description
Sets whether Sidebar starts open.
Kind
control
Targets
provider
Full type
boolean
mobileOpen boolean -
Description
Controls the mobile open state for Sidebar.
Kind
control
Targets
provider
Full type
boolean
defaultMobileOpen boolean false
Description
Controls the default mobile open state for Sidebar.
Kind
control
Targets
provider
Full type
boolean
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
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

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
State control support
React supports controlled and default state with open and defaultOpen props. Runtime/HTML reads initial state from data-default-open, emits starwind:sidebar-change and starwind:sidebar-mobile-change, and updates with setOpen. Astro adapters render initial/default state, but do not expose reactive controlled-state props for this state.
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
State control support
React supports controlled and default state with mobileOpen and defaultMobileOpen props. Runtime/HTML reads initial state from data-default-mobile-open and updates with setMobileOpen. Astro adapters render initial/default state, but do not expose reactive controlled-state props for this state.

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