Table of contents
The table of contents that follows the headings in view.
Installation
npx shadcn@latest add @docscn/tocUsage
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>| Style | Module | Looks like |
|---|---|---|
normal (default) | components/toc/default | A line that curves with the heading levels, and a thumb on the active headings. |
clerk | components/toc/clerk | A line that steps with the heading levels, highlighted beside the active headings. |
block | components/toc/block | A highlight behind the active headings. |
Each module exports TOCItems, TOCItem and TOCEmpty, so you can swap them in the example above.