useScrollSpy
Find which section is currently in view, for a table of contents or sticky nav.
Source@monoline/react 0.4.0Markdown for AI toolsstable
- No DOM output. Highlight the active link and keep real links so keyboard and no-JS users can navigate.
Import
import '@monoline/css';
import {useScrollSpy} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add use-scroll-spy
Usage
import {useScrollSpy, type Demo} from '@monoline/react';
function Demo() {
const active = useScrollSpy(['intro', 'usage'], 0);
return <p>Active section: <output>{active ?? 'none yet'}</output>. Add elements with these ids to your page and scroll.</p>;
}
<Demo />
Examples
Active section
Behaviour
- No DOM output. Highlight the active link and keep real links so keyboard and no-JS users can navigate.