/* ═══════════════════════════════════════════════════════════════ POST CONTENTS A post's table of contents, from its headings (lib/contents.ts). On a wide screen it's a sticky column beside the post, the heading you're reading lit; below lg it's a fold-out at the top. Which heading is lit comes from an IntersectionObserver, not a scroll listener: the last heading to cross the band just under the fixed header. ═══════════════════════════════════════════════════════════════ */ import { useEffect, useState } from 'react' import type { ContentsEntry } from '../lib/contents.ts' import { jumpTo } from '../lib/focus.ts' function useLit(entries: ContentsEntry[]) { const [lit, setLit] = useState(entries[0]?.id ?? null) const ids = entries.map((entry) => entry.id).join('|') useEffect(() => { const els = ids .split('|') .map((id) => document.getElementById(id)) .filter((el): el is HTMLElement => Boolean(el)) if (els.length === 0) return // The band: from just under the header to 60% up the screen. const header = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--header-h')) || 80 const observer = new IntersectionObserver( (records) => { const crossing = records.filter((r) => r.isIntersecting).map((r) => r.target.id) if (crossing.length) setLit(crossing[0]) }, { rootMargin: `-${header + 16}px 0px -60% 0px` }, ) els.forEach((el) => observer.observe(el)) return () => observer.disconnect() }, [ids]) return lit } function jump(event: React.MouseEvent, id: string) { event.preventDefault() jumpTo(id) history.replaceState(null, '', `#${id}`) } function List({ entries, lit }: { entries: ContentsEntry[]; lit: string | null }) { return (
    {entries.map((entry) => (
  1. jump(event, entry.id)} aria-current={entry.id === lit ? 'location' : undefined} className={ 'block border-l-2 px-3 py-1.5 transition-colors ' + (entry.id === lit ? 'border-link bg-tint font-semibold text-link' : 'border-transparent text-muted hover:text-link') } > {entry.text}
  2. ))}
) } export default function PostContents({ entries }: { entries: ContentsEntry[] }) { const lit = useLit(entries) return ( <>
On this page
) }