Skip to content
Playground

useScrollSpy

Track the reached heading in a document or scroll pane.

TSX
import { useScrollSpy } from "@zentara-co/zenos-ui"; const activeId = useScrollSpy(["overview", "installation", "configuration"]);// For an independent scroll pane:const paneActiveId = useScrollSpy(ids, { container: articleRef });

Pass heading IDs in document order. The hook returns the first ID during server rendering, or an empty string for an empty array. After mounting, it tracks heading positions on scroll, resize, and content changes. Missing elements are skipped. Each heading’s scroll-margin-top determines when it is reached; at the end of a scrolled document, the last heading becomes active.

The optional container ref selects an independently scrolling element. Headings outside that pane are ignored. The hook reads positions and returns an ID; it does not render links, change the URL, or move the document.

See Table of Contents for navigation that includes link activation and an indicator.