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

## API Reference

Read the [installed source](/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. The component does not discover headings from arbitrary content. 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.
