TOC

Docs table of contents component.


Installation

This component is part of the Doc block. Install the full block to use it:

CLI

npx shadcn@latest add "https://ui.xaclabs.dev/r/doc.json"

Usage

import { TOC } from "@/registry/blocks/doc/components/doc-toc"
View component source
doc-toc.tsx
tsx
import { cn } from "@/lib/utils"

interface Heading {
  depth: number
  slug: string
  text: string
}

interface DocTocProps {
  headings: Heading[]
}

export function DocToc({ headings }: DocTocProps) {
  const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3)

  if (tocHeadings.length === 0) return null

  return (
    <div className="sticky top-20">
      <p className="text-sm font-medium mb-4">On this page</p>
      <nav className="flex flex-col gap-2">
        {tocHeadings.map((h) => (
          <a
            key={h.slug}
            href={`#${h.slug}`}
            className={cn(
              "text-sm text-muted-foreground hover:text-foreground transition-colors",
              h.depth === 3 && "pl-4",
            )}
          >
            {h.text}
          </a>
        ))}
      </nav>
    </div>
  )
}