'use client'; import { useEffect, useRef, useState, type ReactNode } from 'react'; export type TocItem = { value: ReactNode; id: string; depth: number; }; /** * Track the section the reader is in, and resolve the items to show. * * Nextra builds the TOC from markdown headings only, so pages whose sections * are emitted by a component (an API reference, most of all) arrive with an * empty toc — the hook falls back to the headings actually rendered into the * article. Returns the resolved items plus the active heading id, which * doubles as the feedback widget's `sectionId`. */ export function useSectionSpy(toc?: TocItem[]): { items: TocItem[]; activeId: string | null } { const [activeId, setActiveId] = useState(null); const [derived, setDerived] = useState([]); useEffect(() => { if (toc && toc.length > 0) return; const headings = document.querySelectorAll( 'article h2[id], article h3[id]', ); setDerived( [...headings].map((heading) => { const label = heading.cloneNode(true) as HTMLHeadingElement; label.querySelector('a[aria-label]')?.remove(); return { id: heading.id, value: label.textContent?.trim() ?? '', depth: Number(heading.tagName[1]), }; }), ); }, [toc]); const items = toc && toc.length > 0 ? toc : derived; useEffect(() => { if (items.length === 0) return; const ids = items.map((item) => item.id); function spy() { const top = window.scrollY + 140; let current: string | null = ids[0] ?? null; for (const id of ids) { const element = document.getElementById(id); if (element && element.offsetTop <= top) current = id; } setActiveId(current); } spy(); window.addEventListener('scroll', spy, { passive: true }); window.addEventListener('resize', spy); return () => { window.removeEventListener('scroll', spy); window.removeEventListener('resize', spy); }; }, [items]); return { items, activeId }; } const FADE = 28; /** * Only the heading list scrolls; everything in `footer` stays pinned. * * A long page (Stage is 16 sections) used to overflow the whole rail, which * put a scrollbar down the side and pushed the markdown actions and the * feedback prompt below the fold, where nobody found them. The list gets the * leftover height and scrolls inside itself instead, its scrollbar traded for * a fade at whichever edge still has content behind it. */ function useRailScroll(items: TocItem[], activeId: string | null) { const ref = useRef(null); const [edges, setEdges] = useState({ top: false, bottom: false }); useEffect(() => { const element = ref.current; if (!element) return; const update = () => { const overflowing = element.scrollHeight > element.clientHeight + 1; setEdges({ top: overflowing && element.scrollTop > 4, bottom: overflowing && element.scrollTop + element.clientHeight < element.scrollHeight - 4, }); }; update(); element.addEventListener('scroll', update, { passive: true }); const observer = new ResizeObserver(update); observer.observe(element); return () => { element.removeEventListener('scroll', update); observer.disconnect(); }; }, [items]); // Follow the reader: once the list scrolls, the highlighted section would // otherwise drift out of the rail's viewport. Never smooth — this fires // during page scrolling, and an animation here fights the reader. // // Positions come from rects, not offsetTop: the link's offsetParent is the // sticky