EQTY LabDocs

Nav

Sidebar navigation: an icon rail, a switcher, and lists of links that drill in or open as folders

View as Markdown

Overview

Nav builds sidebars. NavRail holds an app’s largest sections as icons, and NavPanel beside it holds the current section’s pages, with a NavSwitcher in its header. Every row goes to a page. When one list can’t hold them all, a row drills in or expands.

Which One to Use

You want toUseFor exampleNot for
Go to a pageA row in a groupDashboard, IndicatorsActions, such as Log out: use a Dropdown Menu
Switch to another object, like a project, that the current section belongs toNavSwitcherAnother projectFiltering a page: use a Select
Navigate into a sub-section with its own pages, where other rows in the current section stop matteringDrill inA policy, an agent’s run, an app’s Settings pagesA few views of one screen: use Tabs
Browse pages grouped by topicExpandA docs site’s folders, at any depthA sub-section people work inside: drill in
Move between an app’s largest sectionsNavRailProjects, Agents, LibraryAn app with one section

Usage

Import the parts:

tsx
import {
  NavDrill,
  NavDrillLevel,
  NavList,
  NavListExpandable,
  NavListExpandableContent,
  NavListExpandableTrigger,
  NavListGroup,
  NavListItem,
  NavListSeparator,
  NavPanel,
  NavRail,
  NavRailItem,
  NavSwitcher,
} from "@eqtylab/equality";

A rail beside a panel, with a switcher in the panel’s header and the rows below:

tsx
const projectOptions = projects.map((p) => ({ value: p.id, label: p.name }));

<div style={{ display: "flex", height: "100vh" }}>
  <NavRail aria-label="Main">
    <NavRailItem icon={<Folder />} label="Projects" href="/projects" current />
    <NavRailItem icon={<Bot />} label="Agents" href="/agents" />
  </NavRail>
  <NavPanel
    aria-label={`Project: ${project.name}`}
    header={
      <NavSwitcher
        label={project.name}
        noun="project"
        collection="projects"
        options={projectOptions}
        value={project.id}
        onValueChange={switchProject}
      />
    }
  >
    <NavList>
      <NavListGroup label="Project">
        <NavListItem
          label="Dashboard"
          prefix={<LayoutGrid />}
          href="/"
          current
        />
      </NavListGroup>
    </NavList>
  </NavPanel>
  <main>…</main>
</div>;

Groups

Give each group an optional label. A separator sets off a different kind of group, like settings at the foot. Mark the current page with current.

jsx
<NavList>
  <NavListGroup label="Project">
    <NavListItem label="Dashboard" prefix={<LayoutGrid />} href="/" current />
    <NavListItem label="Indicators" prefix={<Activity />} href="/indicators" />
  </NavListGroup>
  <NavListGroup label="Applied Policies">
    <NavListItem
      label="EU AI Act"
      prefix={<BookOpen />}
      href="/policies/eu-ai-act"
    />
  </NavListGroup>
  <NavListSeparator />
  <NavListGroup>
    <NavListItem
      label="Project Settings"
      prefix={<Settings />}
      href="/settings"
    />
  </NavListGroup>
</NavList>

href makes a plain link. With a router, pass its link through asChild, so the page doesn’t reload. A drill row needs neither. Set current from the route.

jsx
<NavListItem label="Indicators" current={pathname === "/indicators"} asChild>
  <Link to="/indicators" />
</NavListItem>

target="_blank" opens the row in a new tab, with a ↗.

jsx
<NavListItem
  label="Status Page"
  href="https://status.eqtylab.io"
  target="_blank"
/>

Prefix and Suffix

prefix takes an icon. suffix takes a status or count as Badge size="sm", and screen readers read it with the label.

jsx
<NavListItem
  label="Applied Policies"
  prefix={<BookOpen />}
  suffix={<Badge size="sm">2</Badge>}
  href="/policies"
/>

Switcher

NavSwitcher goes in the panel’s header and shows the current item’s name. Picking another item calls onValueChange. Keep the reader on the same page in the new item, such as Indicators. If they were inside a policy, take them to the new item’s top level instead: that policy belongs to the old item. action adds a command at the foot of the list.

tsx
const projectOptions = projects.map((p) => ({ value: p.id, label: p.name }));

<NavSwitcher
  label={project.name}
  noun="project"
  collection="projects"
  options={projectOptions}
  value={project.id}
  onValueChange={switchProject}
  action={{ label: "Create Project", onSelect: openCreateProject }}
/>;

When the List Fails to Load

Pass error and onRetry, and the list offers Retry: Couldn't load {collection}. with no options, or Some {collection} couldn't be loaded. over a partial list. Pass retrying while it reloads.

