Modern Toc

Modern Table of Contents (Right Menu) Styling

Overview

Enhanced the right-side Table of Contents (TOC) menu with modern, professional styling and interactive features.

Features Implemented

🎨 Visual Enhancements

  1. Clean White Background - Matches left sidebar design
  2. Border & Shadow - Subtle left border and shadow for depth
  3. “On This Page” Header - Purple underline accent
  4. Hover Effects - Smooth color transitions and movement
  5. Active Section Highlighting - Purple accent on current section
  6. Reading Progress Dots - Visual indicators for each heading
  7. Nested Indentation - Clear hierarchy for H2, H3, H4, etc.

🚀 Interactive Features

  1. Auto-Highlight on Scroll - JavaScript automatically highlights current section
  2. Smooth Scrolling - Click TOC links for smooth page navigation
  3. Auto-Scroll TOC - TOC scrolls to show active link
  4. Reading Progress Bar - Visual indicator of page progress
  5. Hover Arrows - Right arrow appears on hover

📱 Responsive Design

Screen SizeBehavior
< 1200pxHidden (mobile/small tablets)
1201-1400pxCompact mode (14rem width)
1401-1919pxStandard mode (16rem width)
1920px+Expanded mode (18rem width)

♿ Accessibility

  • Keyboard Navigation - Full keyboard support
  • Focus Indicators - Clear focus outlines
  • ARIA Labels - aria-current="true" for active links
  • High Contrast Mode - Enhanced borders and backgrounds
  • Reduced Motion - Respects user preferences
  • Screen Reader Friendly - Semantic HTML structure

File Structure

docs-hugo/
├── static/
│   ├── css/
│   │   └── modern-toc.css          # TOC styling (8KB)
│   └── js/
│       └── modern-toc.js           # Active highlighting (5KB)
└── layouts/
    └── partials/
        └── docs/
            └── inject/
                └── head.html       # Import CSS/JS

CSS Classes

Main Classes

  • .book-toc - Main TOC container
  • .book-toc nav - TOC navigation wrapper
  • .book-toc nav ul - List containers
  • .book-toc nav ul a - TOC links

State Classes

  • .active - Currently visible section
  • .compact - Compact mode (optional)
  • .expanded - Expanded mode (optional)

Pseudo-elements

  • ::before - “On This Page” header and progress indicator
  • ::after - Hover arrow

Color Scheme

ElementColorUsage
Background#ffffffMain background
Border#e8e8e8Separators
Text#555Default links
Hover#667eeaInteractive states
Active#667eeaCurrent section
Nested#666 / #777Subdued hierarchy

JavaScript Features

Auto-Highlighting

// Automatically highlights section as user scrolls
config.offset = 100; // Pixels from top to trigger

Smooth Scrolling

// Click TOC links for smooth navigation
config.smoothScroll = true;

Reading Progress

// Visual progress bar showing page completion
addReadingProgress();

Customization

Change Header Text

.book-toc nav > ul:first-child::before {
  content: 'Table of Contents'; /* Change this */
}

Change Active Color

.book-toc nav ul a.active {
  color: #your-color;
  border-left-color: #your-color;
}

Adjust Spacing

.book-toc nav ul li {
  margin: 0.5rem 0; /* Adjust this */
}

Change Offset for Active Highlighting

// In modern-toc.js
const config = {
  offset: 150, // Increase/decrease as needed
};

Usage Examples

Standard TOC

<!-- Automatically generated by Hugo -->
<aside class="book-toc">
  <nav>
    <ul>
      <li><a href="#heading-1">Heading 1</a></li>
      <li><a href="#heading-2">Heading 2</a>
        <ul>
          <li><a href="#subheading">Subheading</a></li>
        </ul>
      </li>
    </ul>
  </nav>
</aside>

With Custom Classes

<aside class="book-toc compact">
  <!-- More condensed spacing -->
</aside>

<aside class="book-toc expanded">
  <!-- More spacious layout -->
</aside>

Browser Support

✅ Chrome/Edge 90+
✅ Firefox 88+
✅ Safari 14+
✅ Opera 76+
✅ Mobile browsers (iOS Safari, Chrome Mobile)

Performance

  • CSS Size: ~8KB (minified ~5KB)
  • JS Size: ~5KB (minified ~2KB)
  • Load Time: < 50ms
  • Render Impact: Minimal
  • No Dependencies: Pure CSS/JS

Features Comparison

FeatureBeforeAfter
Visual StyleBasic listModern design
Active HighlightingManualAutomatic
Smooth ScrollingBrowser defaultEnhanced
Progress IndicatorNoneVisual bar
ResponsiveFixed widthAdaptive
AccessibilityBasicEnhanced
Hover EffectsNoneSmooth transitions

Dark Mode Support

The TOC includes dark mode support that activates automatically:

@media (prefers-color-scheme: dark) {
  .book-toc {
    background: #1a1a1a;
    color: #ccc;
  }
}

Testing Checklist

  • TOC appears on pages with headings
  • Active section highlights on scroll
  • Click links for smooth navigation
  • Hover effects work properly
  • Nested headings display correctly
  • Progress bar updates on scroll
  • Responsive breakpoints work
  • Keyboard navigation functional
  • Screen reader accessible

Troubleshooting

TOC Not Highlighting

  • Check if headings have id attributes
  • Verify JavaScript is loaded
  • Check console for errors

Styling Not Applied

  • Clear browser cache
  • Verify CSS file is loaded
  • Check for CSS conflicts

Smooth Scroll Not Working

  • Check browser compatibility
  • Verify JavaScript is enabled
  • Check for conflicts with other scripts

Future Enhancements

Potential additions:

  • Collapse/expand functionality
  • Search within TOC
  • Estimated reading time per section
  • Print-friendly view
  • Bookmark/favorite sections
  • TOC export (PDF, Markdown)

Credits

  • Design inspired by: Stripe, GitHub, and MDN documentation
  • Icons: Font Awesome 6.4
  • Smooth scroll: Native browser API
  • Progress indicator: Custom implementation

Result: Professional, interactive Table of Contents with automatic section highlighting! 📚