/* ═══════════════════════════════════════════════════════════════ ACCESSIBILITY PANEL A button fixed to the bottom right of every public page, opening two switches and a choice that change how the whole site reads: Dyslexia-friendly font every typeface becomes OpenDyslexic (self-hosted, index.css) Reading mode the page as text: its headings, paragraphs, links and described pictures in one column, nothing else (ReaderView.tsx) Color blind mode the palette swapped so its colors stay apart for that type of color blindness, or grays (color.ts, VISIONS) Each is a data attribute on (data-font, data-reading, data-vision) that index.css styles, saved in this browser (site-font, site-reading, site-vision) and set again before the first paint by index.html, so a page never flashes the other look. A color blind mode reloads the page, since colors set from code and data are swapped as they load. Blocked storage just means the choice lasts until the tab closes. Reading and color blind modes belong to the public pages, which are the only ones with this panel: it sets them when they mount and clears them when the admin takes over (index.html leaves them off there too), since the admin edits the colors and layout they would hide. The font follows the visitor everywhere. The panel opens on its first switch, comes after its button in the Tab order, closes on Escape, on the button again, or a click anywhere else, and gives focus back to the button. ═══════════════════════════════════════════════════════════════ */ import { PersonArmsSpread, X } from '@phosphor-icons/react' import { useEffect, useId, useRef, useState } from 'react' import { VISIONS } from '../lib/color.ts' /* A setting: its attribute, storage key, the value that turns it on, and its words. */ const SETTINGS = [ { attribute: 'font', key: 'site-font', on: 'dyslexic', label: 'Dyslexia-friendly font', help: 'Letters shaped to be easier to tell apart.', }, { attribute: 'reading', key: 'site-reading', on: 'on', label: 'Reading mode', help: 'The page as text: headings, paragraphs, links and pictures in one column, nothing else.', }, ] as const type Setting = (typeof SETTINGS)[number] /* The saved choice, or the page's own where storage is blocked. */ function saved(attribute: string, key: string): string | null { try { return localStorage.getItem(key) } catch { return document.documentElement.dataset[attribute] ?? null } } const isOn = (setting: Setting) => saved(setting.attribute, setting.key) === setting.on const PUBLIC_ONLY = [ { attribute: 'reading', key: 'site-reading' }, { attribute: 'vision', key: 'site-vision' }, ] /* Sets (or, given null, clears) an attribute and its saved copy. */ function apply(attribute: string, key: string, value: string | null) { const root = document.documentElement if (value) root.dataset[attribute] = value else delete root.dataset[attribute] try { if (value) localStorage.setItem(key, value) else localStorage.removeItem(key) } catch { // Storage blocked: the choice holds for this page view. } } export default function AccessibilityPanel() { const [open, setOpen] = useState(false) const [state, setState] = useState(() => Object.fromEntries(SETTINGS.map((s) => [s.key, isOn(s)]))) const [vision, setVision] = useState(() => saved('vision', 'site-vision') ?? '') const rootRef = useRef(null) const buttonRef = useRef(null) const firstRef = useRef(null) const panelId = useId() useEffect(() => { const root = document.documentElement for (const { attribute, key } of PUBLIC_ONLY) { const value = saved(attribute, key) if (value) root.dataset[attribute] = value } return () => { for (const { attribute } of PUBLIC_ONLY) delete root.dataset[attribute] } }, []) useEffect(() => { if (!open) return undefined firstRef.current?.focus() const close = () => { setOpen(false) buttonRef.current?.focus() } const onKey = (event: KeyboardEvent) => { if (event.key === 'Escape') close() } const onPointer = (event: PointerEvent) => { if (!rootRef.current?.contains(event.target as Node)) setOpen(false) } document.addEventListener('keydown', onKey) document.addEventListener('pointerdown', onPointer) return () => { document.removeEventListener('keydown', onKey) document.removeEventListener('pointerdown', onPointer) } }, [open]) const toggle = (setting: Setting) => { const next = !state[setting.key] apply(setting.attribute, setting.key, next ? setting.on : null) setState((current) => ({ ...current, [setting.key]: next })) } const chooseVision = (value: string) => { setVision(value) apply('vision', 'site-vision', value || null) window.location.reload() } return (
{/* After the button in the page, so Tab goes from it into the panel; drawn above it. */} {open && (

Accessibility

    {SETTINGS.map((setting, i) => { const on = state[setting.key] const labelId = `${panelId}-${setting.attribute}` const helpId = `${labelId}-help` return (
  • {setting.label} {setting.help}
  • ) })}
)}
) }