Table of contents
An outline of the headings of a long page. It marks the section being read and fills a thin track down to it, so a reader can see how far they are and jump elsewhere. For the plain share of a page that has been scrolled, use the scroll progress bar; for moving between pages, use a sidebar.
Install
npx shadcn@latest add @thrum-pro/table-of-contentsThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Data shape
type TableOfContentsItem = {
id: string; // the id of the heading element
title: string;
level?: number; // 2 for an h2, 3 for an h3: lower levels are indented
};const HEADINGS = [
{ id: "overview", title: "Overview", level: 2 },
{ id: "thresholds", title: "Thresholds", level: 2 },
{ id: "static", title: "Static", level: 3 },
{ id: "channels", title: "Channels", level: 2 },
];Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly TableOfContentsItem[] | Required | The headings, in the order they appear in the page. Each id must be the id of an element in the page. With no items the component renders nothing. |
target | RefObject<HTMLElement | null> | – | A ref to the scroll container that holds the headings. Leave it out when the page itself scrolls. |
heading | string | – | Optional visible heading drawn above the outline links, such as On this page. |
className | string | – | Extra classes for the nav element, merged last. Set its width and position here. |
...props | ComponentProps<"nav"> | – | Every other nav attribute. aria-label is On this page unless you set another. |
Motion and sound
Motion
- A link's text colour and its stretch of the track change over 150 ms when another section becomes current.
- Nothing moves or changes size: both weights of every title are laid out from the start, so marking a section never re-wraps the list.
- Reduced motion: colours change at once.
Sound
Silent. This component makes no sound.
Accessibility
- It is a nav landmark named On this page that holds a list of plain links to the headings, so it works without JavaScript and with the browser's back button.
- The link of the section being read carries aria-current="location".
- The current link is heavier and darker, and the track beside the list is filled from the top down to it: the mark does not rest on colour alone.
- A section becomes current when its heading passes a line a quarter of the way down the scroll area, and stays current until the next heading does. A last section too short to bring its heading that far never becomes current: leave room under it.
- One intersection observer watches the headings. Nothing listens to the scroll itself.
- Long titles wrap. Levels are indented by 12 pixels each, up to three steps.
| Keys | Action |
|---|---|
| Tab | Move through the links. |
| Enter | Jump to the focused link's heading. The browser scrolls to it, and that section becomes current. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/table-of-contents.tsx- Packages
- None
- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.