# NavList

Side navigation for an area with many pages: a navigation landmark of titled sections, links and collapsible groups. This site’s sidebar is a NavList. For a handful of flat links, Sidebar is enough.

```tsx
import { NavList, NavListGroup, NavListItem, NavListSection } from '@blocks/design-system'
```

NavList is a client module. A Server Component imports the parts by name; client-authored code can also write `NavList.Section`, `NavList.Group`, `NavList.Item` and `NavList.Heading`.

## Sections and groups

`NavListSection` is always open and labels its list with its `title`. `NavListGroup` is a button that expands and collapses its items. A group starts open when it contains the `active` item and opens again when navigation makes one of its items active; `defaultOpen` overrides the first.

**Example: Station pages** (`examples/nav-list/station-nav.tsx`)

```tsx
// A Server Component: the named NavList parts with native links.
import { NavList, NavListGroup, NavListItem, NavListSection } from '@blocks/design-system'

export default function StationNav() {
  return (
    <NavList aria-label="Station pages">
      <NavListSection title="Mount Hood">
        <NavListItem href="#overview" active>
          Overview
        </NavListItem>
        <NavListItem href="#history">History</NavListItem>
      </NavListSection>
      <NavListSection title="Data">
        <NavListGroup title="Charts">
          <NavListItem href="#chart-depth">Snow depth</NavListItem>
          <NavListItem href="#chart-swe">Water equivalent</NavListItem>
        </NavListGroup>
      </NavListSection>
    </NavList>
  )
}
```

## Router links and the current page

Pass your router’s link as `as` and mark the current page with `active`, which sets `aria-current="page"`.

**Example: With next/link** (`examples/nav-list/router-links.tsx`)

```tsx
'use client'

import { NavList } from '@blocks/design-system'
import NextLink from 'next/link'
import { usePathname } from 'next/navigation'

const pages = [
  { title: 'Tabs', href: '/components/tabs' },
  { title: 'NavList', href: '/components/nav-list' },
]

// Client-authored: the dot API, next/link items, active from the router.
export default function RouterLinks() {
  const pathname = usePathname()
  return (
    <NavList aria-label="Navigation components">
      <NavList.Group title="Navigation" defaultOpen>
        {pages.map((page) => (
          <NavList.Item key={page.href} as={NextLink} href={page.href} active={page.href === pathname}>
            {page.title}
          </NavList.Item>
        ))}
      </NavList.Group>
    </NavList>
  )
}
```

## Custom sections

`NavListHeading` is the label style a section uses. Build a section by hand when its label needs more than a title.

**Example: A hand-built section** (`examples/nav-list/custom-section.tsx`)

```tsx
import { NavList, NavListHeading, NavListItem } from '@blocks/design-system'

// NavListSection renders a heading from `title`. Build a section by hand when
// it needs something more, using NavListHeading for the same label style.
export default function CustomSection() {
  return (
    <NavList aria-label="Saved stations">
      <li>
        <NavListHeading id="saved-heading">Saved · 2</NavListHeading>
        <ul aria-labelledby="saved-heading" style={{ listStyle: 'none', margin: 0, padding: 0 }}>
          <NavListItem href="#saved-alta">Alta</NavListItem>
          <NavListItem href="#saved-taos">Taos</NavListItem>
        </ul>
      </li>
    </NavList>
  )
}
```

## Keyboard

| Key | Action |
| --- | --- |
| <kbd>Tab</kbd> | Moves through group buttons and visible links in order; links in a closed group are skipped. |
| <kbd>Enter</kbd> / <kbd>Space</kbd> | On a group’s button, opens or closes the group. |
| <kbd>Enter</kbd> | On a link, follows it. |

## Props

`NavList` has no props of its own.

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

**`NavListSection` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `title` | `ReactNode` | — | The section heading. The section's list is labelled by it. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

`NavListHeading` has no props of its own.

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

**`NavListItem` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `active` | `boolean` | `false` | Marks the current page: sets `aria-current="page"` and the active styles. |
| `as` | `ElementType` | — | The element or component to render, e.g. `as={NextLink}`. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches), and the native attributes of the element it renders.

**`NavListGroup` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `title` (required) | `ReactNode` | — | The label on the button that expands and collapses the group. |
| `defaultOpen` | `boolean` | — | Whether the group starts open. Defaults to open when one of its items is `active`. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

## Accessibility

- `aria-label` is required: name the landmark after the area (“Documentation”, “Station pages”), especially when a page has other navigation.
- Exactly one item should be `active`; screen readers announce it as the current page.
- Group buttons announce “expanded” or “collapsed”.
