Skip to content

Organisms · Sidebar

Sidebar

The navigation down the side of an app. It holds groups of pages, each page with an icon and an optional count or badge. Groups can fold away, there is room for a header and an account line, and its edge can be dragged to make it wider or narrower.

Best for
App navigation down the side: grouped pages with icons and counts.
Not for
A site header with a few links: use Navbar.

This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).

Examples

Dashboard navigation

Code
tsx
const GROUPS: SidebarGroup[] = [  {    title: 'Operations',    icon: <Warehouse />,    items: [      { label: 'Overview', href: '#overview', icon: <LayoutDashboard /> },      { label: 'Orders', href: '#orders', icon: <Package />, current: true, count: 12 },      { label: 'Shipments', href: '#shipments', icon: <Truck /> },      { label: 'Returns', href: '#returns', icon: <Inbox />, count: 3 },    ],  },  {    title: 'Catalogue',    icon: <Boxes />,    defaultOpen: false,    items: [      { label: 'Products', href: '#products', icon: <Box /> },      { label: 'Inventory', href: '#inventory', icon: <Map /> },    ],  },  {    title: 'Account',    icon: <UserCog />,    collapsible: false,    items: [      { label: 'Settings', href: '#settings', icon: <Settings /> },      { label: 'Team', href: '#team', icon: <Users /> },    ],  },]​const HEADER = <p className="font-display font-semibold">Hexline ops</p>​const FOOTER = <p className="text-xs text-[var(--color-text-secondary)]">Signed in as Ana Pop</p>​<div className="max-w-64">  <Sidebar label="Dashboard navigation" groups={GROUPS} header={HEADER} footer={FOOTER} /></div>

A Projects group with an action in its title row and a footer under its items

Code
tsx
const PROJECT_GROUPS: SidebarGroup[] = [  {    title: 'Projects',    icon: <FolderKanban />,    action: (      <IconButton label="New project" variant="ghost" size="sm">        <Plus aria-hidden="true" />      </IconButton>    ),    items: [      { label: 'Spring restock', href: '#spring-restock', icon: <Folder /> },      { label: 'Warehouse move', href: '#warehouse-move', icon: <Folder /> },    ],    footer: (      <Button variant="ghost" size="sm">        <Plus aria-hidden="true" />        New project      </Button>    ),  },]​<div className="max-w-64">  <Sidebar label="Projects navigation" groups={PROJECT_GROUPS} /></div>

Projects that fold open to their tasks, with a New task action on hover and focus, and counts in one column

Code
tsx
const newTask = (project: string) => (  <IconButton label={`New task in ${project}`} variant="ghost" size="sm">    <Plus aria-hidden="true" />  </IconButton>)​const NESTED_GROUPS: SidebarGroup[] = [  {    title: 'Projects',    icon: <FolderKanban />,    items: [      {        label: 'Spring restock',        href: '#spring-restock',        icon: <Folder />,        count: 2,        action: newTask('Spring restock'),        children: [          { label: 'Count bay 4', href: '#count-bay-4', icon: <Circle />, current: true },          { label: 'Order shelf labels', href: '#order-labels', icon: <CircleCheck /> },        ],      },      {        label: 'Warehouse move',        href: '#warehouse-move',        icon: <Folder />,        action: newTask('Warehouse move'),        children: [          { label: 'Book the trucks', href: '#book-trucks', icon: <Circle /> },          { label: 'Label the racks', href: '#label-racks', icon: <Circle /> },        ],      },      { label: 'Returns audit', href: '#returns-audit', icon: <Folder />, count: 14 },    ],  },]​<div className="max-w-64">  <Sidebar label="Project tasks" groups={NESTED_GROUPS} /></div>

Group counts, item bars and a narrow column: labels truncate, counts stay

Code
tsx
const METER_GROUPS: SidebarGroup[] = [  {    title: 'Warehouses',    icon: <Warehouse />,    count: '3/4',    items: [      { label: 'North depot and loading bay', href: '#north', icon: <Box />, count: 412, bar: { value: 412, max: 600, color: 'teal' } },      { label: 'Harbour store', href: '#harbour', icon: <Box />, count: 268, bar: { value: 268, max: 600, color: 'teal' } },      { label: 'East yard', href: '#east', icon: <Box />, count: 74, bar: { value: 74, max: 600, color: 'teal' } },    ],  },]​<div className="max-w-48">  <Sidebar label="Warehouse navigation" groups={METER_GROUPS} /></div>

