# Sidebar

> The app sidebar, ready to use: workspace switcher, search, counts, groups and account. Drag its edge to resize it or fold it into a rail where no icon moves.

Most sidebars collapse by squeezing: labels get cut off mid-word, counts vanish, icons shuffle sideways and the collapse is a button you press, never something your hand can do. This one folds on a spring you can hold. The icons stay exactly where they are while the edge passes over the labels, section names shrink into short rules and every count slides onto its icon's corner and turns into a notification pill. Drag the edge to resize it; keep dragging and the fold follows your hand, then let go and the throw decides whether it lands open or as a rail. In the rail, pointing at an icon grows its grey fill out into the label (and, for a group, its pages), so there are no tooltips to wait for. The current page's fill glides to the new row, groups open with branches drawn from their icon, and the workspace and account menus come with it. Below 768px of its own width it turns into a drawer you swipe in from the edge and drag shut.

## Install

```bash
npx shadcn@latest add @hairline/sidebar
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `avatar`, `number-roll`, `dropdown-menu`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/sidebar
- Registry item (source): https://hairlineui.com/r/sidebar.json

## Usage

```tsx
import { House, Inbox, Settings } from "lucide-react";
import { Sidebar, SidebarTrigger } from "@/components/sidebar";

<Sidebar
  className="h-dvh"
  sections={[{ id: "main", items: [
    { id: "home", label: "Home", icon: House, href: "/" },
    { id: "inbox", label: "Inbox", icon: Inbox, href: "/inbox", badge: 12 },
  ] }]}
  footer={[{ id: "settings", label: "Settings", icon: Settings, href: "/settings" }]}
>
  <header><SidebarTrigger /></header>
  <main>{children}</main>
</Sidebar>
```

## In a Next.js app layout

The layout owns the frame: the current page comes from the pathname, rows are Next.js Links, and the width and fold are kept in a cookie so the server renders them right on the next visit.

```tsx
// app/(app)/layout.tsx
import { cookies } from "next/headers";
import { AppSidebar } from "./app-sidebar";

export default async function AppLayout({ children }: { children: React.ReactNode }) {
  const saved = (await cookies()).get("sidebar")?.value?.split(":");
  return (
    <AppSidebar collapsed={saved?.[0] === "1"} width={Number(saved?.[1]) || 256}>
      {children}
    </AppSidebar>
  );
}

// app/(app)/app-sidebar.tsx
"use client";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { useState } from "react";
import { FolderKanban, House, Inbox, Settings } from "lucide-react";
import { Sidebar, SidebarTrigger } from "@/components/sidebar";

