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

- Kind: Component (Navigation)
- Docs: https://ui.minidev.pro/docs/table-of-contents
- Source (registry JSON with the full file): https://ui.minidev.pro/r/table-of-contents.json
- License: MIT. One file, yours to edit after install.

## Installation

### shadcn CLI (copies the source into your project)

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

Writes `components/ui/table-of-contents.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/table-of-contents.json`.

### npm package

```bash
npm i minidev-ui-kit motion
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

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

From the npm package:

```tsx
import { TableOfContents } from "minidev-ui-kit/ui/table-of-contents"
```

## Usage

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

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

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `items` | `TocItem[]` | - | no | Headings to list. When absent, h2 and h3 elements are read from `selector` after mount. |
| `selector` | `string` | - | no | CSS selector of the container to read headings from when `items` is absent. Headings without an id get one. |
| `title` | `string` | `"On this page"` | no | Small label above the list. Empty hides it. |
| `offset` | `number` | `24` | no | Pixels between the top of the scroll area and a heading for it to count as reached. |
| `onActiveChange` | `(id: string) => void` | - | no | Called when the active heading changes. |
| `sticky` | `boolean` | `true` | no | Keep the list in view while the page scrolls (position sticky). |
| `className` | `string` | - | no |  |

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

```tsx
<div className="flex w-full max-w-3xl flex-col gap-4 sm:flex-row sm:gap-6">
  <article id="lumen-release-notes" className="h-80 min-w-0 overflow-y-auto sm:flex-1 rounded-xl border border-border bg-surface px-5 py-4 shadow-raised">
    <p className="font-mono text-[10px] font-medium tracking-[0.08em] text-fg-subtle uppercase">Changelog</p>
    <p className="mt-1 text-xl font-medium text-fg">Lumen 2.4</p>
    <h2 className="mt-7 text-lg font-medium text-fg">Scheduled payouts</h2>
    <p className="mt-2 text-[0.8125rem] leading-[1.65] text-fg-muted">Pick daily, weekly or monthly and Lumen batches everything that cleared since the last run. A preview shows the next batch before it goes out.</p>
    <h3 className="mt-5 text-base font-medium text-fg">Cut-off times</h3>
    <p className="mt-2 text-[0.8125rem] leading-[1.65] text-fg-muted">Batches close at 16:00 in the workspace time zone. Payments that settle later roll into the next run, so totals never change after you have seen them.</p>
    <h2 className="mt-7 text-lg font-medium text-fg">Invoice reminders</h2>
    <p className="mt-2 text-[0.8125rem] leading-[1.65] text-fg-muted">Reminders now respect the customer’s time zone. Northwind’s finance team gets theirs at 9:00 Seattle time instead of the middle of the night.</p>
    <h2 className="mt-7 text-lg font-medium text-fg">API changes</h2>
    <p className="mt-2 text-[0.8125rem] leading-[1.65] text-fg-muted">GET /v1/payouts returns the schedule that produced each payout. The legacy payout.created webhook is deprecated and stops firing on 1 December.</p>
    <h2 className="mt-7 text-lg font-medium text-fg">Fixes</h2>
    <p className="mt-2 text-[0.8125rem] leading-[1.65] text-fg-muted">Rounding on multi-currency refunds is corrected and exports above 50,000 rows no longer time out.</p>
  </article>
  <div className="shrink-0 sm:w-44 sm:pt-4">
    <TableOfContents selector="#lumen-release-notes" title="Release notes" sticky={false} />
  </div>
</div>
```

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

```tsx
const GUIDE: (TocItem & { body: string })[] = [
  { id: "guide-install", level: 2, title: "Install", body: "Add the package with your package manager, then import the stylesheet once in your root layout. Nothing else is global." },
  { id: "guide-tokens", level: 2, title: "Tokens", body: "Every colour, radius and shadow is a CSS variable. Override them on :root to rebrand the whole kit without touching a component." },
  { id: "guide-dark-mode", level: 3, title: "Dark mode", body: "Put the dark class on html and the tokens swap. Components never check the theme themselves, so there is nothing to wire up." },
  { id: "guide-first-component", level: 2, title: "First component", body: "Copy Button into your app and render it with no props. If it looks right, the tokens are loaded and everything else will follow." },
  { id: "guide-deploy", level: 2, title: "Deploy", body: "Lumen ships behind a CDN with long cache headers. Fonts and tokens are small enough to inline, so the first paint is already styled." },
]

function ItemsDemo() {
  const [active, setActive] = React.useState("guide-install")
  return (
    <div className="flex w-full max-w-3xl flex-col gap-4 sm:flex-row sm:gap-6">
      <div className="h-80 min-w-0 overflow-y-auto sm:flex-1 rounded-xl border border-border bg-surface px-5 py-4 shadow-raised">
        <p className="text-xl font-medium text-fg">Setting up Lumen</p>
        {GUIDE.map((s) => {
          const H = s.level === 3 ? "h3" : "h2"
          return (
            <section key={s.id}>
              <H id={s.id} className={s.level === 3 ? "mt-5 text-base font-medium text-fg" : "mt-7 text-lg font-medium text-fg"}>{s.title}</H>
              <p className="mt-2 text-[0.8125rem] leading-[1.65] text-fg-muted">{s.body}</p>
            </section>
          )
        })}
      </div>
      <div className="shrink-0 sm:w-44 sm:pt-4">
        <TableOfContents
          items={GUIDE.map(({ id, title, level }) => ({ id, title, level }))}
          title="Setup guide"
          offset={16}
          sticky={false}
          onActiveChange={setActive}
        />
        <p className="mt-3 pl-3 text-xs text-fg-muted">Reading: {GUIDE.find((s) => s.id === active)?.title}</p>
      </div>
    </div>
  )
}
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Tab` or `Shift+Tab` | Moves between the entries. |
| `Enter` | Scrolls 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`.

## FAQ

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

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `motion`

## Related

- [Toc](https://ui.minidev.pro/docs/toc/md): Table of contents nav with anchor links indented by heading level and a highlighted active item. Pair it with scroll tracking for docs and long articles.
- [Scroll Progress Rail](https://ui.minidev.pro/docs/scroll-progress-rail/md): A React reading progress bar that fills with page scroll on a spring, with section labels such as intro, product, proof and pricing shown beneath it.
- [Back Link](https://ui.minidev.pro/docs/back-link/md): Small back navigation link with a left arrow icon, muted text that darkens on hover and a visible focus ring. Defaults to the label Back and accepts any href.
- [Breadcrumb Ellipsis](https://ui.minidev.pro/docs/breadcrumb-ellipsis/md): Ellipsis marker for collapsing the middle of long breadcrumb trails. Decorative and hidden from assistive tech, sized to sit between breadcrumb separators.
- [Breadcrumb](https://ui.minidev.pro/docs/breadcrumb/md): Breadcrumb navigation from an items array, with chevron separators, linked ancestors and the current page marked with aria-current. Built with React.
- [Collapsible Sidebar](https://ui.minidev.pro/docs/collapsible-sidebar/md): Sidebar with a toggle button on its edge that collapses it to a narrow rail and expands it again. The button label updates for screen readers.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