Resizable: drag the edge, or focus it and use the arrow keys, between 200 and 400px

Code
tsx
const GROUPS: SidebarGroup[] = [  {    title: 'Operations',    icon: <Warehouse />,    items: [      { label: 'Overview', href: '#overview', icon: <LayoutDashboard /> },      { label: 'Orders', href: '#orders', icon: <Package />, current: true, count: 12 },      { label: 'Shipments', href: '#shipments', icon: <Truck /> },      { label: 'Returns', href: '#returns', icon: <Inbox />, count: 3 },    ],  },  {    title: 'Catalogue',    icon: <Boxes />,    defaultOpen: false,    items: [      { label: 'Products', href: '#products', icon: <Box /> },      { label: 'Inventory', href: '#inventory', icon: <Map /> },    ],  },  {    title: 'Account',    icon: <UserCog />,    collapsible: false,    items: [      { label: 'Settings', href: '#settings', icon: <Settings /> },      { label: 'Team', href: '#team', icon: <Users /> },    ],  },]​const newTask = (project: string) => (  <IconButton label={`New task in ${project}`} variant="ghost" size="sm">    <Plus aria-hidden="true" />  </IconButton>)​const NESTED_GROUPS: SidebarGroup[] = [  {    title: 'Projects',    icon: <FolderKanban />,    items: [      {        label: 'Spring restock',        href: '#spring-restock',        icon: <Folder />,        count: 2,        action: newTask('Spring restock'),        children: [          { label: 'Count bay 4', href: '#count-bay-4', icon: <Circle />, current: true },          { label: 'Order shelf labels', href: '#order-labels', icon: <CircleCheck /> },        ],      },      {        label: 'Warehouse move',        href: '#warehouse-move',        icon: <Folder />,        action: newTask('Warehouse move'),        children: [          { label: 'Book the trucks', href: '#book-trucks', icon: <Circle /> },          { label: 'Label the racks', href: '#label-racks', icon: <Circle /> },        ],      },      { label: 'Returns audit', href: '#returns-audit', icon: <Folder />, count: 14 },    ],  },]​const RESIZABLE_GROUPS: SidebarGroup[] = [  {    ...NESTED_GROUPS[0],    action: (      <IconButton label="New project" variant="ghost" size="sm">        <Plus aria-hidden="true" />      </IconButton>    ),  },  GROUPS[2],]​function ResizableExample() {  const [width, setWidth] = useState<number>(SIDEBAR_WIDTH.default)  return (    <div className="flex min-w-0">      <Sidebar label="Resizable navigation" groups={RESIZABLE_GROUPS} header={HEADER} width={width} onWidthChange={setWidth} />      <div className="hidden min-w-0 flex-1 flex-col gap-2 p-6 sm:flex">        <p className="font-display font-semibold">Orders</p>        <p className="font-data text-sm text-[var(--color-text-secondary)]">Sidebar: {width}px</p>      </div>    </div>  )}​const HEADER = <p className="font-display font-semibold">Hexline ops</p>​<ResizableExample />

A warning badge for tasks waiting in review: the label truncates, the badge stays whole

Code
tsx
const BADGE_GROUPS: SidebarGroup[] = [  {    title: 'Projects',    icon: <FolderKanban />,    items: [      { label: 'Spring restock', href: '#spring-restock', icon: <Folder />, current: true, badge: { text: '2', tone: 'warning', label: '2 in review' } },      { label: 'Warehouse move and the new loading bay', href: '#warehouse-move', icon: <Folder />, badge: { text: '1', tone: 'warning', label: '1 in review' } },      { label: 'Returns audit', href: '#returns-audit', icon: <Folder /> },    ],  },]​<div className="max-w-60">  <Sidebar label="Projects with reviews" groups={BADGE_GROUPS} /></div>

In a sheet on a narrow screen: picking a link closes the sheet, through renderLink

