Sidebar Controller
Controls expanded/collapsed state for sidebar markup. The Blade component wires the attributes for you, but the controller also works with hand-written HTML.
Identifier: sidebar
Requirements
- No external dependencies.
- Ships with
_composition.js,_focus_trap.js,_overlay.js,_overlay_stack.js,_presence.js, and_top_layer.js; publishing the controller publishes these helpers too.
Values
| Value | Type | Default | Description |
|---|---|---|---|
open |
boolean |
true |
Current open state. |
persist |
boolean |
true |
Whether state changes write a cookie. |
cookieName |
string |
sidebar_state |
Cookie name used when persistence is enabled. |
The Blade provider reads the same cookie on each request when defaultOpen is omitted, so reloads, Turbo refreshes and full navigations preserve the collapsed/expanded state rendered by the server.
Actions
| Action | Description |
|---|---|
toggle |
Toggle expanded/collapsed state. |
open |
Expand the sidebar. |
close |
Collapse the sidebar. |
shortcut |
Toggle on Cmd/Ctrl+B when bound to keydown@window. |
closeForCache |
Close the mobile drawer before Turbo caches the page. |
preserveStateForRender |
Copy the current desktop state into Turbo's next body before render. |
The shortcut and mobile Escape dismissal are suspended during IME composition.
Events
State changes dispatch sidebar:change with:
{ open: boolean, state: "expanded" | "collapsed" }
Markup Contract
The controller updates:
- root
data-state - child sidebars with
data-slot="sidebar"anddata-sidebar-collapsible - trigger
aria-expanded
The child sidebar's data-sidebar-collapsible value is the mode restored when the sidebar closes.
Use icon to keep an icon rail visible, offcanvas to move the sidebar fully out of view, or
none for a static sidebar.
Mobile viewports use a separate Presence state exposed as data-mobile-state="open|closed" on the sidebar element.
Desktop data-state remains unchanged while the mobile drawer opens or closes. Set data-motion="default|none" on the
sidebar target; Presence owns hidden and inert only for its mobile lifecycle.
When the mobile drawer is open, normal clicks on links inside the drawer wait for actual exit motion before the click is replayed. Modified clicks, non-primary mouse buttons, non-_self target links, downloads, empty hashes, mailto: and tel: links are left alone.
Standalone Usage
<div
data-controller="sidebar"
data-sidebar-open-value="true"
data-action="keydown@window->sidebar#shortcut turbo:before-cache@window->sidebar#closeForCache turbo:before-render@window->sidebar#preserveStateForRender"
data-state="expanded"
style="--sidebar-width: 16rem; --sidebar-width-icon: 3rem"
>
<button
type="button"
data-slot="sidebar-trigger"
data-action="click->sidebar#toggle"
aria-label="Toggle Sidebar"
>
Toggle
</button>
<aside
data-slot="sidebar"
data-state="expanded"
data-collapsible=""
data-sidebar-collapsible="icon"
>
<nav>
<a data-slot="sidebar-menu-button" href="/dashboard">
<svg aria-hidden="true"></svg>
<span>Dashboard</span>
</a>
</nav>
</aside>
</div>
For custom markup, keep data-slot="sidebar" and data-sidebar-collapsible on every sidebar panel
that should be controlled. Keep data-slot="sidebar-trigger" on triggers if you want the controller
to maintain aria-expanded.