Skip to content

Navigation

Table of Contents

Auto-generated navigation built from page headings.

stableEnhancer: enhanceTocBuilt on <nav>hydrateless/toc.css

Auto-generated navigation built from the headings on the page, with optional scroll-spy highlighting of the current section. Add an empty <nav data-hl-toc> and the enhancer fills it in.

Table of contents

The empty nav on the left is filled from the sample content on the right. Toggle JS off to see the unenhanced state.

Live

Introduction

A short overview of the project.

Installation

Package managers

Install with your tool of choice.

Usage

Import a component and render it.

JavaScript

This component ships an optional enhancer. With auto-init it loads automatically; to wire it yourself, import it directly:

import { enhanceToc } from '@hydrateless/enhancers/toc';

const handle = enhanceToc(document, { headings, scrollSpy, contentSelector });
// handle.api  -> imperative controls
// handle.destroy()  -> tear everything down

Props

The framework bindings expose these props (and forward the rest to the root element).

PropTypeDefaultDescription
headingsstring'h2,h3'Which headings to include.
scrollSpybooleantrueHighlight the section currently in view.
contentSelectorstring'main'Root element to scan for headings.

CSS variables

Override these on any ancestor to theme the component. See the theme studio to preview changes live.

CSS variableUsed for
--hl-borderOutline of the nav.
--hl-surface-2Active link background.
--hl-radius-lgCorner radius.

Accessibility

  • Headings without an id are given one automatically so links resolve.
  • Scroll-spy sets aria-current on the link for the section in view.

Released under the MIT License.