Overview
The Table of Contents (ToC) provides an interactive navigation sidebar that displays the page’s section structure. It tracks your reading position, allows expanding/collapsing of nested sections, and enables quick navigation to any part of the page.The Table of Contents is automatically generated from page headings and updates dynamically as you navigate the page.
Features
- Active section highlighting - Shows your current position in the document
- Collapsible subsections - Expand/collapse nested sections to reduce clutter
- Keyboard and mouse navigation - Multiple ways to interact
- Smooth scrolling - Animated scroll to selected sections
- Auto-expand on navigation - Opens relevant sections when jumping to headings
- Persistent state - Remembers which sections you’ve expanded
- Hash change support - Updates when URL hash changes
User Interface
Structure
The ToC displays a hierarchical list of page sections:Visual Indicators
- ▼ Toggle arrow - Click to expand/collapse subsections
- Bold text - Active top-level section
- Highlight background - Currently visible section
- Indentation - Shows section hierarchy
Collapsible Sections
Top-level sections with subsections can be collapsed:- Collapsed - Shows only the parent heading
- Expanded - Shows all subsections
- Auto-collapse - Enabled when page has 4+ level-1 sections and enough total sections
Interactions
Clicking Links
- Click a section link - Navigates to that section
- ToC highlights the active section - Shows where you are
- Parent sections expand - Reveals subsection hierarchy
- Smooth scroll - Page scrolls smoothly to the heading
Toggle Buttons
Click the ▼ arrow to expand/collapse subsections:- Collapsed → Click to expand and show subsections
- Expanded → Click to collapse and hide subsections
- State persists - Stays expanded until you collapse it
Keyboard Navigation
- Click any ToC link - Standard keyboard navigation works
- Tab - Move between ToC links
- Enter - Navigate to selected section
Pinned/Unpinned
On desktop, the ToC can be pinned or unpinned:- Pinned - Always visible in sidebar
- Unpinned - Hidden, can be toggled
Automatic Active Section Tracking
The ToC automatically highlights the section you’re currently reading:- Scroll observer monitors your position (implemented separately)
- Active section changes as you scroll
- ToC updates to highlight current section
- Parent sections expand if needed
- ToC scrolls to keep active section visible
Active section tracking is handled by the section observer system, which works in conjunction with the ToC.
Configuration
Collapse Threshold
The ToC auto-collapses subsections based on page size. Configure the threshold intableOfContentsConfig.json:
- Page has 4+ level-1 sections AND
- Total section count ≥
CitizenTableOfContentsCollapseAtCount
CSS Classes
The ToC uses specific CSS classes for styling and behavior:Technical Implementation
Architecture
The ToC is implemented as a JavaScript class:Key Methods
Section Management
Expand/Collapse
Scrolling
prefers-reduced-motion:
Dynamic Reloading
The ToC can reload dynamically when content changes (e.g., after editing):- Saves expanded section IDs
- Re-renders ToC HTML from section data
- Re-expands previously expanded sections
- Restores active section highlighting
Section Data Format
Event Handling
Hash Changes
The ToC listens for URL hash changes:Click Events
Delegated click handler on ToC container:Accessibility
ARIA Attributes
Toggle buttons include proper ARIA attributes:Keyboard Support
- Tab navigation - All links and buttons are focusable
- Enter/Space - Activate links and buttons
- Focus visible - Clear visual focus indicators
Reduced Motion
Respects user preferences for reduced motion:Integration with Other Features
Section Observer
Works with the section observer to track reading position:Collapsible Sections
When collapsible sections are enabled:- ToC navigation still works
- Clicking ToC link expands collapsed sections
hidden="until-found"allows browser to auto-expand
Sticky Header
ToC accounts for sticky header offset:- Scroll calculations consider header height
- Active section detection compensates for fixed header
Best Practices
Limit nesting depth to 3-4 levels maximum. Deeper nesting makes the ToC harder to navigate and understand.
Customization
Styling
Customize ToC appearance with CSS:Behavior
Extend ToC behavior with hooks:Troubleshooting
ToC Not Appearing
- Check that page has headings (h2, h3, etc.)
- Verify
mw-panel-toc-listcontainer exists - Check that
skins.citizen.scriptsmodule loaded
Active Section Not Highlighting
- Ensure section observer is running
- Check that ToC section IDs match page heading IDs
- Verify no JavaScript errors in console
Sections Not Expanding
- Check that toggle buttons have correct classes
- Verify ARIA attributes are set correctly
- Ensure click event listeners are attached
Performance Considerations
- Efficient DOM queries - Uses
getElementByIdandclosest() - Event delegation - Single click listener on container
- Throttled updates - Section activation is debounced
- Smooth scroll - Hardware-accelerated when supported
- Lazy initialization - Only activates when ToC exists on page