# NavRail

> A narrow bar, 64px wide, down the far left of an app. It holds the few sections of the whole app, each an icon over a short label. Picking one usually swaps the Sidebar beside it for that section’s pages.

- **Best for:** A narrow app-wide bar of 4–8 sections that each swap a sidebar.
- **Not for:** Pages inside one section: use Sidebar.
- Organism. Source: `src/components/organisms/nav-rail/NavRail.tsx`
- Rendered docs page: `/components/nav-rail`

## Import

```tsx
import { NavRail } from 'src/components/organisms/nav-rail/NavRail'
```

Paths are from the Division DS repository root. Installed with shadcn they start `src/division-ds/` instead of `src/` (`src/division-ds/components/...`); import them relative to your file. An install has no index barrels: import each component from its own file, never from `components/molecules`.

## Requires

- The root TooltipProvider (AppShell and DemoLayout mount it).

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `label` | `string` |  | Required. The name screen readers give the navigation region. |
| `items` | `NavRailItem[]` |  | Required. Each item has id (the key), label, icon, and either href (a link) or onSelect (a button), never both. Optionally current, count (a corner badge, capped at 99+, read as "<label>, <count>") and dot (a small signal square when there is no number, read as "<label>, new"). |
| `footerItems` | `NavRailItem[]` |  | Pinned to the bottom of the rail when it has a height (h-full in a row layout): settings, help. Same shape as items. |
| `header` | `ReactNode` |  | Sits above the items: a product mark, a workspace avatar. |
| `renderLink` | `(props: LinkRenderProps) => ReactNode` | `a plain anchor` | Pass your router's link component here. Map href to its own prop, and pass className, aria-current and onClick through. Return one element, or the item loses its tooltip. |
| `className` | `string` |  | Added to the root classes. Where two Tailwind utility classes clash, the later one wins. |
| `ref` | `Ref<HTMLElement>` |  | The nav element. |

## Types and helpers

Exported beside the component, from the same file. Import a type with `import type { ... }`.

```ts
export type NavRailItem = {
  /** The React key; unique within the rail. */
  id: string
  /** Drawn under the icon, cut short with an ellipsis when it does not fit; the tooltip shows it whole. */
  label: string
  /** Drawn aria-hidden; the label carries the meaning. */
  icon: ReactNode
  /** A link… */
  href?: string
  /** …or a button (exactly one of the two). With href set, onSelect is ignored. */
  onSelect?: () => void
  current?: boolean
  /** A badge on the icon, capped at "99+"; read as "<label>, <count>" with the real number. Hidden at 0. */
  count?: number
  /** A small signal dot when there is no number, read as "<label>, new". Ignored while a count shows. */
  dot?: boolean
}
```

## Examples

Each is compiled and rendered on the docs page. Imports are omitted; see above.

### Beside a Sidebar: each section in the rail swaps the column next to it

