// The section jump strip (SectionJump.astro): marks the section being read // and keeps its link in view inside the strip's own sideways scroll. export function initSectionJump(): void { const nav = document.querySelector('[data-sec-jump]'); if (!nav || nav.dataset.bound || !('IntersectionObserver' in window)) return; nav.dataset.bound = '1'; const track = nav.querySelector('.sec-jump__track'); const links = [...nav.querySelectorAll('a[data-sf-jump]')]; const byId = new Map(links.map((l) => [decodeURIComponent(l.hash.slice(1)), l])); const mark = (id: string): void => { const link = byId.get(id); if (!link || link.getAttribute('aria-current') === 'true') return; links.forEach((l) => l.removeAttribute('aria-current')); link.setAttribute('aria-current', 'true'); // Scroll the strip, never the page: `scrollIntoView` would move both. if (track) { const left = link.offsetLeft - track.offsetLeft; const right = left + link.offsetWidth; if (left < track.scrollLeft) track.scrollLeft = left; else if (right > track.scrollLeft + track.clientWidth) track.scrollLeft = right - track.clientWidth; } }; // A band just under the masthead and strip: whichever section crosses it // is the one being read. const observer = new window.IntersectionObserver( (entries) => entries.forEach((e) => { if (e.isIntersecting) mark((e.target as HTMLElement).id); }), { rootMargin: '-130px 0px -72% 0px', threshold: 0 }, ); byId.forEach((_, id) => { const section = document.getElementById(id); if (section) observer.observe(section); }); document.addEventListener('astro:before-swap', () => observer.disconnect(), { once: true }); }