Auto Table of Contents
This component automatically scans your page content for heading elements and builds a dynamic table of contents without requiring manual input.
Demo
See the Auto Table of Contents component in action.
Installation
pnpm dlx shadcn@latest add https://shadcn-ui-variants.vercel.app/r/auto-toc.json
Features
The Auto-Generating Table of Contents component includes several powerful features:
- Automatically detects headings in your content
- Creates a hierarchical navigation structure
- Highlights the currently active section
- Provides smooth scrolling to sections
- Fully responsive design with Tailwind CSS
- Accessible with proper ARIA attributes
Usage
Using this component in your React application is straightforward:
Basic Implementation
auto-toc-basic.tsx
1import { AutoTableOfContents } from "@/components/ui/auto-toc"
2
3export default function AutoTOCBasic() {
4 return (
5 <div className="flex">
6 <AutoTableOfContents />
7 <main>
8 <h1 id="heading-1">Heading 1</h1>
9 {/* Your content here */}
10 </main>
11 </div>
12 )
13}
Accessibility
This component follows accessibility best practices:
- Uses semantic HTML elements
- Includes proper ARIA attributes
- Supports keyboard navigation
- Maintains focus management
Browser Support
This component uses the Intersection Observer API, which is supported in all modern browsers. For older browsers, you may need to include a polyfill.
AutoToc Props
Prop | Type | Default |
---|---|---|
containerSelector | string | "main" |
title | string | "Table of Contents" |
maxDepth | number | 3 |
minHeadings | number | 2 |
className | string | — |
autoGenerateIds | boolean | false |
offset | number | 100 |