# Sidebar

Sidebar provides the navigation rows, groups, sub-menus and search that go inside a Shell pane.

Sidebar is the set of navigation rows in an app: a list of links, grouped and labelled, with sub-menus and a search field. The column that holds them is a [Shell](/docs/shell) pane. For a list of commands that opens from a button, use a [Dropdown Menu](/docs/dropdown-menu).


## Usage

```tsx
import { Shell, Sidebar } from '@kushagradhawan/kookie-ui';
import { HugeiconsIcon } from '@hugeicons/react';
import { Home01Icon, InboxIcon } from '@hugeicons/core-free-icons';
import Link from 'next/link';

export function MainNav({ pathname }: { pathname: string }) {
  return (
    <Shell.Sidebar aria-label="Main">
      <Shell.Scroll>
        <Sidebar.Menu>
          <Sidebar.MenuItem>
            <Sidebar.MenuButton asChild isActive={pathname === '/'}>
              <Link href="/">
                <HugeiconsIcon icon={Home01Icon} strokeWidth={1.75} />
                Home
              </Link>
            </Sidebar.MenuButton>
          </Sidebar.MenuItem>
          <Sidebar.MenuItem>
            <Sidebar.MenuButton asChild isActive={pathname === '/inbox'} badge="3">
              <Link href="/inbox">
                <HugeiconsIcon icon={InboxIcon} strokeWidth={1.75} />
                Inbox
              </Link>
            </Sidebar.MenuButton>
          </Sidebar.MenuItem>
        </Sidebar.Menu>
      </Shell.Scroll>
    </Shell.Sidebar>
  );
}
```

## Anatomy

```tsx
<Shell.Sidebar>
  <Shell.PaneHeader>
    <Sidebar.Search />
  </Shell.PaneHeader>
  <Shell.Scroll>
    <Sidebar.Group>
      <Sidebar.GroupLabel />
      <Sidebar.GroupContent>
        <Sidebar.Menu>
          <Sidebar.MenuItem>
            <Sidebar.MenuButton />
          </Sidebar.MenuItem>
          <Sidebar.MenuItem>
            <Sidebar.MenuSub>
              <Sidebar.MenuSubTrigger />
              <Sidebar.MenuSubContent />
            </Sidebar.MenuSub>
          </Sidebar.MenuItem>
        </Sidebar.Menu>
      </Sidebar.GroupContent>
    </Sidebar.Group>
    <Sidebar.Separator />
  </Shell.Scroll>
  <Shell.PaneFooter />
</Shell.Sidebar>
```

- **Menu:** A list of rows, a `ul`.
- **MenuItem:** One row's slot, an `li`.
- **MenuButton:** The row people press. Render a link with `asChild`.
- **MenuSub, MenuSubTrigger, MenuSubContent:** A row that opens a nested list.
- **Group, GroupLabel, GroupContent:** A set of rows under a plain-text label.
- **Separator:** A line between groups.
- **Search:** A search field for the rows.

## Best practices

**Make every row a link.** A sidebar row goes somewhere, so render your router's link with `asChild`. People can then open it in a new tab and see its address.

**Mark the current page with `isActive`.** It highlights the row and sets `aria-current="page"`, so a screen reader announces where you are too.

**Put an icon on every row or on none.** A column where some rows have icons and others don't is hard to scan. In a Rail, the icon is what people see, so every row needs one.

**Group by what people look for.** A short group label, such as "Workspace" or "Settings", helps people find a row in a long list. A list of five rows needs no groups.

**Keep commands out of the list.** "New project" or "Log out" is an action. Put it in a Button in the pane's header or footer, or in a [Dropdown Menu](/docs/dropdown-menu).

## Size

The rows take their size from `Shell.Root size`: `'1'`, `'2'` or `'3'`. Without it, they follow the Theme's size, and a Theme size of 4 maps to 3. A row is as tall as a [Button](/docs/button) of the same size, and its badge and shortcut follow. Rows outside a Shell pane are size 2.

## Examples

### Unread count

`badge` shows a [Chip](/docs/chip) at the end of the row. Pass a string for a soft chip, or an object to set its look.

