NGU-Web/src/components/PostContents.tsx
Zaldimmar b816203c0c Accessibility: reading and color blind modes, audit fixes, calendar window
- Accessibility panel: the switch knob stays in its track; the panel
  follows its button in the Tab order and opens on its first switch.
- Reading mode is the page as plain text: one column, no colors,
  pictures, cards or motion; descriptions stay in place.
- Color blind mode (protanopia, deuteranopia, tritanopia, monochrome):
  palette swaps chosen by simulation, applied to code, data and CSS
  colors on public pages only. The newsletter uses the stored palette.
- Audit fixes: focus not hidden by the header, focus on page change
  and in-page jumps, focus return for the menu, day list and people
  details, labelled admin fields and dialogs, keyboard map tiles,
  scrollable tables, and more.
- Calendar asks /events for its six weeks only (?from=&to=); carousels
  take their 12 most recent past events (?past=12).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 14:33:09 -05:00

93 lines
3.4 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 { jumpTo } from '../lib/focus.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()
jumpTo(id)
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>
</>
)
}