Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

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/toc

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".

Overview

Start with a small page. Choose the content and controls that help people complete their task, then add more sections as the page grows.

Installation

Add the components your page needs. Keep the source in your project so you can adjust it as your requirements change.

Configuration

Set the options for your site. Use the same choices across related pages so each control has a consistent purpose and appearance.

Theme

Choose a preset for your colors, fonts, and spacing. Check the content in both light and dark mode before you publish.

Next steps

Read the page from start to finish. Check the links with a keyboard and confirm that each heading is visible when you select it from the contents list.

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.