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 and keeps it current: headings added, removed, or renamed later rebuild the list automatically, and the heading in view is the component's value.

Table of contents

The empty nav is filled from the sample content beside it, or below it on narrow screens. 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);
// 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, article'Root element to scan for headings; rendered as `data-hl-content-selector`.

Events and callbacks

Event / CallbackPayloadDescription
hl:change{ value: string | null }Fires as the reader scrolls, with the id of the heading in view (also the `onValueChange` enhancer callback).

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.