---
title: Table of contents for Astro
description: "Add a table of contents to Astro articles and docs. Explore heading links, composition examples, and source installation."
seo:
    title: "Astro Table of contents examples and source"
related:
    - /components/breadcrumb
    - /components/scroll-spy
    - /components/scroll-area
    - /docs/theming#text-and-headings
sidebar:
    label: "Table of contents"
---

## Usage

```ts
import { Toc, TocMenu, TocMenuItem, TocMenuLink, TocTitle } from "@/components/ui/toc"
```

```astro
---
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

```astro
---
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="#examples">Examples</TocMenuLink>
    </TocMenuItem>
  </TocMenu>
</Toc>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/toc
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/toc/index.ts

```ts
export { default as Toc } from "./toc.astro"
export { default as TocMenu } from "./toc-menu.astro"
export { default as TocMenuItem } from "./toc-menu-item.astro"
export { default as TocMenuLink } from "./toc-menu-link.astro"
export { default as TocTitle } from "./toc-title.astro"
```

#### src/components/ui/toc/toc-menu-item.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"li">

const { class: className, ...props } = Astro.props
---

<li data-slot="toc-menu-item" class={cn("relative", className)} {...props}>
  <slot />
</li>
```

#### src/components/ui/toc/toc-menu-link.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"a"> & {
  depth?: number
  isActive?: boolean
  active?: boolean
}

const {
  class: className,
  depth = 2,
  isActive,
  active = false,
  ...props
} = Astro.props

const resolvedActive = isActive ?? active
---

<a
  data-slot="toc-menu-link"
  data-depth={depth}
  data-active={resolvedActive ? "true" : undefined}
  aria-current={resolvedActive ? "location" : undefined}
  class={cn(
    "text-muted-foreground hover:text-foreground data-[active=true]:text-foreground block text-sm leading-6 transition-colors",
    depth === 3 && "pl-4",
    depth >= 4 && "pl-8",
    className,
  )}
  {...props}
>
  <slot />
</a>
```

#### src/components/ui/toc/toc-menu.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"ul">

const { class: className, ...props } = Astro.props
---

<ul
  data-slot="toc-menu"
  class={cn("flex flex-col gap-1", className)}
  {...props}
>
  <slot />
</ul>
```

#### src/components/ui/toc/toc-title.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"p"> &
  HTMLAttributes<"h2"> &
  HTMLAttributes<"div"> & {
    as?: "p" | "h2" | "div"
  }

const { as: Tag = "p", class: className, ...props } = Astro.props
---

<Tag data-slot="toc-title" class={cn("text-sm font-medium tracking-tight", className)} {...props}>
  <slot />
</Tag>
```

#### src/components/ui/toc/toc.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"nav">

const { class: className, ...props } = Astro.props
---

<nav data-slot="toc" class={cn("flex flex-col gap-3", className)} {...props}>
  <slot />
</nav>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


### Custom Title

You can choose any wrapper label and compose the structure yourself.

```astro
---
import {
  Toc,
  TocMenu,
  TocMenuItem,
  TocMenuLink,
  TocTitle,
} from "@/components/ui/toc"
---

<Toc aria-label="Contents">
  <TocTitle>Contents</TocTitle>
  <TocMenu>
    <TocMenuItem>
      <TocMenuLink href="#installation">Installation</TocMenuLink>
    </TocMenuItem>
    <TocMenuItem>
      <TocMenuLink href="#usage">Usage</TocMenuLink>
    </TocMenuItem>
    <TocMenuItem>
      <TocMenuLink href="#custom-title" depth={3}>
        Custom Title
      </TocMenuLink>
    </TocMenuItem>
  </TocMenu>
</Toc>
```

### With Scroll Spy

Use [Scroll Spy](/ui/components/scroll-spy#table-of-contents) 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](/ui/components/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"`.

```astro
---
import { ScrollArea } from "@/components/ui/scroll-area"
import {
  ScrollSpy,
  ScrollSpyItem,
  ScrollSpyTrack,
} from "@/components/ui/scroll-spy"
import { Toc, TocMenu, TocMenuItem, TocTitle } from "@/components/ui/toc"

const items = [
  {
    id: "toc-reader-overview",
    label: "Overview",
    depth: 2,
    text: "Start with a small page. Choose the content and controls that help people complete their task, then add more sections as the page grows.",
  },
  {
    id: "toc-reader-installation",
    label: "Installation",
    depth: 2,
    text: "Add the components your page needs. Keep the source in your project so you can adjust it as your requirements change.",
  },
  {
    id: "toc-reader-configuration",
    label: "Configuration",
    depth: 3,
    text: "Set the options for your site. Use the same choices across related pages so each control has a consistent purpose and appearance.",
  },
  {
    id: "toc-reader-theme",
    label: "Theme",
    depth: 3,
    text: "Choose a preset for your colors, fonts, and spacing. Check the content in both light and dark mode before you publish.",
  },
  {
    id: "toc-reader-next-steps",
    label: "Next steps",
    depth: 2,
    text: "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.",
  },
]
---

<div class="grid w-full gap-6 sm:grid-cols-[11rem_minmax(0,1fr)]">
  <Toc aria-label="Guide contents">
    <TocTitle>On this page</TocTitle>
    <ScrollSpy variant="toc" scrollTarget="#toc-reading-viewport">
      <ScrollSpyTrack variant="toc">
        <TocMenu>
          {items.map(({ id, label, depth }) => (
            <TocMenuItem>
              <ScrollSpyItem variant="toc" href={`#${id}`} {depth}>
                {label}
              </ScrollSpyItem>
            </TocMenuItem>
          ))}
        </TocMenu>
      </ScrollSpyTrack>
    </ScrollSpy>
  </Toc>
  <ScrollArea
    class="h-80 min-w-0 rounded-xl border"
    aria-label="Getting started guide"
    viewportProps={{ id: "toc-reading-viewport" }}
    overscrollContain
  >
    <article class="flex flex-col gap-6 p-6">
      {items.map(({ id, label, depth, text }) => {
        const Heading = depth === 3 ? "h3" : "h2"
        return (
          <section class="flex min-h-52 flex-col gap-3">
            <Heading {id} class="scroll-mt-6 text-base font-medium">
              {label}
            </Heading>
            <p class="text-sm leading-relaxed text-muted-foreground">{text}</p>
          </section>
        )
      })}
    </article>
  </ScrollArea>
</div>
```

For a page TOC, use `scrollTarget="html"` and place `sticky top-24` on `Toc`.
See the [page-scroll example](/ui/components/scroll-spy#table-of-contents).
Both examples build the links and heading IDs from one content array.

## API Reference

Read the [installed source](/ui/r/toc.json) 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.
