NavItem

A navigation row with an icon, label, and active state, plus sub-items and a collapsed-rail mode for building a sidebar

Installation

pnpm add @wandercom/design-system-web

Usage

NavItem renders an <a> by default. Pass asChild to merge its props onto a framework's own link component instead (Next.js shown here), and pass active yourself — NavItem never reads the route:

14 lines
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { IconUsers } from '@wandercom/design-system-icons/users';
import { NavItem } from '@wandercom/design-system-web/ui/nav-item';

export function Example() {
  const pathname = usePathname();

  return (
    <NavItem active={pathname === '/guests'} asChild icon={<IconUsers />}>
      <Link href="/guests">Guests</Link>
    </NavItem>
  );
}

Rows are 34px tall with no gap between them and an 8px inset: an 18px icon, then a 14px/450 label at 34px in text-secondary. Hover and the current page share the surface-quaternary fill; the current page's label turns text-primary.

Examples

Toggle "Collapse" below to see the rows collapse to the icon rail. Finances and Growth are folders:

Loading example...
28 lines
import { IconChat } from '@wandercom/design-system-icons/chat';
import { IconHome } from '@wandercom/design-system-icons/home';
import {
  NavItem,
  NavSectionHeader,
} from '@wandercom/design-system-web/ui/nav-item';

export function Example() {
  return (
    <nav className="flex w-64 flex-col">
      <NavItem active icon={<IconHome />} href="/">
        Home
      </NavItem>

      <NavItem
        active
        icon={<IconChat />}
        href="/guests"
        subItems={[{ title: 'Messages', href: '/guests/messages' }]}
      >
        Guests
      </NavItem>

      <NavSectionHeader>Channels</NavSectionHeader>
      <NavItem href="/website">Website</NavItem>
    </nav>
  );
}

Sub-items

Pass subItems — an array of { title, href, active?, onClick? } — to pair a row with sub-items sharing its styling. NavItem renders them for you, so you never compose the rows yourself, in an inline panel below the row. Sub-items have no icon: their label is indented to 34px, in line with their parent's label. On the rail, sub-items are hidden:

12 lines
<NavItem
  active={isGuestsSectionActive}
  asChild
  icon={<IconUsers />}
  rail={isSideNavCollapsed}
  subItems={[
    { title: 'Messages', href: '/guests/messages', active: pathname === '/guests/messages' },
    { title: 'Reviews', href: '/guests/reviews', active: pathname === '/guests/reviews' },
  ]}
>
  <Link href="/guests">Guests</Link>
</NavItem>

The row (and, outside rail mode, the panel) opens whenever active is true or any sub-item's active is — NavItem never tracks this itself.

Folders

Leave out href (and asChild) to turn a row with subItems into a folder: a group of pages with no page of its own, such as "Finances". The row renders a <button> with a chevron right after its label. Clicking it, or pressing Enter or Space, opens or closes the panel without navigating. The button carries aria-expanded and an aria-controls that points at the sub-item list.

11 lines
<NavItem
  icon={<IconDollar />}
  rail={isSideNavCollapsed}
  subItems={[
    { title: 'Payouts', href: '/finance/payouts', active: pathname === '/finance/payouts', onClick: navigate },
    { title: 'Charges', href: '/finance/charges', active: pathname === '/finance/charges', onClick: navigate },
    { title: 'Discounts', href: '/finance/discounts', active: pathname === '/finance/discounts', onClick: navigate },
  ]}
>
  Finances
</NavItem>
  • Open state. Uncontrolled by default. It starts open when one of the sub-items is active (override with defaultOpen). Pass open and onOpenChange to control it, for example to remember it across reloads.
  • Opens by itself, never closes by itself. When a sub-item becomes active (the user navigated into the folder), the folder opens and onOpenChange(true) fires. Moving between pages never closes it, and the user can close it while one of its pages is current.
  • Active state. The folder row only highlights on hover (or with an explicit active); its sub-items never highlight it, open or closed.
  • Clicks. onClick runs before the toggle. Call event.preventDefault() in it to stop the toggle.
  • Chevron. 16px, tertiary, pointing down when open and right when closed, with no transition.
  • Rail. The folder becomes an icon-only link to its first sub-item, with no popover. A click calls onClick, then that sub-item's onClick (for client-side routing), unless onClick calls event.preventDefault().

