Table of Contents
The page's "On this page" list: its headings as in-page links, with the section in view marked. It fills the docs layout's contents column on wide screens; below that, its compact bar names the section in view at the top of the page and opens the whole list.
import {
TableOfContents,
useActiveHeading,
} from '@fairgarden/design/navigation/table-of-contents'
Pass items as the page's headings, { id, title, level }, in order: level 2 at the top, levels 3 and 4 nested under the heading before them. Pass activeId to mark the section in view. The component draws and does nothing else, so the marked section can come from anywhere; useActiveHeading provides it from scrolling. Links are plain #id anchors, which scroll to the heading and write the hash, unless you pass renderLink.
On this page
On this page
A nav named by its caps heading, "On this page" unless you pass label, over an ordered list of type-caption links on 44 px rows. The entries hang on a hairline guide at the list's start edge, and nested levels step in on their own guide. The section in view takes aria-current="location", the 3 px start-edge bar over the guide and weight 700: the same current-item marks as the sidebar, the drawer and the section bar's Jump list. Every entry reserves its bold width, so marking a section never rewraps a line. Hover follows the list-link rule. Inside the compact bar's panel the heading is hidden, since the bar already says it, and it stays the landmark's name. In print the list is hidden; the document's own headings carry its structure.
The compact bar
The compact bar
With kind="bar" the contents shrink to one line for narrow screens: a field-like box holding "On this page" and the title of the section in view, truncated, with a chevron. Before scrollspy runs (and in the server HTML) it names the first section, and its height never changes, whichever section it names. Pressing it opens the whole list in a panel anchored under the bar, about a third of the viewport tall (at least 240 px, never more than the room below the bar), with the browser's own scrollbar kept clear of its rounded corners. The panel scrolls itself, never the page, so the section in view sits centered and marked, and focus lands on it. Following an entry goes to its heading (clear of a sticky bar, given a scroll-margin), closes the panel and moves focus to the heading; Escape or a press outside closes it and focus returns to the bar. The bar's ring, or its edge when pressed with a pointer, grows into the panel's frame as the panel opens and shrinks back as it closes, as the Select's does; pass morph={false}, or set --fgd-outline-morph: none, to turn that off, and under reduced motion the panel simply appears. The section in view is announced politely when it changes. With no items the component renders nothing. The docs layout draws the same toc element as this bar below 1440 px, pinned under its header, so you pass one contents element for both widths. In print the bar and the panel are hidden.
Scrollspy
Scrollspy
useActiveHeading(ids) returns the id of the section in view. An IntersectionObserver watches the headings against a band from the top of the view down to the activation line, a quarter of the way down unless you pass line (in px or %); each time a heading crosses the line, the last heading above it becomes current. At the top of the page the first heading is current, and scrolled to the end, the last. A pass after each scroll ends catches jumps that cross no line. Pass root when the content scrolls in a box of its own, as in this demo. The hook only reads positions, and the list reserves every entry's bold width, so scrollspy moves nothing. It returns undefined on the server, so the server HTML marks no section.
Before you sow
Rake the bed level and water it the evening before. Seed sown into dry soil sits there waiting; seed sown into damp soil starts the same week.
Choosing seed
The seed library in the tool shed lends packets by the spoonful. Take what you will sow this month, and write the variety in the ledger by the door.
Sowing
Sow in drills a finger deep for beans and peas, a knuckle deep for roots and greens. Cover, firm the soil with the back of the rake, and label the row.
Thinning
When the seedlings have two true leaves, thin them to a hand’s width. The thinnings of lettuce, beet and chard make a good first salad.
Saving seed for next year
Leave the best plant of each row to flower and set seed. Once the pods are dry and rattle, pick them on a dry afternoon and bring the seed back to the library.
API Reference
TableOfContents
The page’s contents [D202, D203].
kind="list" (default): a nav named by its caps heading (type-label)
over an ol of in-page links in type-caption, H2s at the top level and
H3 and H4 nested on the --role-hairline guide. The section in view
(activeId) takes aria-current="location", the --border-size-2–25
start-edge bar and weight 700; every entry reserves that weight’s box, so
marking a section never moves the list.
kind="bar": one field-box line, “On this page” and the section in view
(the first before scrollspy runs), truncated, with a chevron. It opens
the same list in an anchored panel about a third of the viewport tall,
scrolled inside so the section in view sits centered and marked, with
focus on it; following an entry goes to its heading and closes the
panel; Escape and an outside press close it and focus returns to the
bar. The bar’s ring or edge morphs into the panel’s frame [D204].
Presentational: pair it with useActiveHeading for scrollspy. The docs
layout shows the list in a sticky column from --xl-n-above and the bar,
pinned under the header, below. With no items it renders nothing. Hidden
in print.
| Prop | Type | Description |
|---|---|---|
label | | The visible heading (the bar’s label) and the landmark’s name. Default “On this page”. |
activeId | | The id of the section in view: its entry takes |
items | | The page’s headings in order, levels 2–4 (a level-3 nests under the level-2 before it). |
kind | |
|
morph | | With |
primary | | Override the scope’s primary. Never defaulted [D133]. |
renderLink | | Builds each anchor. Default: a plain |
secondary | | Override the scope’s secondary. Never defaulted. |
useActiveHeading
Scrollspy for a table of contents [D202]: the id of the heading whose
section is in view, for TableOfContents’ activeId. An
IntersectionObserver watches the headings against a band from the root’s
top edge down to line; each time one crosses the line the hook takes
the last heading above it (the first while none has passed, the last
once the root is scrolled to its end). The heading a link just landed on
(the URL’s hash) stays current while it sits above the line, so a short
section chosen from the contents is the one marked. A scrollend pass
re-reads the positions after jumps that cross no line.
It only reads layout and returns an id: nothing it drives changes size
(the list reserves each entry’s current weight), so it causes no layout
shift. It returns undefined on the server and before the first
observation, so the server HTML marks no section.
| Parameter | Type | Description |
|---|---|---|
ids | | |
options | |
string | undefinedAdditional types
tableOfContents
type tableOfContents = tableOfContentsTableOfContentsContext
Provided by a host of the contents, such as the docs layout.
type TableOfContentsContext = React.Context<TableOfContentsContextValue>TableOfContentsContextValue
What a host tells the contents inside it. The docs layout sets
kind: 'bar' where it pins the compact bar (below --xl-n-above), so
the one toc it is given draws as the column there and as the bar here.
type TableOfContentsContextValue = {
/** The kind to draw when the component names none. */
kind?: TableOfContentsKind;
/** Hides the list's visible heading; it stays the landmark's name. */
labelHidden?: boolean;
}TableOfContentsItem
One heading on the page.
type TableOfContentsItem = {
/** The heading's `id`: the entry links to `#id`. */
id: string;
/** The heading's text. */
title: string;
/** The heading level: 2, 3 or 4. */
level: TableOfContentsLevel;
}TableOfContentsKind
How the contents draw: the full list, or the compact bar that opens it.
type TableOfContentsKind = 'list' | 'bar'<'list' | 'bar' | null | undefined>TableOfContentsLevel
Heading levels the list shows: H2 at its top level, H3 and H4 nested under it.
type TableOfContentsLevel = 2 | 3 | 4TableOfContentsProps
Props for TableOfContents: nav props, the headings, the section in view, the kind and the color axes.
type TableOfContentsProps = {
/** The page's headings in order, levels 2–4 (a level-3 nests under the level-2 before it). */
items: TableOfContentsItem[];
/**
* The id of the section in view: its entry takes `aria-current="location"`,
* and the bar names it. Pass `useActiveHeading(ids)` for scrollspy.
* Omitted, no entry is marked and the bar names the first section.
*/
activeId?: string;
/** The visible heading (the bar's label) and the landmark's name. Default "On this page". */
label?: string;
/**
* `list` (default): the full list. `bar`: one line naming the section in
* view, which opens the list in an anchored panel. Omitted, a host's
* `TableOfContentsContext` may set it (the docs layout does).
*/
kind?: 'list' | 'bar' | null;
/**
* With `kind="bar"`: the outline morph, the bar's ring or edge growing
* into the panel's frame on open and back on close [D204]. Default
* `true`; it never runs under reduced motion, in forced colors or in
* print.
*/
morph?: boolean;
/** Builds each anchor. Default: a plain `<a href="#id">`, which scrolls and writes the hash. */
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;
}UseActiveHeadingOptions
Options for useActiveHeading.
type UseActiveHeadingOptions = {
/**
* The scroll container the headings move in. Default: the viewport. Pass
* a ref when the content scrolls inside its own box (a panel, a demo).
*/
root?: React.RefObject<Element | null>;
/**
* Where a heading becomes current: the activation line's distance below
* the root's top edge, in `px` (`'80px'`) or as a percentage of the
* root's height (`'25%'`). Default `'25%'`: a heading is current once
* its top has passed the top quarter of the view, and stays current
* until the next one passes.
*/
line?: string;
/** `false` pauses the hook (it then returns `undefined`). Default `true`. */
enabled?: boolean;
}Specification: DESIGN-SYSTEM.md §9.16 (table of contents) [D202, D203], in the docs layout of §11.15.