Components / Navigation
Collapsible Sidebar
A sidebar that collapses to icons, with a highlight that slides between items.
Installation
Requires React 18+ and Tailwind CSS.
- 1
Install dependencies
$ npm install motion - 2
Add the component
Copy the code from the Code tab above into
components/ui/collapsible-sidebar.tsx. - 3
Use it
example.tsx"use client"; import { CollapsibleSidebar } from "@/components/ui/collapsible-sidebar"; const icon = (d: string) => ( <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"> <path d={d} /> </svg> ); const items = [ { label: "Dashboard", icon: icon("M3 3h7v9H3zM14 3h7v5h-7zM14 12h7v9h-7zM3 16h7v5H3z") }, { label: "Projects", icon: icon("M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z") }, { label: "Messages", icon: icon("M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z") }, { label: "Analytics", icon: icon("M3 3v18h18M7 15l4-4 3 3 5-6") }, { label: "Settings", icon: icon("M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM19 12a7 7 0 0 0-.1-1.2l2-1.6-2-3.4-2.4 1a7 7 0 0 0-2-1.2L14 3h-4l-.5 2.6a7 7 0 0 0-2 1.2l-2.4-1-2 3.4 2 1.6a7 7 0 0 0 0 2.4l-2 1.6 2 3.4 2.4-1a7 7 0 0 0 2 1.2L10 21h4l.5-2.6a7 7 0 0 0 2-1.2l2.4 1 2-3.4-2-1.6c.1-.4.1-.8.1-1.2z") }, ]; export default function CollapsibleSidebarDemo() { return ( <div className="h-80"> <CollapsibleSidebar items={items} header={(collapsed) => ( <span className="flex items-center gap-2 font-semibold text-zinc-900 dark:text-white"> <span className="grid size-8 shrink-0 place-items-center rounded-lg bg-gradient-to-br from-orange-400 to-rose-500 text-sm text-white"> A </span> {!collapsed && <span className="whitespace-nowrap">Acme Inc.</span>} </span> )} /> </div> ); }
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| items | { label; icon; href?; onClick? }[] | — | Navigation items. |
| header | (collapsed: boolean) => ReactNode | — | Brand area at the top. |
| defaultCollapsed | boolean | false | Start collapsed. |
| defaultActive | number | 0 | Index of the item selected at first. |