# Sidebar

The main navigation of an app: brand, sections with nested items and badges, a pinned footer, collapsible to icons.

- Family: navigation
- Import: `import { Sidebar, SidebarHeader, SidebarItem, SidebarSubmenu, SidebarSection, SidebarFooter } from "@senvenli/react/navigation";`
- Page: https://design.senvenli.com/docs/components/sidebar
- JSON: https://design.senvenli.com/docs/components/sidebar.json

## Example: An app's main navigation

Brand, items with a group and a badge, and the account. Under 1024px it becomes a panel that the menu button opens.

```tsx
import { Button } from "@senvenli/react/action";
import { Avatar, Icon, Text } from "@senvenli/react/display";
import { Sidebar } from "@senvenli/react/navigation";
import { useState } from "react";

export default function Example() {
  const [isMenuOpen, setIsMenuOpen] = useState(false);
  return (
    // The Sidebar is as tall as the viewport unless its parent has a height: this frame gives it one.
    <div className="flex h-112 overflow-hidden rounded-ds-large border border-ds-border">
      <Sidebar
        logo={<Icon name="hexagon" />}
        brand="Acme"
        brandHref="#"
        currentKey="invoices"
        isMobileOpen={isMenuOpen}
        onMobileOpenChange={setIsMenuOpen}
        items={[
          { id: "home", label: "Home", icon: "house", href: "#" },
          { id: "orders", label: "Orders", icon: "shopping-cart", href: "#", badge: { value: 4 } },
          { id: "customers", label: "Customers", icon: "users", href: "#" },
          {
            id: "billing",
            label: "Billing",
            icon: "credit-card",
            children: [
              { id: "invoices", label: "Invoices", href: "#" },
              { id: "payouts", label: "Payouts", href: "#" },
            ],
          },
        ]}
        avatar={<Avatar name="Sofia Marin" src="/avatars/05.webp" size="small" />}
        footer={<Text>Sofia Marin</Text>}
      />
      <div className="flex min-w-0 flex-1 flex-col items-start gap-ds-150 p-ds-300">
        <div className="lg:hidden">
          <Button variant="subtle" iconOnly iconBefore="menu" aria-label="Open menu" onPress={() => setIsMenuOpen(true)} />
        </div>
        <Text tone="subtle">Invoices</Text>
      </div>
    </div>
  );
}
```

## Examples

### Sections and a pinned account

Labelled sections scroll between the brand and the account, which stay in view.

```tsx
import { Avatar, Icon, Text } from "@senvenli/react/display";
import { Sidebar } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <div className="flex h-112 overflow-hidden rounded-ds-large border border-ds-border">
      <Sidebar
        logo={<Icon name="hexagon" />}
        brand="Acme"
        brandHref="#"
        currentKey="orders"
        isFooterPinned
        items={[{ id: "home", label: "Home", icon: "house", href: "#" }]}
        sections={[
          {
            id: "sales",
            label: "Sales",
            items: [
              { id: "orders", label: "Orders", icon: "shopping-cart", href: "#", badge: { value: 4 } },
              { id: "customers", label: "Customers", icon: "users", href: "#" },
              { id: "invoices", label: "Invoices", icon: "receipt", href: "#", badge: { value: 2, tone: "danger" } },
            ],
          },
          {
            id: "catalog",
            label: "Catalog",
            items: [
              { id: "products", label: "Products", icon: "package", href: "#" },
              { id: "discounts", label: "Discounts", icon: "tag", href: "#" },
            ],
          },
          {
            id: "settings",
            label: "Settings",
            items: [
              { id: "team", label: "Team", icon: "user-cog", href: "#" },
              { id: "integrations", label: "Integrations", icon: "plug", href: "#" },
            ],
          },
        ]}
        avatar={<Avatar name="Sofia Marin" src="/avatars/05.webp" size="small" />}
        footer={<Text>Sofia Marin</Text>}
      />
      <div className="min-w-0 flex-1 p-ds-300">
        <Text tone="subtle">Shown from 1024px up. Under that, the Sidebar is a panel behind the app's menu button.</Text>
      </div>
    </div>
  );
}
```

### Composed from parts

The same navigation built from `SidebarItem`, `SidebarSubmenu` and `SidebarSection`, with the brand and the account as `SidebarHeader` and `SidebarFooter`.

