- Reading mode is now a reader view (ReaderView.tsx): the page renders hidden and its headings, paragraphs, links and described pictures are written out in one column, with no header, backdrop or footer. - Color blind modes swap all three color families (teal, green, amber); palette hexes in components and CSS are now var(--pal-<family>-<shade>) so the modes reach them. - CLAUDE.md and docs/updating-data-in-code.md describe both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
245 lines
9.4 KiB
TypeScript
245 lines
9.4 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
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 <html> (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 <html> 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 <html> 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<HTMLDivElement>(null)
|
|
const buttonRef = useRef<HTMLButtonElement>(null)
|
|
const firstRef = useRef<HTMLButtonElement>(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 (
|
|
<div ref={rootRef} className="fixed right-4 bottom-4 z-50 print:hidden">
|
|
<button
|
|
ref={buttonRef}
|
|
type="button"
|
|
onClick={() => setOpen((value) => !value)}
|
|
aria-expanded={open}
|
|
aria-controls={open ? panelId : undefined}
|
|
aria-label="Accessibility options"
|
|
className="btn flex h-12 w-12 items-center justify-center bg-action text-white shadow-lg transition-colors hover:bg-action-hover"
|
|
>
|
|
<PersonArmsSpread size={26} weight="bold" aria-hidden="true" />
|
|
</button>
|
|
|
|
{/* After the button in the page, so Tab goes from it into the
|
|
panel; drawn above it. */}
|
|
{open && (
|
|
<div
|
|
id={panelId}
|
|
role="region"
|
|
aria-label="Accessibility"
|
|
className="absolute right-0 bottom-full mb-3 w-[min(20rem,calc(100vw-2rem))] border border-brand/20 bg-raised p-4 shadow-xl"
|
|
>
|
|
<div className="flex items-center justify-between gap-4">
|
|
<h2 className="font-display text-base font-bold text-ink">Accessibility</h2>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setOpen(false)
|
|
buttonRef.current?.focus()
|
|
}}
|
|
className="btn flex h-10 w-10 items-center justify-center text-muted hover:bg-tint"
|
|
aria-label="Close accessibility options"
|
|
>
|
|
<X size={18} weight="bold" aria-hidden="true" />
|
|
</button>
|
|
</div>
|
|
|
|
<ul className="mt-3 space-y-3">
|
|
{SETTINGS.map((setting, i) => {
|
|
const on = state[setting.key]
|
|
const labelId = `${panelId}-${setting.attribute}`
|
|
const helpId = `${labelId}-help`
|
|
return (
|
|
<li key={setting.key} className="flex items-start gap-3">
|
|
<button
|
|
ref={i === 0 ? firstRef : undefined}
|
|
type="button"
|
|
role="switch"
|
|
aria-checked={on}
|
|
aria-labelledby={labelId}
|
|
aria-describedby={helpId}
|
|
onClick={() => toggle(setting)}
|
|
className={`relative mt-0.5 h-6 w-11 shrink-0 rounded-full border-2 transition-colors ${
|
|
on ? 'border-action bg-action' : 'border-muted/50 bg-surface'
|
|
}`}
|
|
>
|
|
{/* Pinned to the left edge: without it the knob
|
|
starts from the button's centered content and
|
|
slides out of the track when switched on. */}
|
|
<span
|
|
aria-hidden="true"
|
|
className={`absolute top-0.5 left-0.5 h-4 w-4 rounded-full transition-transform ${
|
|
on ? 'translate-x-5 bg-white' : 'translate-x-0 bg-muted'
|
|
}`}
|
|
/>
|
|
</button>
|
|
<span>
|
|
<span id={labelId} className="block font-semibold text-ink">
|
|
{setting.label}
|
|
</span>
|
|
<span id={helpId} className="block text-sm text-muted">
|
|
{setting.help}
|
|
</span>
|
|
</span>
|
|
</li>
|
|
)
|
|
})}
|
|
</ul>
|
|
|
|
<label className="mt-4 block">
|
|
<span className="block font-semibold text-ink">Color blind mode</span>
|
|
<span className="block text-sm text-muted">
|
|
Colors chosen to stay apart for your type. Reloads the page.
|
|
</span>
|
|
<select
|
|
value={vision}
|
|
onChange={(event) => chooseVision(event.target.value)}
|
|
className="mt-2 w-full border border-muted/50 bg-surface px-3 py-2 text-sm text-ink"
|
|
>
|
|
<option value="">Off</option>
|
|
{VISIONS.map((option) => (
|
|
<option key={option.id} value={option.id}>
|
|
{option.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|