Table of contents for Astro
Add a table of contents to Astro articles and docs. Explore heading links, composition examples, and source installation.
Usage
import { Toc, TocMenu, TocMenuItem, TocMenuLink, TocTitle } from "@/components/ui/toc"
---
import {
Toc,
TocMenu,
TocMenuItem,
TocMenuLink,
TocTitle,
} from "@/components/ui/toc"
---
<Toc aria-label="On This Page">
<TocTitle>On This Page</TocTitle>
<TocMenu>
<TocMenuItem>
<TocMenuLink href="#installation">Installation</TocMenuLink>
</TocMenuItem>
<TocMenuItem>
<TocMenuLink href="#usage">Usage</TocMenuLink>
</TocMenuItem>
<TocMenuItem>
<TocMenuLink href="#basic" depth={3}>Basic</TocMenuLink>
</TocMenuItem>
</TocMenu>
</Toc>
Examples
Installation
bunx --bun shadcn@latest add @gnostic/tocnpx shadcn@latest add @gnostic/tocpnpm dlx shadcn@latest add @gnostic/tocCopy the files
src/components/ui/toc/index.tssrc/components/ui/toc/toc-menu-item.astrosrc/components/ui/toc/toc-menu-link.astrosrc/components/ui/toc/toc-menu.astrosrc/components/ui/toc/toc-title.astrosrc/components/ui/toc/toc.astro
Custom Title
You can choose any wrapper label and compose the structure yourself.
With Scroll Spy
Use Scroll Spy to update the contents
as the reader scrolls. Toc supplies the navigation, title, and list structure.
ScrollSpy measures the headings and draws the path beside their links.
| Part | Role |
|---|---|
Toc, TocTitle, TocMenu, TocMenuItem |
Render the navigation, title, ul, and li elements. |
TocMenuLink |
Renders a native link. Its isActive or active prop sets active state when Astro renders the page. |
ScrollSpyItem variant="toc" |
Renders a link that Scroll Spy tracks. Its depth and padding match the path. |
ScrollSpyTrack variant="toc" |
Draws the path and the highlight for the visible heading links. |
In a tracked list, use ScrollSpyItem in each TocMenuItem. Both
TocMenuLink and ScrollSpyItem render an anchor, so use one link part per item.
Keep ScrollSpy as its default div inside the Toc navigation.
This example uses Scroll Area for the reading panel.
Scroll inside the panel or select a heading. Several visible headings can share
the highlight; the first visible link gets aria-current="location".
For a page TOC, use scrollTarget="html" and place sticky top-24 on Toc.
See the page-scroll example.
Both examples build the links and heading IDs from one content array.
API Reference
Read the installed source for the complete prop types.
When to use Toc
Build Toc links from the headings the page actually renders. Give each target a stable ID and use concise link text. Keep nesting aligned with the heading hierarchy.
Keyboard and behavior
Links use native fragment navigation. Toc does not discover headings or track
scroll position. TocMenuLink keeps the active state supplied at render time.
Use Scroll Spy for live tracking. Neither component creates links from arbitrary
content; supply the heading IDs and links. Check that sticky page headers do not
hide the target heading after navigation.
Theme and layout
Set a readable width for long section names and avoid excessive nesting. Shared text and border colors keep the contents list distinct without competing with the article.