Skip to main content

Sidebar Nav

The page tree of a documentation site: groups under caps headings, their pages on a hairline guide, and the current page marked. The same component fills the docs layout's sidebar column and its drawer.

import { SidebarNav } from '@fairgarden/design/navigation/sidebar-nav'

Pass items as plain data, { title, href, items }, so the column and the drawer render the same object; a docs site builds it from the docs engine's sitemap with toSidebarItems from @fairgarden/design/utils/docs. Pass currentPath and the page whose path matches takes aria-current="page". Links are plain anchors unless you pass renderLink, such as (href) => <NextLink href={href} />. The nav is named "Documentation" unless you pass label.

Groups and pages

Groups and pages

Each top-level item with items is a group: its heading is caps wayfinding in type-label, a link to the group's index page when it has an href, and hairline rules split the groups. Pages are type-body-ui links on 44 px rows, hung on a hairline guide at the list's start edge; a page's own items nest one level in on their own guide. The current page takes the 3 px start-edge bar over the guide and weight 700, and every label reserves its bold width, so moving the current page never rewraps a line. Hover follows the list-link rule: the link-hover ink, never a fill. When the page loads, and whenever the current page changes, its link is scrolled into view inside the column or the drawer that holds it, never by scrolling the page, so nothing around it moves. In print the navigation is hidden.

import * as React from 'react'
import { SidebarNav } from '@fairgarden/design/navigation/sidebar-nav'
import { handbook } from './handbook'
import styles from './groups.module.css'

/** The handbook's page tree, on the Composting page, at the docs layout's 240 px column width. */
export function SidebarNavGroups() {
  return (
    <div className={styles.column}>
      <SidebarNav items={handbook} currentPath="/handbook/composting" label="Handbook" />
    </div>
  )
}

Collapsible groups

Collapsible groups

With collapsible, each group is an independent Collapsible, so several may be open. A group with an index page keeps its heading as a link and gets a 44 px glyph toggle beside it; a group without one makes its whole heading row the toggle. The expand/collapse glyph points out when the group is closed and in when it is open, swapping rather than rotating. The group holding the current page starts open in the server HTML; pass an item's defaultOpen to override it. The toggle is named after its group ("Garden care pages") unless you pass toggleLabel.

import * as React from 'react'
import { SidebarNav, type SidebarNavItem } from '@fairgarden/design/navigation/sidebar-nav'
import styles from './collapsible.module.css'

/** A longer tree: each group folds away, and the one holding the current page starts open. */
const handbook: SidebarNavItem[] = [
  {
    title: 'Getting started',
    href: '/handbook',
    items: [
      { title: 'Welcome', href: '/handbook/welcome' },
      { title: 'Your first season', href: '/handbook/first-season' },
      { title: 'Plot agreements', href: '/handbook/plot-agreements' },
    ],
  },
  {
    title: 'Garden care',
    href: '/handbook/garden-care',
    items: [
      { title: 'Soil and beds', href: '/handbook/soil' },
      { title: 'Watering', href: '/handbook/watering' },
      { title: 'Composting', href: '/handbook/composting' },
      { title: 'Seed saving', href: '/handbook/seed-saving' },
    ],
  },
  {
    title: 'Shared spaces',
    href: '/handbook/shared-spaces',
    items: [
      { title: 'Tool shed', href: '/handbook/tool-shed' },
      { title: 'Water points', href: '/handbook/water-points' },
      { title: 'Work days', href: '/handbook/work-days' },
    ],
  },
  {
    // A group without an index page: its whole heading row is the toggle.
    title: 'Forms',
    items: [
      { title: 'Plot application', href: '/handbook/forms/plot-application' },
      { title: 'Tool loan', href: '/handbook/forms/tool-loan' },
      { title: 'Work-day sign-up', href: '/handbook/forms/work-day' },
    ],
  },
]

/** Collapsible groups: the heading links to the group's page, the glyph beside it folds the group. */
export function SidebarNavCollapsible() {
  return (
    <div className={styles.column}>
      <SidebarNav items={handbook} currentPath="/handbook/watering" label="Handbook" collapsible />
    </div>
  )
}

API Reference

SidebarNav

The docs page tree [D202]: a nav of groups, each a caps heading (type-label, a link to the group’s index page when it has one) over its pages on a --role-hairline guide, groups split by hairline rules. The current page takes aria-current="page", the --border-size-2–25 start-edge bar and weight 700. The same component, from the same data, fills the docs layout’s column from --fgd-nav-inline-n-above and the drawer below it. On mount, and when the current page changes, the current link is scrolled into view inside the nearest scrolling ancestor (the column or the drawer’s Scroll Area), never the page, so nothing moves around it. Hidden in print.

PropTypeDescription
label
string | undefined

The landmark’s name. Default “Documentation”.

collapsible
boolean | undefined

