A table of contents navigation for the headings on the current page. Use it in a doc layout sidebar so readers can jump to any section.
Install
pnpm dlx shadcn@latest add https://sitex.full.dev/r/toc.jsonUsage
Sitex passes the extracted MDX headings to layouts via the headings prop, so a doc layout can map them straight into the menu:
import type { LayoutProps } from "@fulldotdev/sitex"
import {
Toc,
TocMenu,
TocMenuItem,
TocMenuLink,
TocTitle,
} from "@/components/ui/toc"
export default function DocLayout({ headings }: LayoutProps) {
return (
<Toc>
<TocTitle>On this page</TocTitle>
<TocMenu>
{headings?.map((heading) => (
<TocMenuItem key={heading.href}>
<TocMenuLink href={heading.href} depth={heading.depth}>
{heading.label}
</TocMenuLink>
</TocMenuItem>
))}
</TocMenu>
</Toc>
)
}Key parts:
Tocrenders the outernav,TocTitlethe heading (apby default, changeable viaas), andTocMenu/TocMenuItemtheul/listructure.TocMenuLinkindents bydepth(default2;3and deeper get increasing left padding), matching thedepthof each Sitex heading.- Pass
isActive(oractive) toTocMenuLinkto highlight the current section; it setsdata-activeandaria-current="location".