```tsx
import { Avatar, Icon, Text } from "@senvenli/react/display";
import { Sidebar, SidebarFooter, SidebarHeader, SidebarItem, SidebarSection, SidebarSubmenu } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <div className="flex h-112 overflow-hidden rounded-ds-large border border-ds-border">
      <Sidebar brandHref="#" currentKey="returns" isFooterPinned>
        <SidebarHeader logo={<Icon name="hexagon" />}>Acme</SidebarHeader>
        <SidebarItem id="home" label="Home" icon="house" href="#" />
        <SidebarSection id="sales" label="Sales">
          <SidebarItem id="orders" label="Orders" icon="shopping-cart" href="#" badge={{ value: 4 }} />
          <SidebarSubmenu id="after-sales" label="After sales" icon="package">
            <SidebarItem id="returns" label="Returns" href="#" />
            <SidebarItem id="claims" label="Claims" href="#" />
          </SidebarSubmenu>
          <SidebarItem id="customers" label="Customers" icon="users" href="#" />
        </SidebarSection>
        <SidebarSection id="catalog" label="Catalog">
          <SidebarItem id="products" label="Products" icon="tag" href="#" />
        </SidebarSection>
        <SidebarFooter avatar={<Avatar name="Sofia Marin" src="/avatars/05.webp" size="small" />}>
          <Text>Sofia Marin</Text>
        </SidebarFooter>
      </Sidebar>
      <div className="min-w-0 flex-1 p-ds-300">
        <Text tone="subtle">Shown from 1024px up. Under that, the Sidebar is a panel behind the app's menu button.</Text>
      </div>
    </div>
  );
}
```

### Collapsed to icons

56px wide, labels in tooltips; the toggle at the bottom expands it.

```tsx
import { Avatar, Icon, Text } from "@senvenli/react/display";
import { Sidebar } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <div className="flex h-80 overflow-hidden rounded-ds-large border border-ds-border">
      <Sidebar
        logo={<Icon name="hexagon" />}
        brand="Acme"
        brandHref="#"
        currentKey="orders"
        defaultCollapsed
        items={[
          { id: "home", label: "Home", icon: "house", href: "#" },
          { id: "orders", label: "Orders", icon: "shopping-cart", href: "#", badge: { value: 4 } },
          { id: "customers", label: "Customers", icon: "users", href: "#" },
          { id: "reports", label: "Reports", icon: "chart-column", href: "#" },
        ]}
        avatar={<Avatar name="Sofia Marin" src="/avatars/05.webp" size="small" />}
        footer={<Text>Sofia Marin</Text>}
      />
      <div className="min-w-0 flex-1 p-ds-300">
        <Text tone="subtle">Shown from 1024px up. Pass storageKey to remember the choice between visits.</Text>
      </div>
    </div>
  );
}
```

## Guidelines

### When to use it

- Navigating an app with several sections.

### Use something else when

