Skip to content

Table Of Contents

Sticky React table of contents that reads h2 and h3 headings, marks the section in view with a sliding accent bar and smooth scrolls to a heading on click.

src/registry/ui/table-of-contents.tsx/r/table-of-contents.json

Installation

bash
npx shadcn@latest add @minidev/table-of-contents

MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/table-of-contents.json

Usage

tsx
import { TableOfContents } from "@/components/ui/table-of-contents"

export function Example() {
  return (
    <TableOfContents />
  )
}

API reference

TableOfContents props
PropTypeDefault
itemsHeadings to list. When absent, h2 and h3 elements are read from `selector` after mount.TocItem[]-
selectorCSS selector of the container to read headings from when `items` is absent. Headings without an id get one.string-
titleSmall label above the list. Empty hides it.string"On this page"
offsetPixels between the top of the scroll area and a heading for it to count as reached.number24
onActiveChangeCalled when the active heading changes.(id: string) => void-
stickyKeep the list in view while the page scrolls (position sticky).booleantrue
classNamestring-

Keyboard interactions

Keyboard interactions
KeyAction
TaborShiftTabMoves between the entries.
EnterScrolls to the heading and moves focus to it.

Accessibility

  • A <nav> named by title ("On this page"), or "Table of contents" when title is empty. The current entry has aria-current="location".
  • After a click the heading gets tabindex="-1" and focus, so the next Tab continues from that section, not from the list.
  • With reduced motion the scroll jumps and the marker moves without a spring. The marker is aria-hidden.

Frequently asked questions

How do I use it with MDX or a CMS?

Pass selector for the article container, for example selector="#doc". After mount it reads the h2 and h3 elements and gives any heading without an id one made from its text. Headings added later are not picked up; pass items if your content loads afterwards.

My sticky header covers the heading after a click. How do I fix it?

Set offset to the header height plus a little space, for example offset={80}. It shifts both the scroll target and the point where a heading counts as reached. The default is 24.

Why does it show an article when I render it with no props?

With neither items nor selector it renders a demo article in a scroll box, so the spy can be seen working. Pass one of them in your app.

Dependencies

npm

motion

Built from

No other MiniDev files

Examples

From a selector

Point selector at the container and the h2 and h3 headings are read after mount; headings without an id get one from their text.

Explicit items with callback

Pass items when you already know the headings, and use onActiveChange to mirror the reading position elsewhere, for example in the URL.

All navigation components