Code
tsx
const GROUPS: SidebarGroup[] = [  {    title: 'Operations',    icon: <Warehouse />,    items: [      { label: 'Overview', href: '#overview', icon: <LayoutDashboard /> },      { label: 'Orders', href: '#orders', icon: <Package />, current: true, count: 12 },      { label: 'Shipments', href: '#shipments', icon: <Truck /> },      { label: 'Returns', href: '#returns', icon: <Inbox />, count: 3 },    ],  },  {    title: 'Catalogue',    icon: <Boxes />,    defaultOpen: false,    items: [      { label: 'Products', href: '#products', icon: <Box /> },      { label: 'Inventory', href: '#inventory', icon: <Map /> },    ],  },  {    title: 'Account',    icon: <UserCog />,    collapsible: false,    items: [      { label: 'Settings', href: '#settings', icon: <Settings /> },      { label: 'Team', href: '#team', icon: <Users /> },    ],  },]​const HEADER = <p className="font-display font-semibold">Hexline ops</p>​const FOOTER = <p className="text-xs text-[var(--color-text-secondary)]">Signed in as Ana Pop</p>​const closeOnChoose = ({ onClick, ...props }: LinkRenderProps) => (  <SheetClose asChild type={undefined}>    <a {...props} onClick={onClick} />  </SheetClose>)​<Sheet>  <SheetTrigger asChild>    <Button variant="secondary">Open navigation</Button>  </SheetTrigger>  <SheetContent side="left" title="Navigation">    <Sidebar      label="Dashboard navigation, in a sheet"      groups={GROUPS}      header={HEADER}      footer={FOOTER}      renderLink={closeOnChoose}      className="border-e-0"    />  </SheetContent></Sheet>

Anatomy

