Table of contents
Links to a page's sections that mark the one you are reading as you scroll.
Demo
Usage
import { useState } from 'react'
import { TableOfContents } from '@tuttn/slant'
// Each entry links to the element with its id; levels 2 and 3 indent.
const items = [
{ id: 'install', label: 'Install' },
{ id: 'configure', label: 'Configure' },
{ id: 'themes', label: 'Themes', level: 2 },
{ id: 'upgrade', label: 'Upgrade' },
]
// Uncontrolled: follows the window. offset is the height of a sticky header.
<TableOfContents items={items} offset={64} />
// Controlled: you hold the current section's id.
<TableOfContents items={items} value={section} onValueChange={setSection} offset={64} />
// A scroll container of its own: pass the element through state, not
// ref.current, so the contents re-render once it's attached.
const [article, setArticle] = useState<HTMLElement | null>(null)
<article ref={setArticle} tabIndex={0} aria-label="Release notes" style={{ height: 400, overflowY: 'auto' }}>…</article>
<TableOfContents items={items} root={article} />
/* Your stylesheet: land a linked section below the sticky header. */
h2, h3 { scroll-margin-top: 72px; }Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly TableOfContentsEntry[] | — | The sections, in page order. Each links to the element whose id it carries. |
value | string | — | The current section's id, when controlled. |
defaultValue | string | the first item's id | The section marked current at first, when uncontrolled, until the page has been measured. |
onValueChange | (id: string) => void | — | Called with a section's id when it becomes current, by scrolling or by a click on its link. |
label | string | messages.onThisPage ("On this page") | The navigation's accessible name, also shown as a small heading over the links. |
hideLabel | boolean | false | Keeps `label` as the accessible name only, without the visible heading. |
offset | number | 0 | The px a sticky header covers at the top of the scroll container. A section is current once its top passes this line, plus a small margin. Where the page lands a linked section lower (its `scroll-margin-top` plus the container's `scroll-padding-top`), that lower line counts, so a clicked section stays current. |
root | HTMLElement | null | — | The scroll container the sections are in. Leave it out for the window; `null` (a ref that isn't attached yet) waits for the element. |
…rest | HTMLAttributes | — | className, style, other attributes and the ref go on the <nav>. |
An entry is { id, label, level? }, where id is the id of the element it links to. The links are plain <a href="#id"> anchors in the tab order: Tab moves through them and Enter follows one. There are no arrow keys, since a table of contents is a list of links, not a composite widget. The navigation is named by label ("On this page" by default), which also shows as a small heading unless hideLabel.
The current section is the last one whose top has passed the line: offset pixels from the top of the window or of root, plus a small margin. Before the first section it's the first one, and at the very bottom of the scroll range it's the last, since a short last section may never reach the line. The link has aria-current="location", and a marker slides along the rail to it (it jumps under reduced motion).
A click marks its section at once, and the browser does the scrolling; landing the section below a sticky header is the page's job, with scroll-margin-top or scroll-padding-top. Where those land a section lower than offset, that lower line counts, so a section you've just followed stays current. Scrolling, resizing and content that grows (a ResizeObserver) all measure again, at most once a frame.
Pass root as an element, not ref.current: hold it in state with a callback ref. null means it isn't attached yet, and the contents wait for it rather than following the window. All the measuring runs in effects, so the contents render on the server with the first section current.