```tsx
type SectionId = 'inbox' | 'work' | 'agents' | 'chat'

const SECTIONS: { id: SectionId; label: string; icon: ReactNode }[] = [
  { id: 'inbox', label: 'Inbox', icon: <Inbox /> },
  { id: 'work', label: 'Work', icon: <Briefcase /> },
  { id: 'agents', label: 'Agents', icon: <Bot /> },
  { id: 'chat', label: 'Chat', icon: <MessageSquare /> },
]

const PAGES: Record<SectionId, SidebarGroup[]> = {
  inbox: [
    {
      title: 'Inbox',
      collapsible: false,
      items: [
        { label: 'Mentions', href: '#mentions', current: true, count: 2 },
        { label: 'Assigned to me', href: '#assigned' },
        { label: 'Archive', href: '#archive' },
      ],
    },
  ],
  work: [
    {
      title: 'Views',
      collapsible: false,
      items: [
        { label: 'My issues', href: '#my-issues', icon: <LayoutList />, current: true },
        { label: 'Board', href: '#board', icon: <LayoutList /> },
      ],
    },
    {
      title: 'Projects',
      items: [
        { label: 'Spring restock', href: '#spring-restock', icon: <Folder /> },
        { label: 'Warehouse move', href: '#warehouse-move', icon: <Folder /> },
      ],
    },
  ],
  agents: [
    {
      title: 'Agents',
      collapsible: false,
      items: [
        { label: 'Runs', href: '#runs', icon: <Sparkles />, current: true },
        { label: 'Skills', href: '#skills', icon: <Sparkles /> },
      ],
    },
  ],
  chat: [
    {
      title: 'Chats',
      collapsible: false,
      items: [
        { label: 'Today', href: '#today', current: true },
        { label: 'Earlier', href: '#earlier' },
      ],
    },
  ],
}

const FOOTER: NavRailItem[] = [
  { id: 'settings', label: 'Settings', icon: <Settings />, href: '#settings' },
  { id: 'help', label: 'Help', icon: <CircleHelp />, href: '#help' },
]

function ShellExample() {
  const [open, setOpen] = useState<SectionId>('work')
  const items: NavRailItem[] = SECTIONS.map(section => ({
    ...section,
    current: section.id === open,
    onSelect: () => setOpen(section.id),
  }))
  const title = SECTIONS.find(section => section.id === open)!.label
  return (
    <div className="flex h-96 w-full">
      <NavRail label="App sections" items={items} footerItems={FOOTER} className="h-full border-r border-[var(--color-border-hairline)]" />
      <div className="min-w-0 flex-1 max-w-64 pl-4 pt-2">
        <Sidebar label={`${title} pages`} groups={PAGES[open]} className="border-e-0" />
      </div>
    </div>
  )
}

<ShellExample />
```

### Badges: a count, a count past 99, a count on the current item and a dot

```tsx
const BADGES: NavRailItem[] = [
  { id: 'inbox', label: 'Inbox', icon: <Inbox />, href: '#inbox', count: 3 },
  { id: 'work', label: 'Work', icon: <Briefcase />, href: '#work', current: true, count: 12 },
  { id: 'agents', label: 'Agents', icon: <Bot />, href: '#agents', count: 128 },
  { id: 'chat', label: 'Chat', icon: <MessageSquare />, href: '#chat', dot: true },
]

<NavRail label="Sections with badges" items={BADGES} />
```

## Keyboard

| Keys | Result |
|---|---|
| `Tab` | Moves through each item, top to bottom, then the footer items. Focus shows the label in a tooltip. |
| `Enter` | Follows a link item, or picks a button item. |
| `Space` | Picks a button item. |

## Do

- Keep it to four to eight sections, each with a label of one short word, eight letters at most.
- Mark the open section current, and keep the Sidebar beside it in step.
- Put settings and help in footerItems, so the main sections stay at the top.

## Don't

- Don't put pages of one section here. They belong in the Sidebar.
- Don't show a count of zero, or a dot and a count together. Leave the one you don't need off.
- Don't rely on icons alone. Every item keeps its label.

## Anatomy

- **nav**: The navigation region, 64px wide. Screen readers call it by the label prop.
- **Header**: Optional. Sits above the items: a product mark, a workspace avatar.
- **Item**: A link when it has href, a button when it has onSelect. An icon over a label in sentence case, 12px in the body font, the same size on every item; a label of up to eight letters (Settings) fits whole; a longer one ends in an ellipsis, and a tooltip on hover and on focus shows it whole. The current section gets the neutral selected surface, semibold text and a 2px signal-coloured bar on its inline-start edge, plus aria-current="page", which tells a screen reader this is where you are. Hover keeps the faint signal wash, so a hovered item and the current one never look alike.
- **Badge**: Optional. A count on the corner of the icon, capped at 99+. A screen reader hears the item as "Work, 3", with the real number even past 99. A count of 0 shows nothing.
- **Dot**: Optional. A small square in the signal colour when something is new but there is no number. Read as "Inbox, new".
- **Footer items**: Optional. A second list pinned to the bottom of the rail when the rail has a height: settings, help.