Groups become independent Collapsibles, each heading with the D109 glyph; the group holding the current page opens by default. Default false: every group open, no toggles.

currentPath
string | undefined

The current page’s URL path; its link takes aria-current="page".

items
SidebarNavItem[] | undefined

The page tree: groups (a caps heading over its pages) and, rarely, top-level pages. Plain data, so the desktop column and the drawer render the same object.

primary
| 'ruby'
| 'olive'
| 'sage'
| 'slate'
| 'sand'
| 'gray'
| 'mauve'
| 'brown'
| 'bronze'
| 'gold'
| 'red'
| 'crimson'
| 'tomato'
| 'pink'
| 'plum'
| 'indigo'
| 'iris'
| 'violet'
| 'purple'
| null
| undefined

Override the scope’s primary. Never defaulted [D133].

renderLink
((href: string) => ReactElement) | undefined

Builds each anchor, e.g. (href) => <NextLink href={href} />. Default: a plain <a href>.

secondary
| 'ruby'
| 'olive'
| 'sage'
| 'slate'
| 'sand'
| 'gray'
| 'mauve'
| 'brown'
| 'bronze'
| 'gold'
| 'red'
| 'crimson'
| 'tomato'
| 'pink'
| 'plum'
| 'indigo'
| 'iris'
| 'violet'
| 'purple'
| 'amber'
| 'blue'
| 'cyan'
| 'grass'
| 'green'
| 'jade'
| 'lime'
| 'mint'
| 'orange'
| 'sky'
| 'teal'
| 'yellow'
| null
| undefined

Override the scope’s secondary. Never defaulted.

toggleLabel
((title: string) => string) | undefined

The toggle’s accessible name for a group. Default: “{title} pages”.

Additional types

sidebarNav
SidebarNavItem

One entry of the page tree: a group (top level, with items), a page, or a page with subpages.

type SidebarNavItem = {
  /** The link text, authored in sentence case; a group heading sets its own caps [D165]. */
  title: string;
  /** The page. Omitted on a group, whose heading is then plain text. */
  href?: string;
  /**
   * At the top level: the group's pages, under its caps heading. On a page:
   * its subpages, one level deeper on an indented guide. Nest no deeper.
   */
  items?: SidebarNavItem[];
  /** Marks the current page. Default: its path equals `currentPath`. */
  current?: boolean;
  /**
   * A collapsible group's initial state. Default: open when it holds the
   * current page.
   */
  defaultOpen?: boolean;
}
SidebarNavProps

Props for SidebarNav: nav props, the page tree, the current path and the color axes.

type SidebarNavProps = {
  /**
   * The page tree: groups (a caps heading over its pages) and, rarely,
   * top-level pages. Plain data, so the desktop column and the drawer
   * render the same object.
   */
  items: SidebarNavItem[];
  /** The current page's URL path; its link takes `aria-current="page"`. */
  currentPath?: string;
  /** The landmark's name. Default "Documentation". */
  label?: string;
  /**
   * Groups become independent Collapsibles, each heading with the D109
   * glyph; the group holding the current page opens by default. Default
   * `false`: every group open, no toggles.
   */
  collapsible?: boolean;
  /** The toggle's accessible name for a group. Default: "{title} pages". */
  toggleLabel?: (title: string) => string;
  /**
   * Builds each anchor, e.g. `(href) => <NextLink href={href} />`.
   * Default: a plain `<a href>`.
   */
  renderLink?: (href: string) => ReactElement;
  /** Override the scope's primary. Never defaulted [D133]. */
  primary?:
    | 'ruby'
    | 'olive'
    | 'sage'
    | 'slate'
    | 'sand'
    | 'gray'
    | 'mauve'
    | 'brown'
    | 'bronze'
    | 'gold'
    | 'red'
    | 'crimson'
    | 'tomato'
    | 'pink'
    | 'plum'
    | 'indigo'
    | 'iris'
    | 'violet'
    | 'purple'
    | null;
  /** Override the scope's secondary. Never defaulted. */
  secondary?:
    | 'ruby'
    | 'olive'
    | 'sage'
    | 'slate'
    | 'sand'
    | 'gray'
    | 'mauve'
    | 'brown'
    | 'bronze'
    | 'gold'
    | 'red'
    | 'crimson'
    | 'tomato'
    | 'pink'
    | 'plum'
    | 'indigo'
    | 'iris'
    | 'violet'
    | 'purple'
    | 'amber'
    | 'blue'
    | 'cyan'
    | 'grass'
    | 'green'
    | 'jade'
    | 'lime'
    | 'mint'
    | 'orange'
    | 'sky'
    | 'teal'
    | 'yellow'
    | null;
}

Specification: DESIGN-SYSTEM.md §9.15 (sidebar navigation) [D202], in the docs layout of §11.15, with the disclosure glyph of §10.13 [D109].