Documentation
Table of Contents
Automatically generates a table of contents from heading elements on the page. Uses IntersectionObserver to track scroll position and highlight the currently visible section.
Perfect for long-form documentation where readers need to quickly navigate between sections and track their progress.
Installation
shellUsage
tsxProps
Examples
Include Multiple Heading Levels
Show both h2 and h3 headings:
tsxCustom Title
Change the title displayed above the TOC:
tsxCustom Container
Wrap the TOC in custom layout:
tsxAll Heading Levels
Include h2 through h4:
tsxWith DocsPage
Standard integration with DocsPage:
tsxHow It Works
- Extraction: TableOfContents searches for an element with
data-content-areaattribute - Parsing: Finds all headings matching the specified levels within that area
- ID Generation: Automatically generates IDs for headings that don't have them (using slug format)
- Navigation: Renders clickable links for each heading
- Scroll Tracking: Uses IntersectionObserver to highlight the currently visible section
Requirements
For TableOfContents to work properly:
- Content Area Marker: Content must be wrapped in an element with
data-content-areaattribute - Heading IDs: Headings should have
idattributes (automatically generated if missing) - Client-Side: Component must be rendered client-side (uses browser APIs)
With MDX
When using MDX, add rehype-slug to automatically generate heading IDs:
jsAccessibility
Table of Contents provides comprehensive accessibility features:
Keyboard Navigation
- Tab - Navigate through TOC links
- Enter - Jump to section
- Links are keyboard accessible and focusable
Screen Readers
- TOC announced as navigation landmark
- Current section highlighted with aria-current
- Links include descriptive text from headings
- Scroll position updates announced
Focus Management
- Clear focus indicators on all links
- Focus preserved after navigation
- Smooth scroll behavior for better orientation
Changelog
Added
- Auto-generated table of contents from headings
- IntersectionObserver-based scroll tracking
- Active section highlighting
- Configurable heading levels
- Automatic ID generation for headings without IDs
- Custom container rendering support
- Client-side heading extraction
© 2026 Kushagra Dhawan. Licensed under MIT. GitHub.