TableOfContents
Sticky 'On this page' navigation with real anchor links, nested levels, aria-current, and a useScrollSpy hook for the active heading.
Design intent
Long-form pages drift into bespoke TOC implementations. TableOfContents fixes the nested list, native anchor navigation, active styling and landmark name; useScrollSpy supplies the active id from real scroll position.
npx @slipstream-ui/cli@latest add table-of-contentsInstallation
Add the package or copy the component source. Every heading in this sample has a matching id, so the links in the table of contents scroll to it and update the URL hash.
pnpm
pnpm add @slipstream-ui/core resolves the workspace package and its peer dependencies. The link above is a level-three item, so it is indented in the list.
Configuration
Wrap your app in the theme provider, or apply the CSS variable block to :root. The TOC highlights the section you are reading through IntersectionObserver, not click state alone.
Tokens
Semantic tokens keep light and dark mode consistent: background, foreground, muted, border, and the status inks. Override them with createTheme rather than editing component classes.
Next steps
Add scroll-mt-24 to headings when a sticky site header overlaps the target. The TableOfContents component renders real anchors, so deep links, middle-click, and open-in-new-tab all keep working.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| headings | TocHeading[] | required | Headings: { id, text, level: 2 | 3 | 4 }. |
| activeId | string | — | Controlled active heading (pair with useScrollSpy). |
| label | string | 'On this page' | Accessible name of the nav landmark. |
| ariaLabel | string | — | Landmark name when it differs from the visible label. |
| labelClassName | string | — | Extra classes for the visible label. |
| sticky | boolean | true | Stick while the article scrolls. |
| onNavigate | (id: string) => void | — | Called when a link is activated; the browser still handles the jump. |
| useScrollSpy(ids) | hook | — | Active heading id from scroll position and the URL hash. |
Anti-patterns
validate_usage is the MCP tool that rejects these mistakes: the same list agents see over MCP and in the canonical markdown.
Don't hardcode the active id
Compute it from scroll position so the highlight follows the reader.
tsx// Avoid<TableOfContents headings={h} activeId="intro" />Don't replace the anchors with buttons
Buttons only run a highlight callback and break deep links, middle-click, and open-in-new-tab. The component renders real anchors; keep them.
tsx// Avoid<button onClick={() => setActiveId(h.id)}>{h.text}</button>// Prefer<TableOfContents headings={h} activeId={activeId} onNavigate={setActiveId} />
Accessibility
- nav landmark with a label; the active link carries aria-current="location".
- Links are native anchors, so Enter, middle-click, and deep links work.
- Nested levels are indented; DOM order matches document order.
- Add scroll-mt-* to target headings when a sticky header overlaps them.
Content guidelines
Heading text
- Match the on-page heading text exactly
Good examples
- Installation
Bad examples
- Section 1
Composition
Compose with Prose: Pairs with article bodies
<TableOfContents headings={headings} activeId={active} />
Related components: Prose, Breadcrumb, List
Dependencies
What the registry installs alongside this component. Package imports already include these.
npm packages
- class-variance-authority
- clsx
- tailwind-merge
Browser support
| Browser | Tested versions |
|---|---|
| Chrome | 111+ (Mar 2023) |
| Edge | 111+ (Mar 2023) |
| Firefox | 121+ (Dec 2023) |
| Safari | 17.2+: macOS & iOS (Dec 2023) |
Slipstream components rely only on CSS custom properties, Flexbox/Grid, and ARIA, the everyday baseline of the last two years of evergreen releases. Anything newer is progressive enhancement: unsupported browsers keep full functionality with static behavior instead of animations.