aiellie

Search

Find a component, template or page

GitHub
Design

Nav Bars

A rail of short bars for a page's sections or a thread's turns, marking what's in view. Hover for a list, a peek at one item, or a rail that expands into labels.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/nav-bars"

shadcn needs the @aiellie registry in components.json first. npx aiellie add nav-bars adds it on its first run, then installs the same way.

Nav bars is a quiet table of contents for a long page or thread: a column of short ticks beside the content, one per section, the ones in view drawn darker. Hovering shows the sections' names, in one of three ways: a card with the whole list, a card for the one tick under the pointer, or the rail growing into a panel.

Getting started

  1. List the sections

    Each item's id should be the id of the element it stands for, so the same list can scroll to it and watch it.

    components/page-nav.tsx
    "use client"
    
    import {
      NavBars,
      useNavBarsInView,
      type NavBarsItem,
    } from "@/components/ui/nav-bars"
    
    export function PageNav({ items }: { items: NavBarsItem[] }) {
      const active = useNavBarsInView(items.map((item) => item.id))
    
      return (
        <NavBars
          items={items}
          active={active}
          onSelect={(id) =>
            document.getElementById(id)?.scrollIntoView({ behavior: "smooth" })
          }
          className="fixed end-4 top-1/2 -translate-y-1/2"
        />
      )
    }
  2. Watch a scroll container

    useNavBarsInView watches the window by default. Pass a ref to the element that scrolls when that's something else, like a thread.

    const scroller = React.useRef<HTMLDivElement>(null)
    const active = useNavBarsInView(ids, scroller)
    
    <div ref={scroller} className="overflow-y-auto">{sections}</div>

Anatomy

<nav data-slot="nav-bars" data-variant="list" aria-label="On this page">
  <button data-slot="nav-bars-tick" aria-label aria-current="location" data-active /> {/* one per item */}
</nav>

{/* list: one card with every item */}
<div data-slot="hover-card-content">
  <button data-slot="nav-bars-row" /> {/* a dot for the active ones, and the label */}
</div>

{/* peek: one card that follows the tick under the pointer */}
<div data-slot="hover-card-content">{label}{description}</div>

{/* expand: the rail itself widens, each row a tick and a label */}

Ticks are 12px wide and 2px tall, in a 28 by 10px button, so they're easy to hit while staying slight. Active ones take the foreground color and get aria-current="location". Cards open after 150ms, so passing the pointer over the rail doesn't flash them.

In expand the nav keeps the rail's width and the panel grows over what's beside it, rather than pushing the content along. It opens on keyboard focus too. side sets which way a card opens, or the rail grows.

Examples

Peek

variant="peek" shows one card for the tick under the pointer, with its description under the label. The card moves from tick to tick rather than closing and opening.

Goal

What the plan is for. A few lines of the section go here, so there is something to scroll past.

Steps

What happens, in order. A few lines of the section go here, so there is something to scroll past.

Risks

What could go wrong. A few lines of the section go here, so there is something to scroll past.

Owners

Who does each step. A few lines of the section go here, so there is something to scroll past.

Expand

variant="expand" widens the rail into a panel of labels, over the content beside it.

Goal

What the plan is for. A few lines of the section go here, so there is something to scroll past.

Steps

What happens, in order. A few lines of the section go here, so there is something to scroll past.

Risks

What could go wrong. A few lines of the section go here, so there is something to scroll past.

Owners

Who does each step. A few lines of the section go here, so there is something to scroll past.

On the other side

For a rail at the start of the page, open toward the end.

<NavBars side="inline-start" items={items} onSelect={scrollTo} />

API reference

NavBars

PropTypeDefaultDescription
itemsNavBarsItem[]requiredThe sections, in page order.
onSelect(id: string) => voidrequiredCalled with an item's id when its tick or row is pressed.
activestring[][]The ids of the items in view.
variant"list" | "peek" | "expand""list"How the names show on hover.
side"inline-start" | "inline-end""inline-end"Which way the card opens, or the rail grows.
aria-labelstring"On this page"The nav's name.
classNamestringMerged onto the nav.

NavBarsItem

FieldTypeDescription
idstringThe section's element id.
labelstringIts name, read out on the tick and shown on hover.
descriptionstringA line or two under the label, in the peek card.

useNavBarsInView

ExportTypeDescription
useNavBarsInView(ids, root?)(ids: string[], root?: RefObject<Element>) => string[]The ids of the elements on screen, in the order given. root is the scroll container, or the window if left out.