Skip to Content
docscn

Table of contents

The table of contents that follows the headings in view.

Installation

npx shadcn@latest add @docscn/toc

Usage

DocsPage shows the TOC beside the page on wide screens and as a popover on smaller ones. To use it elsewhere:

import { TOCProvider, TOCScrollArea } from "@/components/docs/components/toc";
import { TOCItem, TOCItems } from "@/components/docs/components/toc/default";

<TOCProvider toc={toc}>
  <TOCScrollArea>
    <TOCItems>
      {toc.map((item) => (
        <TOCItem key={item.url} item={item} />
      ))}
    </TOCItems>
  </TOCScrollArea>
</TOCProvider>;

The active headings are highlighted with a thumb on a line that follows the heading levels. useActiveAnchor, useActiveAnchors, useItems and useTOCItems are re-exported from components/toc.

Styles

Pick a style for DocsPage's TOC and TOC popover with style:

<DocsPage
  toc={page.data.toc}
  tableOfContent={{ style: "clerk" }}
  tableOfContentPopover={{ style: "clerk" }}
>
  {/* ... */}
</DocsPage>
StyleModuleLooks like
normal (default)components/toc/defaultA line that curves with the heading levels, and a thumb on the active headings.
clerkcomponents/toc/clerkA line that steps with the heading levels, highlighted beside the active headings.
blockcomponents/toc/blockA highlight behind the active headings.

Each module exports TOCItems, TOCItem and TOCEmpty, so you can swap them in the example above.