Skip to main content

Starwind UI v3.0 is now available! Migration guide

Sidebar Primitive

Sidebar is a Starwind Runtime primitive in the presence-disclosure-control contract family. Astro and React share one semantic component API. React coordinates reactive state through controlled and default props plus callbacks. Astro renders initial state and coordinates later changes through DOM events and Runtime methods. Raw HTML uses Runtime attributes, DOM events, and imperative methods.

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
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.0.0 3 releases

v1.0.0

  • Promoted this Primitive's vendoring version to the stable 1.0.0 baseline. Its existing API and Runtime behavior carry forward from the previous release.

v0.1.1

  • Marked generated React Primitive files as client modules so vendored installs preserve client boundaries in React server frameworks.

v0.1.0

  • Introduced provider-owned expanded, collapsed, and mobile state with coordinated controls and panels.