Directory
- Welcome
Table of Contents
A dynamic, interactive table of contents component that automatically generates navigation from your page headings. Perfect for long-form content, documentation, and articles that need clear structure and easy navigation.
Key Features
-
Auto-highlighting: Current section is highlighted during scroll
-
Smooth Scrolling: Click navigation with smooth transitions
-
Responsive Design: Adapts to different screen sizes
-
Accessibility: Semantic markup with ARIA support
Quick Start
---const headings = await getHeadings(); // Get from your content---
<TOC {headings} depth={3} title="On This Page" />
Features & Options
Heading Depth Control
Choose how deep your table of contents should go (1-6 levels):
Three Levels Deep
---<TOC headings={[ { depth: 1, slug: '#example-1', text: 'H1 Heading' }, { depth: 2, slug: '#example-2', text: 'H2 Heading' }, { depth: 3, slug: '#example-3', text: 'H3 Heading' }, ]} depth={3} title="Three Levels Deep" />
Progress Bar
Track reading progress with an optional progress bar:
Progress Bar Example
The progress bar can track either:
- Overall page scroll progress
- Specific content section progress using the
contentSelector
prop
Content Section Tracking
Specify a content section to track:
<TOC headings={headings} contentSelector=".article-content" progressBar={true}/>
This is useful when you want the TOC to track a specific content area rather than the entire page.
Alignment Options
Position your TOC to match your layout:
Left Alignment
Right Alignment
TOC Component Props API
Prop | Type | Default | Description |
---|---|---|---|
headings | MarkdownHeading[] | [] | Array of heading objects from your content |
depth | number | 3 | Maximum heading depth to display (1-6) |
showBreak | boolean | false | Show separators between different heading depth |
title | string | Table of Contents | Custom title for the TOC |
align | left | right | left | Horizontal alignment of the TOC |
variant | default | card | default | Style variant of the TOC |
indent | boolean | true | Indent the TOC items by the heading depth |
progressBar | boolean | false | Show a progress bar indicating reading progress |
contentSelector | string | undefined | CSS selector for the content section to track |
Using with Content Collections
---export async function getStaticPaths() { const posts = (await getCollection('docs')).map((post) => { return { params: { slug: post.slug }, props: { post }, }; }); return posts;}
const { post } = Astro.props;
const { Content, headings } = await post.render();
---
<TOC headings={headings} depth={3} title="On This Page" />
Common Use Cases
Limited Time Launch Sale
Ready to improve your content navigation? Get our AstroJS starter template now and implement the Table of Contents component in minutes.
GET 60% OFF!