tsx
const projects = projectsQuery.data ?? [];
const projectOptions = projects.map((p) => ({ value: p.id, label: p.name }));

<NavSwitcher
  {...props}
  options={projectOptions}
  error={projectsQuery.isError}
  retrying={projectsQuery.isFetching}
  onRetry={() => projectsQuery.refetch()}
/>;

Drill-in

A row with drill opens the NavDrillLevel with that value. A level with a parent gets a ‹ row that goes back. Levels are direct children of NavDrill. Put the list on a background colour, like a panel or sheet, or the levels show through as they slide.

Uncontrolled

Set the first level with defaultValue. Use it where the URL doesn’t change, like a popover or sheet.

jsx
<NavList>
  <NavDrill defaultValue="project">
    <NavDrillLevel value="project">
      <NavListGroup label="Applied Policies">
        <NavListItem
          label="EU AI Act"
          prefix={<BookOpen />}
          drill="eu-ai-act"
        />
      </NavListGroup>
    </NavDrillLevel>
    <NavDrillLevel value="eu-ai-act" parent="project" label="EU AI Act">
      <NavListGroup>
        <NavListItem
          label="Controls"
          prefix={<Layers />}
          href="/policies/eu-ai-act/controls"
        />
      </NavListGroup>
    </NavDrillLevel>
  </NavDrill>
</NavList>

Controlled by the URL

In an app, set value from the URL, so Back and bookmarks open the right level. onValueChange gets the level a click asks for: navigate there.

jsx
const { policy } = useParams();
const navigate = useNavigate();

function openLevel(level) {
  navigate(level === "project" ? "/project" : `/policies/${level}`);
}

<NavDrill value={policy ?? "project"} onValueChange={openLevel}>
  …
</NavDrill>;

Expandable Groups

NavListExpandable is a folder. Put it in a NavListGroup, or inside another folder. A folder opens itself when a row inside it becomes current.

jsx
<NavListGroup>
  <NavListExpandable defaultOpen>
    <NavListExpandableTrigger label="Getting Started" />
    <NavListExpandableContent>
      <NavListItem label="Installation" href="/installation" current />
      <NavListItem label="Theming" href="/theming" />
    </NavListExpandableContent>
  </NavListExpandable>
</NavListGroup>

Nested Folders

Folders nest to any depth. Each level indents, so long labels cut off sooner.

jsx
<NavListExpandable defaultOpen>
  <NavListExpandableTrigger label="Deployment" />
  <NavListExpandableContent>
    <NavListItem label="Quick Start" href="/deployment/quick-start" />
    <NavListExpandable defaultOpen>
      <NavListExpandableTrigger label="Baremetal" />
      <NavListExpandableContent>
        <NavListItem
          label="K3s CoCo"
          href="/deployment/baremetal/k3s-coco"
          current
        />
      </NavListExpandableContent>
    </NavListExpandable>
  </NavListExpandableContent>
</NavListExpandable>

Folders in a Drill-in

Inside NavDrill, give each folder a value, unique within its level. The folder then stays open or closed when the reader drills away and comes back. Without a value, it forgets when its level rebuilds.

jsx
<NavDrillLevel value="project">
  <NavListGroup>
    <NavListExpandable value="reports">
      <NavListExpandableTrigger label="Reports" />
      <NavListExpandableContent>…</NavListExpandableContent>
    </NavListExpandable>
  </NavListGroup>
</NavDrillLevel>

Examples

App Sidebar

The sidebar from Usage in full, with router links and a policy level that follows the URL.

tsx
const { projectId, policyId } = useParams();
const { pathname } = useLocation();
const navigate = useNavigate();
const base = `/projects/${projectId}`;
const policy = policies.find((item) => item.id === policyId);
const projectOptions = projects.map((p) => ({ value: p.id, label: p.name }));

function goToLevel(level) {
  navigate(level === "project" ? base : `${base}/policies/${level}`);
}

