NGU-Web/src/components/PostContents.tsx
Zaldimmar d81f7bd856 Adopt the new palette; square corners, superellipse buttons
The theme defaults (light and dark) come from the 16-color grid,
kept as PALETTE in src/lib/color.ts; every text pair passes the
admin's 4.5:1 check. Nothing is rounded any more except buttons,
which share one btn utility (Poppins Bold, a superellipse corner
where the browser has corner-shape) and true dots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 14:40:39 -05:00

93 lines
3.5 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
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 { scrollBehavior } from '../lib/motion.ts'
function useLit(entries: ContentsEntry[]) {
const [lit, setLit] = useState<string | null>(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<HTMLAnchorElement>, id: string) {
event.preventDefault()
document.getElementById(id)?.scrollIntoView({ behavior: scrollBehavior(), block: 'start' })
history.replaceState(null, '', `#${id}`)
}
function List({ entries, lit }: { entries: ContentsEntry[]; lit: string | null }) {
return (
<ol className="space-y-1 text-sm">
{entries.map((entry) => (
<li key={entry.id} className={entry.level === 2 ? 'pl-4' : ''}>
<a
href={`#${entry.id}`}
onClick={(event) => 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}
</a>
</li>
))}
</ol>
)
}
export default function PostContents({ entries }: { entries: ContentsEntry[] }) {
const lit = useLit(entries)
return (
<>
<details className="border border-brand/20 bg-tint-soft p-4 lg:hidden print:hidden">
<summary className="cursor-pointer font-semibold text-ink">On this page</summary>
<div className="mt-3">
<List entries={entries} lit={lit} />
</div>
</details>
<nav
aria-label="On this page"
className="sticky hidden self-start lg:block print:hidden"
style={{ top: 'calc(var(--header-h, 5rem) + 1.5rem)' }}
>
<p className="mb-2 px-3 text-sm font-semibold text-ink">On this page</p>
<List entries={entries} lit={lit} />
</nav>
</>
)
}