```tsx
<Sidebar.MenuButton badge={{ content: 'New', variant: 'solid', color: 'crimson' }}>
  <HugeiconsIcon icon={SparklesIcon} strokeWidth={1.75} />
  Features
</Sidebar.MenuButton>
```

### Keyboard shortcut

`shortcut` shows a [Kbd](/docs/kbd) at the end of the row. It only shows the keys. Bind the shortcut yourself.

```tsx
<Sidebar.MenuButton shortcut="⌘K">
  <HugeiconsIcon icon={Search01Icon} strokeWidth={1.75} />
  Search
</Sidebar.MenuButton>
```

### Nested pages

`Sidebar.MenuSub` opens and closes its rows in place. Set `defaultOpen` when the current page is inside it. In a Rail, the same sub-menu opens as a dropdown beside the tile.

```tsx
<Sidebar.MenuItem>
  <Sidebar.MenuSub defaultOpen>
    <Sidebar.MenuSubTrigger>Projects</Sidebar.MenuSubTrigger>
    <Sidebar.MenuSubContent>
      <Sidebar.MenuItem>
        <Sidebar.MenuButton asChild isActive>
          <Link href="/projects/atlas">Atlas</Link>
        </Sidebar.MenuButton>
      </Sidebar.MenuItem>
    </Sidebar.MenuSubContent>
  </Sidebar.MenuSub>
</Sidebar.MenuItem>
```

### Filtering the rows

`Sidebar.Search` is controlled: filter the rows with its `value`. In a Rail it becomes a search tile that opens a popover, and its children render there as the results.

```tsx
<Sidebar.Search value={query} onValueChange={setQuery} placeholder="Search pages..." />
```

## Props

### MenuButton

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `isActive` | `boolean` | `false` | Marks the current page and sets `aria-current="page"` |
| `badge` | `string \| BadgeConfig` | | A chip at the end of the row |
| `shortcut` | `ReactNode` | | Keys shown at the end of the row |
| `asChild` | `boolean` | `false` | Renders the child element, such as a link, with the row's look |

`BadgeConfig` is `{ content, variant?, size?, color?, radius? }`, with the [Chip](/docs/chip) values. MenuButton accepts the attributes of the HTML `button` element, and renders `type="button"` unless you pass a `type`.

In a Rail, the row's icon sits in a tile that shows the hover and active states. The icon is an `svg`, a `HugeiconsIcon`, or the first element before the label, so icons from any set work, and so does a link with `asChild`.

### MenuSub

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `defaultOpen` | `boolean` | `false` | Starts with the nested rows showing |

MenuSubTrigger takes `asChild`. With it, you supply the chevron yourself.

### GroupLabel

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `asChild` | `boolean` | `false` | Renders the child element, such as a heading, with the label's look |

### Search

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | | The text in the field. Required |
| `onValueChange` | `(value: string) => void` | | Runs on each change. Required |
| `placeholder` | `string` | `'Search...'` | The placeholder, and the field's name without the trailing dots |
| `children` | `ReactNode` | | In a Rail, the results shown under the field |

Menu accepts the attributes of `ul`, MenuItem those of `li`, Separator the [Separator](/docs/separator) props, and the other parts those of `div`.

## Accessibility

| Key | Action |
| --- | --- |
| Arrow Down or Arrow Up | From a row's button or a sub-menu's trigger, moves to the next or previous visible row in the same `Shell.Scroll`. Your `onKeyDown` runs first, and `preventDefault()` keeps the key for you |
| Enter or Space | Opens a row, or opens and closes a sub-menu |
| Escape | In Search, clears the text |

- **Landmark:** A Rail, Panel or Sidebar pane is a navigation landmark. Give the pane an `aria-label`, such as "Main".
- **Current page:** `isActive` sets `aria-current="page"`, which a screen reader reads with the row's name.
- **Roles:** The rows are a list of links, not a `menu`, so a screen reader announces them as a list.
- **Group label:** A Group with a GroupLabel is a `role="group"` named by its label. A Group without one has no role, so it isn't announced as an empty group.
- **Search:** The field has `role="search"` around it and takes its name from `placeholder`. The clear button is named "Clear search". In a Rail, the popover's arrow keys move between the field and the results.