<div style={{ display: "flex", height: "100vh" }}>
  <NavRail aria-label="Main">
    <NavRailItem icon={<Folder />} label="Projects" current asChild>
      <Link to="/projects" />
    </NavRailItem>
    <NavRailItem icon={<Bot />} label="Agents" asChild>
      <Link to="/agents" />
    </NavRailItem>
  </NavRail>
  <NavPanel
    aria-label={
      policy ? `Applied Policy: ${policy.name}` : `Project: ${project.name}`
    }
    header={
      <NavSwitcher
        label={project.name}
        noun="project"
        collection="projects"
        options={projectOptions}
        value={project.id}
        onValueChange={(id) => navigate(`/projects/${id}`)}
      />
    }
  >
    <NavList>
      <NavDrill value={policy?.id ?? "project"} onValueChange={goToLevel}>
        <NavDrillLevel value="project">
          <NavListGroup label="Project">
            <NavListItem
              label="Dashboard"
              prefix={<LayoutGrid />}
              current={pathname === base}
              asChild
            >
              <Link to={base} />
            </NavListItem>
          </NavListGroup>
          <NavListGroup label="Applied Policies">
            {policies.map((item) => (
              <NavListItem
                key={item.id}
                label={item.name}
                prefix={<BookOpen />}
                drill={item.id}
              />
            ))}
          </NavListGroup>
        </NavDrillLevel>
        {policy && (
          <NavDrillLevel
            value={policy.id}
            parent="project"
            label={policy.name}
            backLabel={`${policy.name}, back to ${project.name}`}
          >
            <NavListGroup>
              <NavListItem label="Controls" prefix={<Layers />} asChild>
                <Link to={`${base}/policies/${policy.id}/controls`} />
              </NavListItem>
            </NavListGroup>
          </NavDrillLevel>
        )}
      </NavDrill>
    </NavList>
  </NavPanel>
  <main>…</main>
</div>;

Docs Sidebar

Rows built from a tree of folders and pages. Folders around the current row open themselves. @eqtylab/docs builds its sidebar this way.

tsx
function Rows({ nodes, top = false }) {
  const { pathname } = useLocation();
  return nodes.map((node) => {
    const prefix = top && node.icon ? <Icon icon={node.icon} /> : undefined;
    const suffix = node.badge && <Badge size="sm">{node.badge}</Badge>;
    return node.children ? (
      <NavListExpandable key={node.id}>
        <NavListExpandableTrigger
          label={node.label}
          prefix={prefix}
          suffix={suffix}
        />
        <NavListExpandableContent>
          <Rows nodes={node.children} />
        </NavListExpandableContent>
      </NavListExpandable>
    ) : (
      <NavListItem
        key={node.id}
        label={node.label}
        href={node.href}
        current={node.href === pathname}
        prefix={prefix}
        suffix={suffix}
      />
    );
  });
}

<NavList>
  <NavListGroup>
    <Rows nodes={tree} top />
  </NavListGroup>
</NavList>;

Under a Sticky Header

The rail and panel pin to the top of the viewport as the page scrolls. Under a sticky header, set --eq-nav-offset to the header’s height, a length such as 64px, on the element that wraps the rail and panel, and they pin below it instead. Don’t set it on :root: a Sheet holding the sidebar on a phone would inherit it and push the list down.

tsx
<>
  <header style={{ position: "sticky", top: 0, height: 64 }}>…</header>
  <div style={{ display: "flex", "--eq-nav-offset": "64px" }}>
    <NavRail aria-label="Main">…</NavRail>
    <NavPanel aria-label={`Project: ${project.name}`} header={<NavSwitcher … />}>
      …
    </NavPanel>
    <main>…</main>
  </div>
</>

On a phone, put the whole sidebar in a Sheet from the left: the rail and panel side by side, the switcher in the panel’s header. Close the sheet when the route changes. Give it a hidden title. Set minWidth: 0 so the sheet fits the list.

jsx
const { pathname } = useLocation();
const [open, setOpen] = useState(false);

useEffect(() => setOpen(false), [pathname]);

<Sheet open={open} onOpenChange={setOpen}>
  <SheetTrigger asChild>
    <Button>Open navigation</Button>
  </SheetTrigger>
  <SheetContainer
    side="left"
    aria-describedby={undefined}
    style={{ minWidth: 0 }}
  >
    <SheetTitle className="sr-only">Navigation</SheetTitle>
    <div style={{ display: "flex", height: "100%" }}>
      <NavRail aria-label="Main">…</NavRail>
      <NavPanel aria-label={`Project: ${project.name}`} header={<NavSwitcher … />}>
        …
      </NavPanel>
    </div>
  </SheetContainer>
</Sheet>;

Best Practices

Use Nav for Pages, Not Actions

Every row takes the reader somewhere. Put actions elsewhere: Log Out in a Dropdown Menu, Create Project in the switcher’s action. A reader who clicks a row expects to go somewhere, not to change something.

Statuses and Counts in the Suffix

Use Badge size="sm". Screen readers skip the prefix, and a badge there pushes the label out of line.

Icons on Every Top-Level Row, or None

Mixed icons make the labels zigzag.

Don’t Restyle Rows

A row with its own colours or sizes reads as an error, or as a different control.

Don’t Add Your Own Arrows

The row adds them itself: › when you set drill, ↗ when you set target="_blank".

