Active Heading
Configurable active heading detection for table of contents. Tracks which heading is currently active based on scroll position, with top-line or viewport-center activation modes and a programmatic scrollTo helper.
Installation
dgadd ships in @diffgazer/add on npm: pnpm add -D @diffgazer/add puts it on pnpm exec, and npx @diffgazer/add runs it without the install.
UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS from the installed package.
Parameters
Returns
Examples
Basic Table of Contents
Activation Modes
Notes
Disabled state
When enabled is false, activeId is null from the first render, including server rendering. Enabling observation later starts from the first available heading; disabling it again clears activeId.
Activation Modes
Use "top-line" (default) for fixed-header layouts where headings activate near the top. Use "viewport-center" for centered reading experiences. Pass a number (0–1) for a custom viewport fraction.
Scroll Settling
When scrollTo is called, scroll-listener updates are suppressed until the animation settles (150ms after the last scroll event). This prevents the active heading from flickering through intermediate headings during programmatic scrolls.
Bottom Lock
When bottomLock is true (default), the last heading is always highlighted when scrolled to the bottom — without it, a last section shorter than the viewport never becomes active. It never fires on a container that cannot scroll, so a static pane keeps its first heading active.
Source
Highlighted source loads after this disclosure opens. Browse the source repository.