Back to components
ElementNavigationFree

Table of Contents React component

An 'On this page' table of contents for articles and docs: it tracks the section in view, slides a marker along its rail (filling it like reading progress), indents subsections, and smooth-scrolls to a heading on click. Collects headings automatically.

About the Table of Contents component

Table of Contents is a vertical list of a page's headings for a sidebar, with the section you are reading highlighted as you scroll. A short bar slides along the left rail to the current entry, and the rail above it fills like reading progress. Clicking an entry scrolls smoothly to its heading, and subsections are indented under their sections.

Pass items with each heading's id, label and level, or pass contentRef and it collects every h2, h3 and h4 that has an id, updating if the content changes. offset sets where a heading becomes current and where a clicked one lands, which should cover any sticky header. title sets the label above the list (On this page by default), showProgress toggles the filling rail, updateHash writes the section's #id to the address bar, and scrollRef follows a scrolling panel instead of the page.

When to use it

Use Table of Contents beside long pages people scan and jump around in: documentation, guides, legal pages, long blog posts. It shows where the reader is and gets them anywhere in one click.

For a short page with a few top-level sections laid out horizontally (a product page's Overview, Specs, Reviews), use Scrollspy Anchor Links instead.

Installation

Add the Table of Contents component to your project with the shadcn CLI. It lands in components/table-of-contents.tsx as plain React + Tailwind CSS source you own and can edit.

Install
Copied!
npx shadcn@latest add @reactframe/table-of-contents

Or copy the source by hand and install its dependencies:

Install
Copied!
npm install motion clsx tailwind-merge

More Navigation components

Scrollspy Anchor Links
Free
Mobile Tab Bar
Free
Language Switcher
Free
Mega Menu
Free
Side Tab Menu
Free
Dropdown Menu
Free