# Navigation

Moves people between the main parts of a product.

```tsx
import { Sidebar, NavItem, NavGroup, TopBar } from '@halo-ds/react';
```

## When to use it
Navigation is the frame around every page. Nav item is one link; Sidebar stacks them down the left, Expanded or Collapsed; Top bar lines them up across the top, with search and the account. On phones, the Top bar menu button opens the Sidebar links in a Drawer.

## Do
- Keep it to about 5 to 8 main links.
- Use short labels with clear icons.
- Mark the current page with Selected.
- Put settings, help and the account at the bottom or top right.

## Don't
- Mix Sidebar and Top bar links for the same level.
- Use icons with no labels on wide screens.
- Nest groups more than one level deep.
- Change the order of links from page to page.

## Navigation, Tabs or Breadcrumbs?
All three help people move around. Each works at a different level.
- **Navigation**: The main parts of the product, on every page. Each link goes to a different page. Shows where people are with a Selected item.
- **Tabs or Breadcrumbs**: Tabs: views inside one page. Breadcrumbs: the path back up from a deep page. Use them together with Navigation, not instead of it.

## Props
### Sidebar props
| Prop | Type | What it does |
| --- | --- | --- |
| `header` | `ReactNode` | Logo or workspace name at the top. |
| `footer` | `ReactNode` | NavItems below the main ones, like Settings and Help. |
| `user` | `ReactNode` | The signed-in person, below a divider. |
| `collapsed` | `boolean \| undefined` | Shows only icons. Figma: Layout=Collapsed. |
| `defaultCollapsed` | `boolean \| undefined` |  |
| `onCollapsedChange` | `((collapsed: boolean) => void) \| undefined` | Pass to show a collapse button in the header. |
| `aria-label` | `string \| undefined` | Screen reader name for the navigation landmark. |
| `classNames` | `Partial<Record<SidebarPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<SidebarPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### NavItem props
| Prop | Type | What it does |
| --- | --- | --- |
| `children` (required) | `ReactNode` | Figma: Label. |
| `icon` | `ReactNode` | Figma: L icon. Needed in a collapsed Sidebar. |
| `badge` | `ReactNode` | A count or short label at the end. Figma: Badge. |
| `expanded` | `boolean \| undefined` | Shows a caret for items that open a group. Pass the open state. Figma: Chevron. |
| `selected` | `boolean \| undefined` | The page people are on. Figma: State=Selected. |
| `disabled` | `boolean \| undefined` |  |
| `onClick` | `MouseEventHandler<HTMLElement> \| undefined` | Renders a button instead of a link when there is no href. |
| `classNames` | `Partial<Record<NavItemPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<NavItemPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### NavGroup props
| Prop | Type | What it does |
| --- | --- | --- |
| `label` (required) | `ReactNode` | Figma: Label. |
| `icon` | `ReactNode` | Figma: L icon. Needed in a collapsed Sidebar. |
| `children` (required) | `ReactNode` | NavItems inside the group. |
| `open` | `boolean \| undefined` | Whether the items show. Opens by itself when one of them is selected. |
| `defaultOpen` | `boolean \| undefined` |  |
| `onOpenChange` | `((open: boolean) => void) \| undefined` |  |
| `className` | `string \| undefined` |  |
It also takes the usual HTML attributes for its element.

### TopBar props
| Prop | Type | What it does |
| --- | --- | --- |
| `logo` | `ReactNode` | Logo or product name on the left. |
| `actions` | `ReactNode` | Search, notifications and the account menu on the right. |
| `onMenuClick` | `(() => void) \| undefined` | Shows a menu button on small screens, where the links and the logo are hidden. Open a Drawer from it with the logo and the links. Figma: Layout=Mobile. |
| `aria-label` | `string \| undefined` | Defines a string value that labels the current element. |
| `classNames` | `Partial<Record<TopBarPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<TopBarPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { useState } from 'react';
import { NavGroup, NavItem, Sidebar } from '@halo-ds/react';
import { ChartBar, FolderSimple, Gear, House, Users } from '@phosphor-icons/react';

export default function Example() {
  const [page, setPage] = useState('home');
  const item = (id: string, label: string, icon?: React.ReactNode) => (
    <NavItem icon={icon} selected={page === id} onClick={() => setPage(id)}>{label}</NavItem>
  );
  return (
    <div style={{ height: 420, display: 'flex', borderRadius: 16, overflow: 'hidden', boxShadow: 'inset 0 0 0 1px var(--halo-border-secondary)' }}>
      <Sidebar header={<strong>Studio</strong>} footer={item('settings', 'Settings', <Gear />)}>
        {item('home', 'Home', <House />)}
        {item('projects', 'Projects', <FolderSimple />)}
        <NavGroup label="Reports" icon={<ChartBar />}>
          {item('sales', 'Sales')}
          {item('traffic', 'Traffic')}
        </NavGroup>
        {item('team', 'Team', <Users />)}
      </Sidebar>
    </div>
  );
}
```

### Top bar
Links across the top for apps with a few main sections.
```tsx
import { useState } from 'react';
import { NavItem, TopBar } from '@halo-ds/react';

export default function Demo() {
  const [page, setPage] = useState('home');
  const item = (id: string, label: string) => (
    <NavItem selected={page === id} onClick={() => setPage(id)}>{label}</NavItem>
  );
  return (
    <div className="demo-panel" style={{ width: '100%', padding: 0, overflow: 'hidden' }}>
      <TopBar logo={<strong>Studio</strong>}>
        {item('home', 'Home')}
        {item('projects', 'Projects')}
        {item('reports', 'Reports')}
      </TopBar>
    </div>
  );
}
```

### Collapsed sidebar
Icons only, to give the page more room. Groups open in a panel beside the bar.
```tsx
import { useState } from 'react';
import { NavGroup, NavItem, Sidebar } from '@halo-ds/react';
import { ChartBar, FolderSimple, House } from '@phosphor-icons/react';

export default function Demo() {
  const [page, setPage] = useState('home');
  const item = (id: string, label: string, icon?: React.ReactNode) => (
    <NavItem icon={icon} selected={page === id} onClick={() => setPage(id)}>{label}</NavItem>
  );
  return (
    <div style={{ height: 320, display: 'flex' }}>
      <Sidebar collapsed>
        {item('home', 'Home', <House />)}
        {item('projects', 'Projects', <FolderSimple />)}
        <NavGroup label="Reports" icon={<ChartBar />}>
          {item('sales', 'Sales')}
          {item('traffic', 'Traffic')}
        </NavGroup>
      </Sidebar>
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/navigation
