Sidebar
Headless icon-rail collapsible sidebar primitive.
Features
Headless sidebar state
Icon-rail collapse
Controlled or uncontrolled
Trigger and rail toggles
Composable menu structure
shadcn-compatible data attributes
Installation
npm install @ariaui/sidebar
Examples
Sidebar is headless and icon-rail collapsible. Compose Sidebar.Root, Panel, Trigger, Rail, and menu parts, then style the rail from data-state and the sidebar width CSS variables.
Adopt compatible sidebar hosts
asChild replaces only the selected wrapper. Root still owns sidebar state and private Panel registration outside the host. DIV shells, the MAIN Inset, BUTTON Rail and UL/LI menu structure retain their native refs/events; custom components must forward every prop/ref. Dimensions, collapse styling and any main-content push are consumer CSS, not generated layout. SVG hosts and display: contents are not compatible substitutes for native boxes/actions.
Classes merge; ordinary child props/styles take precedence. Child preventDefault() cancels composed owner handling. Rail keeps default type="button"/tabIndex={-1} unless explicitly overridden, and native child submit/reset types preserve browser form behavior. A disabled Rail blocks activation before child capture even if child flags contradict it. Slot alone composes the immediate child ref; replacement/unmount retires physical guard ownership.
The attached Panel's actual ID drives Trigger linkage. React ID props visible to Panel publish in layout; external DOM edits are observed. Undefined child IDs fall back to the part/Root ID, and explicit Trigger aria-controls remains caller-owned. Existing Panel/Trigger and menu-button APIs retain their current rendering choices and disabled guards; structural adoption adds no tree/roving navigation, mobile sheet or new public provider API.
Default
Default sidebar that collapses from full labels to compact icon buttons.
No preview output yet.
Framer Motion
Collapse the sidebar with Framer Motion — panel width and label fades stay in sync with open from Sidebar.useSidebar(), while menu buttons keep a fixed icon anchor so icons do not drift during the transition. Toggle the Home item to animate submenu open/close with height, opacity, and staggered item fades.
Sidebar collapse animated with Framer Motion on panel width and label fades, synced to Sidebar.useSidebar().
No preview output yet.
Anatomy
import * as Sidebar from "@ariaui/sidebar";
export default function Example() {
return (
<Sidebar.Root>
<Sidebar.Panel>
<Sidebar.Header />
<Sidebar.Content>
<Sidebar.Group>
<Sidebar.GroupLabel />
<Sidebar.GroupContent>
<Sidebar.Menu>
<Sidebar.MenuItem>
<Sidebar.MenuButton />
</Sidebar.MenuItem>
</Sidebar.Menu>
</Sidebar.GroupContent>
</Sidebar.Group>
</Sidebar.Content>
</Sidebar.Panel>
<Sidebar.Rail />
<Sidebar.Inset />
</Sidebar.Root>
);
}
API Reference
Root
State container with a plain DIV default host and HTMLDivElement ref. State and private Panel registration providers remain outside the adopted host; dimensions and collapse styling remain consumer-owned.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
open | boolean | — |
defaultOpen | boolean | true |
onOpenChange | (open: boolean) => void | — |
side | "left" | "right" | "left" |
collapsible | "icon" | "none" | "icon" |
keyboardShortcut | string | null | "b" |
panelId | string | Generated ID |
| Attribute | Values |
|---|---|
| data-state | "expanded" | "collapsed" |
| data-side | "left" | "right" |
| data-collapsible | "icon" | "none" |
Parts
Panel
Sidebar panel host. Renders an aside by default and can slot props onto a custom host with asChild.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| id | Actual child/part ID, otherwise Root fallback ID |
| data-sidebar | "sidebar" |
| data-state | "expanded" | "collapsed" |
Trigger
Button that toggles the sidebar expanded state.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| aria-controls | ID of the Panel element |
| aria-expanded | "true" when expanded |
| data-sidebar | "trigger" |
| data-state | "expanded" | "collapsed" |
Rail
Secondary native button toggle. Edge dimensions and placement are consumer styling, not generated layout.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| aria-label | "Toggle Sidebar" by default |
| tabIndex | -1 by default |
| data-sidebar | "rail" |
Inset
Headless MAIN wrapper with HTMLElement refs/events. Native semantics and valid UL/LI ancestry remain the consumer's responsibility; Inset does not automatically push content. No dimensions, mobile sheet or navigation controller is generated.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| data-sidebar | "inset" |
| data-slot | "sidebar-inset" |
Header
Headless DIV wrapper with HTMLDivElement refs/events. Native semantics and valid UL/LI ancestry remain the consumer's responsibility; Inset does not automatically push content. No dimensions, mobile sheet or navigation controller is generated.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| data-sidebar | "header" |
| data-slot | "sidebar-header" |
Content
Headless DIV wrapper with HTMLDivElement refs/events. Native semantics and valid UL/LI ancestry remain the consumer's responsibility; Inset does not automatically push content. No dimensions, mobile sheet or navigation controller is generated.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| data-sidebar | "content" |
| data-slot | "sidebar-content" |
Group
Headless DIV wrapper with HTMLDivElement refs/events. Native semantics and valid UL/LI ancestry remain the consumer's responsibility; Inset does not automatically push content. No dimensions, mobile sheet or navigation controller is generated.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| data-sidebar | "group" |
| data-slot | "sidebar-group" |
GroupContent
Headless DIV wrapper with HTMLDivElement refs/events. Native semantics and valid UL/LI ancestry remain the consumer's responsibility; Inset does not automatically push content. No dimensions, mobile sheet or navigation controller is generated.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| data-sidebar | "group-content" |
| data-slot | "sidebar-group-content" |
GroupLabel
Existing composition API, not a new structural seam. Its current rendering, refs and behavior remain unchanged.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| data-sidebar | "group-label" |
GroupAction
Existing composition API, not a new structural seam. Its current rendering, refs and behavior remain unchanged.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
| Attribute | Values |
|---|---|
| data-sidebar | "group-action" |
Keyboard
| Shortcut | Action |
|---|---|
| Tab | Move focus through sidebar controls and links in document order. |
| Shift+Tab | Move focus to the previous sidebar control or link in document order. |
| Enter/Space | Activate the focused button or link. |
| Ctrl+B/⌘+B | Toggle the sidebar when the default Root shortcut is enabled. |
Accessibility
Sidebar.Trigger exposes aria-expanded and aria-controls for the panel it toggles. Use clear visible labels or screen-reader-only labels for icon-only triggers and menu buttons.
The sidebar package does not assign landmark labels automatically. Add aria-label or aria-labelledby to Sidebar.Panel when the navigation region needs a name, and keep final menu item semantics appropriate for your app.
The default Ctrl+B / Meta+B shortcut can be disabled with keyboardShortcut={null} when it conflicts with application shortcuts.