# Table of Contents

- Page: <https://kznlabs.github.io/labsUI/components/table-of-contents>

## Table of Contents (`table-of-contents`, component)

Links to a page's sections that follow the reader on a sliding marker, on the page or in a nested scroller. Short sections at the end still get a turn, and a clicked link stays current where it lands. Links work without JS.

- Install: `npx @kznlabs/labsui add table-of-contents`
- Docs: <https://kznlabs.github.io/labsUI/components/table-of-contents>
- Brings: list, scroll-spy, use-indicator, utils
- npm: clsx@^2.1.1, motion@14.7.1, tailwind-merge@^3.7.0

### API

- `<TableOfContents>` (+ <nav> props): Links to a page's sections that follow the reader. Works on the page or in a nested scroller, and
short sections at the end still get their turn. Give the root a `max-h-*` and it scrolls itself,
keeping the current link in view.
  - `items: TableOfContentsItem[]`: Sections in page order.
  - `hideLabel?: boolean` = `false`: Keeps `label` for screen readers only, e.g. inside a Collapsible that already says it.
  - `label?: string` = `"On this page"`: Title over the list and the navigation's accessible name.
  - `offset?: number` = `96`: Pixels below the top of the scroller where a heading becomes current. Keep it at or above the headings' `scroll-margin-top`.

- `type TableOfContentsItem = { id: string; label: string; level?: number; }`

### Usage

```tsx
<TableOfContents className="sticky top-24 max-h-[calc(100dvh-7rem)]" items={[{ id: "install", label: "Install" }, { id: "options", label: "Options", level: 3 }]} />
```

### Example: docs-page.tsx

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

const sections = [
  { id: "install", label: "Install" },
  { id: "options", label: "Options" },
  { id: "defaults", label: "Defaults", level: 3 },
  { id: "faq", label: "FAQ" },
];

export function DocsPage() {
  return (
    <div className="grid gap-10 lg:grid-cols-[minmax(0,1fr)_14rem]">
      <Prose as="article">
        <h2 id="install">Install</h2>
        <p>Long text…</p>
        <h2 id="options">Options</h2>
        <p>Long text…</p>
        <h3 id="defaults">Defaults</h3>
        <p>Long text…</p>
        <h2 id="faq">FAQ</h2>
        <p>Short text.</p>
      </Prose>

      <aside className="max-lg:hidden">
        <TableOfContents
          items={sections}
          className="sticky top-24 max-h-[calc(100dvh-7rem)]"
        />
      </aside>
    </div>
  );
}
```