export function AppSidebar({ collapsed: c0, width: w0, children }: { collapsed: boolean; width: number; children: React.ReactNode }) {
  const pathname = usePathname();
  const router = useRouter();
  const [collapsed, setCollapsed] = useState(c0);
  const [width, setWidth] = useState(w0);
  const save = (c: boolean, w: number) => (document.cookie = `sidebar=${c ? 1 : 0}:${w}; path=/; max-age=31536000`);

  return (
    <Sidebar
      className="h-dvh"
      linkAs={Link}
      value={pathname}
      collapsed={collapsed}
      onCollapsedChange={(c) => { setCollapsed(c); save(c, width); }}
      width={width}
      onWidthChange={(w) => { setWidth(w); save(collapsed, w); }}
      sections={[
        { id: "main", items: [
          { id: "/", label: "Home", icon: House, href: "/" },
          { id: "/inbox", label: "Inbox", icon: Inbox, href: "/inbox", badge: 12 },
          { id: "projects", label: "Projects", icon: FolderKanban, items: [
            { id: "/projects/relaunch", label: "Website relaunch", href: "/projects/relaunch" },
          ] },
        ] },
      ]}
      footer={[{ id: "/settings", label: "Settings", icon: Settings, href: "/settings" }]}
      workspaces={[{ id: "northwind", name: "Northwind", detail: "Pro · 24 members" }]}
      user={{ name: "Maya Okafor", detail: "maya@northwind.co" }}
      userMenu={[{ id: "out", label: "Sign out", destructive: true, onSelect: () => router.push("/logout") }]}
      onSearch={() => window.dispatchEvent(new CustomEvent("open-command-palette"))}
    >
      <header className="flex h-14 items-center border-b px-3"><SidebarTrigger /></header>
      <main className="min-h-0 flex-1 overflow-y-auto">{children}</main>
    </Sidebar>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `sections` | `SidebarSection[]` | – | { id, label?, items } groups of rows. An item is { id, label, icon?, color?, href?, badge?, dot?, items? }; items makes it a group of pages. |
| `footer` | `SidebarItem[]` | – | Rows above the account, such as Support and Settings. |
| `value / defaultValue / onValueChange` | `string` | – | The current page's item id. onValueChange also gets the item. |
| `collapsed / defaultCollapsed / onCollapsedChange` | `boolean` | – | Folded into the rail. |
| `width / defaultWidth / onWidthChange` | `number` | `256` | The open width in px, set by dragging the edge. |
| `minWidth / maxWidth` | `number` | `220 / 360` | The open width's range. Dragged below minWidth, it folds. |
| `resizable` | `boolean` | `true` | Drag the edge to resize and fold. |
| `workspaces / workspace / onWorkspaceChange` | `SidebarWorkspace[] / string` | – | { id, name, detail?, logo?, color? } for the switcher at the top. Tiles take --chart-1…5 in turn. |
| `workspaceActions` | `MenuEntry[]` | – | Entries under the workspaces, e.g. "Create workspace". |
| `user / userMenu` | `{ name, detail?, avatar? } / MenuEntry[]` | – | The account row at the foot, and its menu. |
| `onSearch` | `() => void` | – | Shows the search row. Open your command palette here. |
| `searchLabel / searchShortcut` | `string` | `"Search" / "⌘K"` | The search row's text and its shortcut hint (display only). |
| `notice` | `ReactNode` | – | A card above the footer, such as a trial or an announcement. Hidden in the rail. |
| `linkAs` | `ElementType` | `"a"` | Rows with href render as this, e.g. Next.js Link. |
| `drawerBelow` | `number` | `768` | Below this width of its own, the sidebar becomes a drawer. |
| `shortcut` | `boolean` | `true` | ⌘B / Ctrl+B toggles it, except while typing. |
| `children` | `ReactNode` | – | The page beside the sidebar. Put SidebarTrigger in its header; useSidebar() reads the state. |

## States

- **fold**: Collapsing runs the panel's width to 60px on one critically damped spring (about 300ms, no overshoot). Icons, the workspace tile and the avatar never move. Labels fade over the last part of the travel, section names cross-fade into 16px rules, the search row closes into a square, and each count slides onto its icon's top-right corner, shrinking to 74% and turning into a primary pill with a 2px ring of the sidebar colour. Opening plays it backwards.
- **drag**: The 9px edge shows a 2px grey line on hover. Dragging sets the width between minWidth and maxWidth and rubber-bands beyond them. Below minWidth the fold follows the pointer. On release the projected throw decides: past the halfway point between the rail and minWidth it lands open at minWidth or wider, otherwise in the rail, carrying the release speed.
- **peek**: In the rail, pointing at a row shows its grey fill, and the fill grows to the right into the label and count (340ms). Moving to another row glides the fill there (300ms) while the label morphs. A group grows its pages underneath. Leaving folds the label back into the rail, then the fill fades (160ms after 220ms).
- **choose**: A grey fill glides from the old row to the new one (340ms) and the row keeps it. Groups open with their pages sliding down (420ms) and branches drawn from the group's icon. A page in a closed group lights up its group instead.
- **workspace**: The workspace row opens its menu with the workspaces as choices. Choosing one morphs the name letter by letter and brings the new tile in through a 4px blur. In the rail, the tile turns into the expand glyph on hover, and a click opens the panel.
- **drawer**: Below drawerBelow (768px) of its own width, the panel is a drawer over a scrim. It slides on a spring, follows a finger dragged from the left edge or across the panel, and the throw decides open or shut. Choosing a page, the scrim or Escape closes it, and focus returns to the trigger.

## What it handles

A sidebar is the one component on every page of an app, and most collapse by squeezing: labels cut mid-word, counts disappear and the icons shift. This one keeps the icons still and carries everything else across.

- **A fold where nothing jumps**: Icons never move. Labels fade as the edge passes them, section names become short rules and counts land on their icons.
- **Resize and fold by hand**: Drag the edge to set the width. Keep going to fold it; a flick throws it shut or open, and a double-click toggles it.
- **Labels that grow out of the rail**: In the rail the hover fill grows into the label and count, and a group opens into its pages right there.
- **Everything an app sidebar has**: Workspace switcher, search with its shortcut, counts, unread dots, groups, coloured tiles, a notice card and the account menu.
- **A drawer on phones**: Below 768px of its own width it becomes a drawer: swipe in from the left edge, drag it shut, and choosing a page closes it.
- **Keyboard and links**: ⌘B or Ctrl+B toggles it (but never inside a text field), arrows move through rows, and rows render as your router's Link.

## Replaces

a sidebar layout, collapsible icon rail, resizable panel, workspace switcher, account menu, mobile navigation drawer, tooltips for the rail

## Where it fits

- **SaaS dashboards**: The main navigation beside every page of the app.
- **Admin panels**: Sections, counts of what needs attention and a workspace switcher.
- **Project and issue trackers**: Inbox and assigned counts, projects as a group, teams as coloured tiles.
- **AI chat apps**: A rail that stays out of the way of the conversation and opens when needed.

## Notes

- A nav landmark of links (or buttons) with aria-current="page" on the current one. Groups are disclosure buttons with aria-expanded. ↑ ↓ Home End move through the rows. Labels stay in the accessibility tree in the rail, so every row keeps its name.
- The edge is a separator you can focus: ← → resize by 16px, Enter folds or opens. ⌘B / Ctrl+B does the same from anywhere, except in inputs and editors, where it means bold.
- In the rail a keyboard press on a group opens the panel with that group open, since the peek is for pointers. The peek is hidden from screen readers; the rows underneath carry the same names and counts.
- The fold writes one width and one custom property per frame; inside the panel only opacity and transforms change. The page beside it reflows with the width, as with any resizable sidebar.
- As a drawer it is a modal dialog: the page is inert, focus moves into the panel and back to the trigger, Escape and the scrim close it. Vertical swipes scroll the list; only a sideways drag moves the drawer.
- Drawer or panel is decided by the sidebar's own width, not the screen's, so it works inside any frame. Reduced motion folds, opens and moves in place with no glide.

License: MIT (https://hairlineui.com/license#components)
