Table of Contents
A table of contents that lights up every heading in view: a stepped rail glows beside them and each dot pulses as its section arrives. On phones it folds into a progress ring.
3 variantsNew
Preview
Installation
Copies the component into components/ui and installs its dependencies and theme tokens.
pnpm dlx shadcn@latest https://floret.shippedui.com/r/table-of-contents.jsonUsage
Pass your own data and wire the callbacks to your app.
import { TableOfContents } from "@/components/ui/table-of-contents";
// Reads every h2 and h3 inside <article>, adding ids where they're missing.
export function DocsLayout({ children }: { children: React.ReactNode }) {
return (
<div className="flex gap-12">
<article className="min-w-0 flex-1">{children}</article>
<aside className="sticky top-24 hidden w-56 self-start md:block">
<TableOfContents selector="article h2, article h3" />
</aside>
</div>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| selector | string | "h2, h3" | Which headings to collect. Headings without an id get one from their text. |
| headings | TocHeading[] | — | List the headings yourself instead, for server-rendered pages that already know them. |
| container | RefObject<HTMLElement | null> | — | The element that scrolls, when it isn't the window. Headings are looked up inside it. |
| offset | number | 96 | Distance in px from the top of the scroller where a heading counts as current. Match your sticky header. |
| collapse | "auto" | "always" | "never" | "auto" | auto folds into a progress button below 768px; always and never force one or the other. |
| label | string | "On this page" | Heading above the list; also the nav's accessible name. |
| emptyLabel | string | "No headings" | Shown when there are no headings to list. |
| className | string | — | Classes for the nav, or the compact wrapper. |
export type TocHeading = {
id: string;
title: string;
level: number; // 2 for an h2, 3 for an h3, …
};