Skip to content

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.json

Usage

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

PropTypeDefaultDescription
selectorstring"h2, h3"Which headings to collect. Headings without an id get one from their text.
headingsTocHeading[]—List the headings yourself instead, for server-rendered pages that already know them.
containerRefObject<HTMLElement | null>—The element that scrolls, when it isn't the window. Headings are looked up inside it.
offsetnumber96Distance 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.
labelstring"On this page"Heading above the list; also the nav's accessible name.
emptyLabelstring"No headings"Shown when there are no headings to list.
classNamestring—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, …
};