Section headers

Use NavSectionHeader to label a group of items, such as a "Channels" section in a sidebar: 12px/450 tertiary text with an 8px inset and 6px below it (21px tall). Every header after the first starts its section 20px below the previous row:

3 lines
<NavSectionHeader>Channels</NavSectionHeader>
<NavItem icon={<IconGlobe />} href="/website">Website</NavItem>
<NavItem icon={<IconBuilding />} href="/marketplace">Marketplace</NavItem>

Collapsed rail

For a sidebar that's toggled down to icons only, pass rail. The row becomes a 34×34 icon-only button, with its icon in the same place as when expanded. Its label is visually hidden but still announced, and repeated in the button's title (a native tooltip) and aria-label when it's a string. There is no hover popover, so a row whose sub-item is the current page carries aria-current="true". The row keeps the same elements in both states, except that a folder swaps its button for a link to its first sub-item. Pass the same rail to NavSectionHeader, which hides its label and draws a 34px divider line at its top instead, without changing its height:

3 lines
<NavItem active={pathname === '/'} asChild icon={<IconHome />} rail={isSideNavCollapsed}>
  <Link href="/">Home</Link>
</NavItem>

Props

icon?:

React.ReactNode
Icon shown in the 18px leading slot. Ignored when subItem is true.

active?:

boolean
Whether this item (or one of its sub-items) represents the current page. Defaults to false — the app decides this, NavItem never reads the route.

subItems?:

{ title: string; href: string; active?: boolean; onClick?: (event) => void }[]
Sub-items in an inline panel below the row, hidden on the rail. With href, the panel opens whenever this item or one of these is active. Without href, the row is a folder (see Folders).

open?:

boolean
Controlled open state of a folder's inline sub-items panel. Only applies to a folder: subItems without href or asChild.

defaultOpen?:

boolean
Uncontrolled initial open state of a folder's panel. Defaults to whether one of its subItems is active.

onOpenChange?:

(open: boolean) => void
Called when the user toggles a folder, or when it opens on its own because a sub-item became active. A folder never closes on its own.

rail?:

boolean
Collapses this row to a 34×34 icon-only button for a sidebar toggled down to icons only. The label is visually hidden and repeated in title and aria-label. No popover; subItems stay hidden, and a folder links to its first sub-item.

subItem?:

boolean
Renders the sub-item variant: no icon, the label indented to 34px so it lines up with its parent's label. Set internally when rendering subItems — set it yourself only if you're building a sub-item row by hand instead of passing subItems.

asChild?:

boolean
Renders children as the root element instead of an <a>, merging this item's props onto it — use this to pass a Next.js Link or any other router's link component. Sub-items rendered from subItems are always plain <a>s.

className?:

string
Additional CSS classes.

className?:

string
Additional CSS classes.

children?:

React.ReactNode
Section label text.

rail?:

boolean
Defaults to false. For a sidebar collapsed to a rail: hides the label and draws a 34px divider line at its top instead. The header keeps its height either way, so the items below it don't move.

Deprecated: nothing renders a rail flyout any more, since a rail folder links to its first sub-item. Kept for compatibility.

active?:

boolean
Whether this sub-item represents the current page. Defaults to false.

asChild?:

boolean
Renders children as the root element instead of an <a>, merging this item's props onto it.

className?:

string
Additional CSS classes.

children?:

React.ReactNode
Sub-item label text. A plain text row for a custom flyout next to a rail. NavItem itself no longer renders a rail popover.
NavItem