//layout
Sidebar
A collapsible app navigation panel with groups, menus, badges and sub-menus that turns into a sheet on mobile. Copy-paste ready.
// title: App shell with icon collapse, groups, badges and user footer
"use client";
import {
BarChart3Icon,
BoxIcon,
ChevronsUpDownIcon,
FolderIcon,
HomeIcon,
InboxIcon,
PlusIcon,
SettingsIcon,
UsersIcon,
} from "lucide-react";
import { useState } from "react";
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Separator } from "@/components/ui/separator";
import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarGroupAction,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarInset,
SidebarMenu,
SidebarMenuBadge,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarRail,
SidebarTrigger,
} from "@/components/ui/sidebar";
const NAV = [
{ icon: HomeIcon, label: "Overview" },
{ badge: 12, icon: InboxIcon, label: "Inbox" },
{ icon: BarChart3Icon, label: "Analytics" },
{ icon: UsersIcon, label: "Customers" },
];
const PROJECTS = ["Website redesign", "Mobile app", "Design system"];
export default function Particle() {
const [active, setActive] = useState("Overview");
return (
// The transform makes this box the containing block for the sidebar's
// fixed positioning, so the demo stays inside the preview.
<div className="flex h-88 w-full transform-gpu overflow-hidden rounded-xl border">
<SidebarProvider className="h-full min-h-0">
<Sidebar className="h-full" collapsible="icon">
<SidebarHeader>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton size="lg" tooltip="Acme Inc">
<span className="flex size-8 shrink-0 items-center justify-center rounded-lg bg-primary text-primary-foreground">
<BoxIcon aria-hidden="true" className="size-4" />
</span>
<span className="grid flex-1 text-left leading-tight">
<span className="truncate font-medium">Acme Inc</span>
<span className="truncate text-muted-foreground text-xs">
Pro plan
</span>
</span>
<ChevronsUpDownIcon aria-hidden="true" className="ms-auto" />
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Workspace</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{NAV.map((item) => (
<SidebarMenuItem key={item.label}>
<SidebarMenuButton
isActive={active === item.label}
onClick={() => setActive(item.label)}
tooltip={item.label}
>
<item.icon aria-hidden="true" />
<span>{item.label}</span>
</SidebarMenuButton>
{item.badge && (
<SidebarMenuBadge>{item.badge}</SidebarMenuBadge>
)}
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
<SidebarGroup>
<SidebarGroupLabel>Projects</SidebarGroupLabel>
<SidebarGroupAction aria-label="New project">
<PlusIcon aria-hidden="true" />
</SidebarGroupAction>
<SidebarGroupContent>
<SidebarMenu>
{PROJECTS.map((project) => (
<SidebarMenuItem key={project}>
<SidebarMenuButton
isActive={active === project}
onClick={() => setActive(project)}
tooltip={project}
>
<FolderIcon aria-hidden="true" />
<span>{project}</span>
</SidebarMenuButton>
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
<SidebarFooter>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton tooltip="Settings">
<SettingsIcon aria-hidden="true" />
<span>Settings</span>
</SidebarMenuButton>
</SidebarMenuItem>
<SidebarMenuItem>
<SidebarMenuButton size="lg" tooltip="Maya Patel">
<Avatar className="size-8 rounded-lg">
<AvatarFallback className="rounded-lg text-xs">
MP
</AvatarFallback>
</Avatar>
<span className="grid flex-1 text-left leading-tight">
<span className="truncate font-medium">Maya Patel</span>
<span className="truncate text-muted-foreground text-xs">
maya@acme.dev
</span>
</span>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarFooter>
<SidebarRail />
</Sidebar>
<SidebarInset className="min-w-0">
<header className="flex h-12 shrink-0 items-center gap-2 border-b px-3">
<SidebarTrigger />
<Separator className="h-4" orientation="vertical" />
<span className="font-medium text-sm">{active}</span>
</header>
<div className="grid gap-3 p-4 sm:grid-cols-3">
{["Revenue", "Orders", "Visitors"].map((label) => (
<div className="h-20 rounded-lg bg-muted/50 p-3" key={label}>
<p className="text-muted-foreground text-xs">{label}</p>
</div>
))}
<div className="h-40 rounded-lg bg-muted/50 sm:col-span-3" />
</div>
</SidebarInset>
</SidebarProvider>
</div>
);
}
Installation
pnpm dlx shadcn@latest add @cnippet/sidebar
Usage
Wrap your layout in SidebarProvider, then put the Sidebar and your page (SidebarInset) side by side.
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupLabel,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar"export default function AppLayout({ children }: { children: React.ReactNode }) {
return (
<SidebarProvider>
<Sidebar collapsible="icon">
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Workspace</SidebarGroupLabel>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton isActive tooltip="Inbox">
<InboxIcon />
<span>Inbox</span>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
</SidebarContent>
</Sidebar>
<SidebarInset>
<header>
<SidebarTrigger />
</header>
{children}
</SidebarInset>
</SidebarProvider>
)
}Anatomy
<SidebarProvider> {/* open state, keyboard shortcut, mobile detection */}
<Sidebar>
<SidebarHeader />
<SidebarContent> {/* scrolls */}
<SidebarGroup>
<SidebarGroupLabel />
<SidebarGroupAction /> {/* e.g. a "+" next to the label */}
<SidebarGroupContent>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton />
<SidebarMenuAction />
<SidebarMenuBadge />
<SidebarMenuSub>
<SidebarMenuSubItem>
<SidebarMenuSubButton />
</SidebarMenuSubItem>
</SidebarMenuSub>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
<SidebarFooter />
<SidebarRail /> {/* thin edge you can click to toggle */}
</Sidebar>
<SidebarInset> {/* your page */}
<SidebarTrigger />
</SidebarInset>
</SidebarProvider>Good to know
- Cmd+B (Ctrl+B on Windows and Linux) toggles the sidebar.
- Below the
mdbreakpoint the sidebar opens as a Sheet fromSidebarTriggerinstead of sitting beside the page. - The open state is saved in a
sidebar_statecookie for a week. Read it on the server and pass it asdefaultOpenso the first render matches what the user left it at. - With
collapsible="icon", the collapsed sidebar shows only icons. Give eachSidebarMenuButtonatooltipso its label still appears on hover. - The sidebar is
position: fixedto the viewport. The examples here sit in a box withtransform-gpu, which makes that box the containing block so the demo stays inside it. You don't need that in a real app shell. useSidebar()returnsstate,open,setOpen,openMobile,setOpenMobile,isMobileandtoggleSidebarfor custom controls.
API Reference
SidebarProvider
| Prop | Type | Default | Description |
|---|---|---|---|
defaultOpen | boolean | true | Initial open state when uncontrolled |
open | boolean | — | Controlled open state |
onOpenChange | (open: boolean) => void | — | Called when the sidebar opens or closes |
Set --sidebar-width and --sidebar-width-icon in style to change the widths (defaults 16rem and 3rem).
Sidebar
| Prop | Type | Default | Description |
|---|---|---|---|
side | "left" | "right" | "left" | Which edge it sits on |
variant | "sidebar" | "floating" | "inset" | "sidebar" | Flush panel, floating card, or panel with the page inset beside it |
collapsible | "offcanvas" | "icon" | "none" | "offcanvas" | Slide fully away, shrink to icons, or never collapse |
SidebarMenuButton
| Prop | Type | Default | Description |
|---|---|---|---|
isActive | boolean | false | Marks the current page |
tooltip | string | TooltipPopup props | — | Shown on hover while collapsed to icons |
size | "sm" | "default" | "lg" | "default" | Height |
variant | "default" | "outline" | "default" | Plain or outlined |
render | ReactElement | — | Render as another element, such as a link |
SidebarMenuSubButton
Renders an <a> by default. Takes isActive, size ("sm" \| "md") and render.
Examples
Inset with sub-menus
Docs-style navigation: each section is a Collapsible with nested links, and the page sits inset beside the sidebar.
// title: Docs navigation with collapsible sections (inset)
"use client";
import {
BookOpenIcon,
ChevronRightIcon,
CodeIcon,
LayersIcon,
type LucideIcon,
} from "lucide-react";
import {
Collapsible,
CollapsiblePanel,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupLabel,
SidebarHeader,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar";
type Section = { title: string; icon: LucideIcon; pages: string[] };
const SECTIONS: Section[] = [
{
icon: BookOpenIcon,
pages: ["Introduction", "Installation", "Theming"],
title: "Getting started",
},
{
icon: LayersIcon,
pages: ["Button", "Dialog", "Sidebar", "Stepper"],
title: "Components",
},
{ icon: CodeIcon, pages: ["CLI", "Registry", "MCP"], title: "Tooling" },
];
export default function Particle() {
return (
// The transform keeps the fixed sidebar inside the preview.
<div className="flex h-88 w-full transform-gpu overflow-hidden rounded-xl border">
<SidebarProvider className="h-full min-h-0">
<Sidebar className="h-full" variant="inset">
<SidebarHeader className="px-4 pt-4 font-semibold text-sm">
Acme Docs
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Guides</SidebarGroupLabel>
<SidebarMenu>
{SECTIONS.map((section, i) => (
<Collapsible
className="group/collapsible"
defaultOpen={i === 1}
key={section.title}
render={<SidebarMenuItem />}
>
<CollapsibleTrigger render={<SidebarMenuButton />}>
<section.icon aria-hidden="true" />
<span>{section.title}</span>
<ChevronRightIcon
aria-hidden="true"
className="ms-auto transition-transform group-data-open/collapsible:rotate-90"
/>
</CollapsibleTrigger>
<CollapsiblePanel>
<SidebarMenuSub>
{section.pages.map((page) => (
<SidebarMenuSubItem key={page}>
<SidebarMenuSubButton
isActive={page === "Sidebar"}
render={<button type="button" />}
>
{page}
</SidebarMenuSubButton>
</SidebarMenuSubItem>
))}
</SidebarMenuSub>
</CollapsiblePanel>
</Collapsible>
))}
</SidebarMenu>
</SidebarGroup>
</SidebarContent>
</Sidebar>
<SidebarInset className="min-w-0">
<header className="flex h-12 items-center gap-2 px-3">
<SidebarTrigger />
<span className="text-muted-foreground text-sm">
Components / <span className="text-foreground">Sidebar</span>
</span>
</header>
<div className="space-y-3 p-4 pt-0">
<div className="h-6 w-1/3 rounded-md bg-muted/60" />
<div className="h-4 w-full rounded-md bg-muted/40" />
<div className="h-4 w-5/6 rounded-md bg-muted/40" />
<div className="h-32 rounded-lg bg-muted/40" />
</div>
</SidebarInset>
</SidebarProvider>
</div>
);
}
Floating, right side
A details panel on the right with a search field that filters its menu.
// title: Floating right-side details panel with search
"use client";
import {
CalendarIcon,
FileTextIcon,
MessageSquareIcon,
PaperclipIcon,
SearchIcon,
} from "lucide-react";
import { useState } from "react";
import { Label } from "@/components/ui/label";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarInput,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar";
const ITEMS = [
{ icon: MessageSquareIcon, label: "Comments", meta: "8 new" },
{ icon: PaperclipIcon, label: "Attachments", meta: "3 files" },
{ icon: CalendarIcon, label: "Schedule", meta: "Due Fri" },
{ icon: FileTextIcon, label: "Activity log", meta: "Today" },
];
export default function Particle() {
const [query, setQuery] = useState("");
const shown = ITEMS.filter((i) =>
i.label.toLowerCase().includes(query.toLowerCase()),
);
return (
// The transform keeps the fixed sidebar inside the preview.
<div className="flex h-88 w-full transform-gpu overflow-hidden rounded-xl border">
<SidebarProvider className="h-full min-h-0">
<SidebarInset className="min-w-0">
<header className="flex h-12 items-center justify-between gap-2 border-b px-4">
<span className="font-medium text-sm">Issue #4821</span>
<SidebarTrigger />
</header>
<div className="space-y-3 p-4">
<div className="h-6 w-1/2 rounded-md bg-muted/60" />
<div className="h-4 w-full rounded-md bg-muted/40" />
<div className="h-4 w-4/5 rounded-md bg-muted/40" />
</div>
</SidebarInset>
<Sidebar className="h-full" side="right" variant="floating">
<SidebarHeader>
<Label className="sr-only" htmlFor="details-search">
Search details
</Label>
<div className="relative">
<SearchIcon
aria-hidden="true"
className="pointer-events-none absolute start-2 top-1/2 size-4 -translate-y-1/2 opacity-60"
/>
<SidebarInput
className="ps-8"
id="details-search"
onChange={(e) => setQuery(e.target.value)}
placeholder="Search details…"
value={query}
/>
</div>
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Details</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{shown.map((item) => (
<SidebarMenuItem key={item.label}>
<SidebarMenuButton>
<item.icon aria-hidden="true" />
<span>{item.label}</span>
<span className="ms-auto text-muted-foreground text-xs">
{item.meta}
</span>
</SidebarMenuButton>
</SidebarMenuItem>
))}
{shown.length === 0 && (
<p className="px-2 py-1.5 text-muted-foreground text-xs">
Nothing matches “{query}”.
</p>
)}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
</Sidebar>
</SidebarProvider>
</div>
);
}