PartWhat it is
navThe navigation region. Screen readers call it by the label prop. A strong hairline on its inline-end edge (the right, in left-to-right text) keeps it separate from the main content. Inside a Sheet, where nothing sits beside it, pass className="border-e-0".
Group titleA disclosure button (one that shows and hides the list) when the group can fold, plain text when it can't. It's text.secondary, so it still reads on a raised panel. Its list opens and closes by animating its height. Opening also fades and slides each item in, each one a little later than the one before (capped, so a long group never feels slow). Closing fades everything out together, all at once. If someone has asked their system for less motion (prefers-reduced-motion: reduce), both happen instantly.
ItemAn icon, a label and an optional badge and count. The current page gets the selected look the NavRail and Tabs share: the neutral surface.selected, primary text in semibold, and a 2px signal bar on its inline-start edge. Orange is only that bar, never a fill. It also gets aria-current, an ARIA attribute (Accessible Rich Internet Applications, the labels screen readers rely on) that tells screen readers this is the page you are on. Hovering a resting item adds a 12% signal tint.
Item badgeOptional. A solid Badge in a status tone, after the label and before the count, at the end of the row: "2" in warning for two tasks waiting in review. A long label is cut short with an ellipsis; the badge never is. Screen readers hear it as part of the link's name, after a comma: the badge's label when you give one ("Spring restock, 2 in review"), else its text ("Spring restock, 2"). Give a label whenever the text is a bare number.
Nested itemsOptional. Pages under an item, such as a project's tasks, indented one level. A chevron button beside the item's link, named "<label> items" with aria-expanded saying whether they show (as a group's title does), folds them with the same height animation as a group. Folded, they can't be reached by Tab. One level only. When any item in a group has children, every item without them keeps the chevron's width free at the end of its row, so the counts line up in one column. A child never does: it cannot fold.
Item actionOptional. A small icon button at the right of an item's row, such as New task. It sits beside the link, not inside it, and shows when the pointer is over the row or anything in the row has focus. It stays in the tab order while it is hidden, and on a touch screen it always shows. When one item has an action, the items beside it without one keep its width free, so the counts stay in one column; that slot is one sm icon button wide.
Group actionOptional. A small icon button at the right end of the title row, such as New project. It sits next to the fold toggle, not inside it, so pressing it never folds the group. A long title is cut short before it can push the action out.
Group footerOptional. Under the group's items and lined up with them: an Add project button. It folds away with the items.
Resize handleOnly with onWidthChange. An 8px strip just inside the inline-end edge, over 8px of end padding the Sidebar keeps free for it, so it never covers an action or a fold toggle: a vertical separator (role="separator") whose value is the width, between minWidth and maxWidth. Drag it, or focus it and use the arrow keys. Its line turns accent.signal.indicator on hover, on focus and while dragging, and its focus ring is drawn inside it, so a parent that clips never hides it. The same behaviour as the SidePanel's edge, from one shared hook.
HeaderSits above the groups: a product name, a workspace switcher.
FooterPinned below the groups: the signed-in account, a help link.

Props

labelstring
Required. The name screen readers give the navigation region.
groupsSidebarGroup[]
Required. Each group has title, icon, items, collapsible and defaultOpen, and optionally action (a small icon button at the right end of the title row, beside the fold toggle, never inside it) footer (under the items, lined up with them, hidden while the group is folded), count (a short right-aligned readout in the data font, such as "2/2", read as part of the toggle’s name) onOpenChange (called with the new state when the reader folds or unfolds it) and open (controls the fold from outside, paired with onOpenChange, for a page that opens one group and folds the rest). Each item can carry count (a number in the data font), badge ({ text, tone, label? }: a solid Badge in a status tone after the label and before the count, never cut short, heard after a comma as label, or text when there is no label), bar (a 2px MeterBar under the item), children (pages under it, indented one level behind a fold toggle named "<label> items"; one level only), defaultOpen (whether those children start shown, false by default, always true when one of them is current) and action (a small icon button at the right of the row, shown on hover and on focus). Labels truncate in a narrow column instead of pushing the count out.
headerReactNode
Sits above the groups: a product name, a workspace switcher.
footerReactNode
Pinned below the groups when the Sidebar has a height (h-full in a column layout): the signed-in account, a help link.
renderLink(props: LinkRenderProps) => ReactNodeDefault a plain anchor
Pass your router's link component here. Map href to its own prop, and pass className, aria-current and onClick through.
widthnumber
The width in pixels. With onWidthChange, the width the handle starts from: store what onWidthChange reports and pass it back here. Alone, a fixed width. Clamped to minWidth and maxWidth. Leave it out and the Sidebar fills its column, or takes the width a className gives it.
onWidthChange(width: number) => void
Turns on the resize handle, and is called with each new width from a drag or a key. Store the value (in localStorage, say) so the Sidebar opens at the same width next time. Without it there is no handle. SIDEBAR_WIDTH, exported beside the component, holds the bounds, the 256px starting width used when width is left out, and the 16px keyboard step.
minWidthnumberDefault 200
The narrowest the handle can make it.
maxWidthnumberDefault 400
The widest the handle can make it.
resizeLabelstringDefault 'Resize sidebar'
The resize handle's accessible name.
classNamestring
Added to the root classes. Where two Tailwind utility classes clash, the later one wins. className="border-e-0" turns off the inline-end border, where nothing sits beside the Sidebar (in a Sheet, or when your layout draws the line itself). With a resize handle, keep at least 8px of end padding (the Sidebar sets pe-2 itself; more is fine) so the handle never covers an action, and scroll a tall Sidebar from a parent, not with overflow on the Sidebar itself: the handle would scroll away with the items.
idstring
The nav's id, which the resize handle's aria-controls names. One is generated when you give none.
refRef<HTMLElement>
The nav element.

Keyboard

KeysResult
TabMoves through each group title that can fold, then each visible item, its action and its fold toggle ("<label> items", which reads as expanded or collapsed). Folded children are skipped.
EnterFollows an item, or folds and unfolds a group or an item's children.
SpaceFolds and unfolds a group or an item's children.
←→On the resize handle: narrower and wider by 16px, by 64px with Shift.
HomeEndOn the resize handle: the narrowest and the widest.

Do and don't

Do

  • Only show counts for things that need attention: open orders, not total products.
  • Keep the group that holds the current page open.
  • On a narrow screen, put the whole sidebar in a Sheet behind a button.
  • Give a badge a label that says what its number counts: { text: '2', tone: 'warning', label: '2 in review' }.
  • Remember the width people drag it to.

Don't

  • Don't nest groups inside groups, or items more than one level deep.
  • Don't give the action a second job. It does one thing to its item, such as New task.
  • Don't show a count of zero, or a badge of zero. Leave it off.
  • Don't use a badge for a plain total. It marks something waiting on the reader; a total is a count.
  • Don't rely on icons alone. Every item keeps its label.