- [navbar](https://design.senvenli.com/docs/components/navbar.md): A public site with few pages.

## Accessibility

Keyboard:

- Tab: Moves through the links.

Built in:

- The current page is marked with aria-current.
- Under 1024px it becomes a panel opened from the header.
- Collapsed, the rail shows the logo and the account's avatar alone; the brand name stays the link's accessible name.

You provide:

- Section names.
- With children: a unique id on every SidebarItem and SidebarSubmenu (currentKey matches it), or isCurrent on the page's item.

## API reference

### Sidebar

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| brand | `React.ReactNode` |  | no | The product name, after `logo` (or the logo and name together). It links to `brandHref`. Or a `SidebarHeader` child. |
| logo | `React.ReactNode` |  | no | The app's mark (an Icon or an image) before `brand`. Collapsed, the rail shows it alone, centred; the name stays the link's accessible name. Without it, collapsed shows the start of `brand`. |
| brandHref | `string` |  | no |  |
| items | `readonly SidebarItemData[]` |  | no | The top-level items (links, or groups with `children`); five to eight is the comfortable range. With `sections`, these are the unlabelled items above the labelled sections (a dashboard) and may be empty. Ids are unique across `items` and `sections` (a repeated id marks two items as the current page). Leave it out to compose the navigation as children instead. |
| children | `React.ReactNode` |  | no | With no `items`: the navigation composed from `SidebarItem`, `SidebarSubmenu` and `SidebarSection` (loose items first, then the sections), plus an optional `SidebarHeader` (instead of `brand`) and `SidebarFooter` (instead of `footer`). |
| sections | `readonly SidebarSectionData[]` |  | no | Labelled sections under `items`, in order. Each is a named group with an overline; collapsed, the overline is visually hidden (the group keeps its name) and a separator stands in for it. Not collapsible. Left out (or empty), the Sidebar renders exactly as before. |
| currentKey | `string` |  | no | The id of the page's item: marked `aria-current="page"`; its group stays open. |
| footer | `React.ReactNode` |  | no | The account at the bottom: name and role after `avatar` (or Avatar, name and role together). Or a `SidebarFooter` child. |
| avatar | `React.ReactNode` |  | no | The account's Avatar before `footer`. Collapsed, the rail shows it alone, centred, and `footer` is hidden. Without it, collapsed shows the start of `footer`. |
| isFooterPinned | `boolean` |  | no | Keeps the account (`footer`) and the collapse toggle in view at the bottom: the brand stays at the top and only the navigation (`items`, then `sections`) scrolls between them, expanded and collapsed. In the phone panel the account stays at the bottom the same way. The DOM order, and so the Tab order, is the same as without it. Off by default: the whole sidebar scrolls as one, as before. |
| isCollapsed | `boolean` |  | no | Icons only (56px), labels in tooltips. Controlled; or `defaultCollapsed` + `onCollapsedChange`. |
| defaultCollapsed | `boolean` |  | no |  |
| onCollapsedChange | `(isCollapsed: boolean) => void` |  | no |  |
| storageKey | `string` |  | no | Remembers the collapsed choice in localStorage under this key. Render `sidebarScript(storageKey, { defaultCollapsed })` in <head> as well, with the same `defaultCollapsed`, so the first frame already has the remembered width (no layout jump). Uncontrolled only: with `isCollapsed` the app owns the state. |
| isMobileOpen | `boolean` |  | no | Under 1024px the sidebar is a menu panel from the start edge; the app's menu button opens it. |
| onMobileOpenChange | `(isOpen: boolean) => void` |  | no |  |
| aria-label | `string` |  | no | Names the navigation landmark; "Main menu" by default. |
| motion | `ComponentMotion` |  | no | `auto` (follow the device), `reduce` or `none`, for this component; the provider's setting otherwise. |

### SidebarHeader

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | no | The product name (or the logo and name together); it links to the Sidebar's `brandHref`. |
| logo | `React.ReactNode` |  | no | The app's mark before the name; collapsed, the rail shows it alone. As the Sidebar's `logo`. |

### SidebarItem

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `string` |  | yes | Unique in the sidebar; `currentKey` matches it. |
| label | `string` |  | yes | A noun for the page ("Orders"); also the tooltip when the sidebar is collapsed. |
| icon | `IconName` |  | no | Lucide name; top-level items carry one (it is all a collapsed sidebar shows). |
| href | `string` |  | no |  |
| badge | `{ value: number; tone?: BadgeCountTone \| undefined; }` |  | no | A count ("4 new"): a Badge after the label, on the icon when collapsed. |
| isCurrent | `boolean` |  | no | Marks this item as the page in view, instead of the Sidebar's `currentKey`. |

### SidebarSubmenu

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `string` |  | yes | Unique in the sidebar; `currentKey` matches it. |
| badge | `{ value: number; tone?: BadgeCountTone \| undefined; }` |  | no | A count ("4 new"): a Badge after the label, on the icon when collapsed. |
| icon | `IconName` |  | no | Lucide name; top-level items carry one (it is all a collapsed sidebar shows). |
| label | `string` |  | yes | A noun for the page ("Orders"); also the tooltip when the sidebar is collapsed. |
| children | `React.ReactNode` |  | yes | The nested `SidebarItem`s, shown indented when it is open. |

### SidebarSection

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `string` |  | no | Also on the DOM as `data-section`. |
| label | `string` |  | yes | The overline over the items, already translated; it names the group, also when collapsed. |
| children | `React.ReactNode` |  | yes | `SidebarItem`s and `SidebarSubmenu`s. |

### SidebarFooter

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | no | The account's name and role (or Avatar, name and role together). |
| avatar | `React.ReactNode` |  | no | The account's Avatar before the rest; collapsed, the rail shows it alone. As the Sidebar's `avatar`. |

### Tokens it reads

`--ds-background-neutral-hovered`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-blanket`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-easing-enter`, `--ds-easing-exit`, `--ds-easing-standard`, `--ds-icon-brand`, `--ds-icon-subtle`, `--ds-radius-medium`, `--ds-shadow-overlay`, `--ds-space-050`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-400`, `--ds-surface-overlay`, `--ds-surface-sunken`, `--ds-text`, `--ds-text-subtle`, `--ds-text-subtlest`

## Related

- [page-header](https://design.senvenli.com/docs/components/page-header.md)
- [navbar](https://design.senvenli.com/docs/components/navbar.md)
