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
npx shadcn@latest add @gnostic/toc
pnpm dlx shadcn@latest add @gnostic/toc

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/toc/index.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
---
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
---
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
---
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
---
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
---
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>

Custom Title

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

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