Server-Rendered Pages: Start the Current Folder Open

Pass defaultOpen to the folder holding the current page, so it’s open before the page’s scripts load. With JavaScript off, every folder shows open.

Put a Folder’s Own Page Inside It

A folder’s header only opens and closes the folder; it can’t link to a page. If the folder has a page of its own, add it as the first row inside, such as Overview.

Name the Rail and Panel for What They Hold

Screen readers list NavRail and NavPanel as navigation landmarks by their aria-label. Use names that tell them apart, like “Main” and `Project: ${project.name}`.

Slots

NameDescription
NavRailThe column of icon links
NavRailItemAn icon link in the rail, with its label as a tooltip
NavPanelThe sidebar panel, with a header
NavSwitcherSwitches between items, in the panel’s header
NavListRoot of the rows
NavListGroupA list of rows, with an optional label
NavListItemA row: a link, or a button for a drill row
NavListSeparatorDivider between kinds of group
NavListExpandableA folder that opens and closes
NavListExpandableTriggerThe folder’s header row
NavListExpandableContentThe folder’s rows
NavDrillShows one level at a time and slides between them
NavDrillLevelOne level of a drill-in, with its ‹ row

Props

NavList, NavListSeparator and NavListExpandableContent take no props of their own.

NameDescriptionTypeDefaultRequired
aria-labelNames the rail’s landmarkstring✅
NameDescriptionTypeDefaultRequired
iconThe item’s only visible contentReactNode✅
labelThe item’s accessible name and its tooltipstring✅
hrefWhere the item goesstringWithout asChild
currentMarks the current sectionbooleanfalse❌
asChildRenders the item as its child, like a router linkbooleanfalse❌
NameDescriptionTypeDefaultRequired
aria-labelNames the panel’s landmarkstring✅
headerAbove the rows: a NavSwitcher or a headingReactNode❌
NameDescriptionTypeDefaultRequired
labelThe current item’s name, shown as the panel’s headingstring✅
nounSingular, for the trigger’s name: {label}, switch {noun}string✅
collectionPlural, for the search and empty copystring✅
optionsThe items to switch between{ value, label, keywords? }[]✅
valueThe current item’s valuestring✅
onValueChangeCalled with the picked item’s value(value: string) => void✅
onHighlightHover or focus on an option, to preload its page(value: string) => void❌
onHighlightEndHover or focus leaves an option, to cancel the preload() => void❌
actionA command at the foot of the list, kept while searching; runs after the list closes{ label: string; onSelect: () => void }❌
errorThe list failed to load, and offers Retrybooleanfalse❌
retryingA retry is running, and the Retry row says sobooleanfalse❌
onRetryCalled when Retry is picked() => voidWith error
loadingHolds the switcher’s height while the current item loadsbooleanfalse❌

With loading, pass no other props. With no options, action or error, it’s a plain heading.

NameDescriptionTypeDefaultRequired
labelNames the group’s liststring❌
NameDescriptionTypeDefaultRequired
labelThe row’s textstring✅
hrefMakes the row a linkstring❌
target, relPassed to the link. target="_blank" adds ↗, “(opens in a new tab)” and rel="noopener noreferrer"string❌
currentMarks the current pagebooleanfalse❌
prefixAn icon before the label; top-level rows onlyReactNode❌
suffixA status or count after the label, read with itReactNode❌
drillThe level this row opens; adds ›string❌
asChildRenders the row as its child, like a router linkbooleanfalse❌
NameDescriptionTypeDefaultRequired
defaultOpenUncontrolled: opens the folder on first renderbooleanfalse❌
openControlled: whether the folder is openboolean❌
onOpenChangeCalled with the new state when the header is pressed, or when the current page moves inside(open: boolean) => void❌
valueInside NavDrill, keeps the folder open or closed when the reader drills away and back; unique within its levelstring❌
NameDescriptionTypeDefaultRequired
labelThe folder’s namestring✅
prefixAn icon before the label; top-level folders onlyReactNode❌
suffixA status or count after the label, read with itReactNode❌
NameDescriptionTypeDefaultRequired
valueControlled: the current levelstring❌
defaultValueUncontrolled: the first levelstring❌
onValueChangeCalled when a drill row or ‹ asks for a level; request says which(value: string, request: "drill" | "back") => void❌
animateSlides between levelsbooleantrue❌
NameDescriptionTypeDefaultRequired
valueThe NavDrill value that shows itstring✅
parentThe value ‹ returns to; adds the ‹ rowstring❌
labelNames where you are, on the ‹ rowstringWith parent
backLabelThe ‹ row’s accessible name; must start with labelstring{label}, back❌