import { useEffect, useState } from 'react'; // A row of links to the sections of a long page. It finds them itself: any // element with a `data-jump="Label"` attribute that is on the page right now // gets a link, so sections only some people see need no special handling. export function JumpNav() { const [labels, setLabels] = useState([]); // Sections come and go as data loads, so the page is watched for them. useEffect(() => { const look = () => { const found = [...document.querySelectorAll('[data-jump]')].map((el) => el.dataset.jump ?? ''); setLabels((current) => (current.join('|') === found.join('|') ? current : found)); }; const observer = new MutationObserver(look); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['data-jump'] }); look(); return () => observer.disconnect(); }, []); if (labels.length < 3) return null; return ( ); }