An application sidebar built without a script. Groups are native details elements, so they fold with a click, Enter or Space. Collapsing is a checkbox inside the trigger button and the layout reads it with :has(). The layout answers to the width of its own container: wide gives a sidebar that hides or shrinks to an icon rail, narrow gives an off-canvas panel with a backdrop. It is all server-rendered and works before hydration.
Browser Support
Baseline Widely available3 featuresDetails
Reads the checkbox state to collapse or open the sidebar. Without it the sidebar stays in its default open state and the trigger does nothing.
Switches between the wide sidebar and the off-canvas panel by the width of the layout container. Without it the sidebar stays wide.
Folds and unfolds a group with keyboard and mouse, no script.
Preview
Installation
Usage
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar"
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<SidebarProvider className="h-svh">
<Sidebar>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Platform</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton href="/" isActive>
Home
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
</Sidebar>
<SidebarInset>
<header className="flex h-12 items-center gap-2 border-b px-3">
<SidebarTrigger />
</header>
{children}
</SidebarInset>
</SidebarProvider>
)
}Examples
Icon rail
collapsible="icon" shrinks the sidebar to a narrow rail with icons instead of hiding it. Labels, group titles and nested lists disappear, titles remain as tooltips.
<Sidebar collapsible="icon">…</Sidebar>Narrow container: off-canvas panel
Below 40rem of container width the sidebar becomes a panel that slides in over the content, with a backdrop that closes it. This frame is 340px wide, so it behaves like a phone.
<SidebarProvider id="docs-nav">
…
<SidebarTrigger sidebarId="docs-nav" />
</SidebarProvider>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | "sidebar" | SidebarProvider: id used by the backdrop to close the panel. Must match sidebarId of the trigger. Default sidebar; make it unique when there are several sidebars on a page |
| sidebarId | string | "sidebar" | SidebarTrigger: same value as id of the provider |
| labels | { collapse?: string; open?: string } | - | SidebarTrigger: accessible names of the two checkboxes (collapse and open) |
| side | "left" | "right" | "left" | Sidebar: side of the layout |
| collapsible | "offcanvas" | "icon" | "none" | "offcanvas" | Sidebar: what the button does on wide containers. offcanvas hides it, icon leaves an icon rail, none keeps it open |
| defaultOpen | boolean | true | SidebarGroup: start expanded (a native details element) |
| isActive | boolean | false | SidebarMenuButton: highlight the current page and set aria-current |
| icon | ReactNode | - | SidebarMenuButton: leading icon, visible in the icon rail |
| asChild | boolean | false | SidebarMenuButton: render the only child, for example a framework Link |
| className | string | - | Additional CSS classes on any part. Width is the --sidebar-width variable on the provider (16rem) |
Features
- No client JavaScript: collapse and open are native checkboxes, groups are details elements.
- The layout reads the checkbox with :has() on the provider, so toggling works before hydration.
- Responds to the container width, not the viewport, so it also fits a sidebar inside a card or a framed preview. The breakpoint is 40rem.
- The panel is position: absolute inside the provider: give the provider a height (h-svh or min-h-svh for a full page).
- Hidden sidebar and closed panel use visibility: hidden, so their links leave the Tab order.
- Uses the sidebar color tokens (--sidebar, --sidebar-accent, ...).
- There is no keyboard shortcut and no remembered state: keep the choice in your own code if you need it.