A button fixed to the bottom right of every public page opens two switches. Dyslexia-friendly font swaps every typeface for OpenDyslexic (self-hosted, @fontsource/opendyslexic, downloaded only when chosen). Reading mode steps the design back: no background circles, motion or shadows, every layout one column, larger text and underlined links. Both are data attributes on <html>, remembered in the browser and set before the first paint like the theme. On a phone the privacy notice sits above the button. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
6.3 KiB
TypeScript
168 lines
6.3 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
ACCESSIBILITY PANEL
|
|
|
|
A button fixed to the bottom right of every public page, opening
|
|
two switches that change how the whole site reads:
|
|
|
|
Dyslexia-friendly font every typeface becomes OpenDyslexic
|
|
(self-hosted, index.css)
|
|
Reading mode the design steps back: no background
|
|
circles, motion or shadows, one column,
|
|
larger text and plain underlined links,
|
|
the same content throughout
|
|
|
|
Each is a data attribute on <html> (data-font, data-reading) that
|
|
index.css styles, saved in this browser (site-font, site-reading)
|
|
and set again before the first paint by index.html, so a page
|
|
never flashes the other look. Blocked storage just means the
|
|
choice lasts until the tab closes.
|
|
|
|
The panel 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'
|
|
|
|
/* 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: 'A plain, single column with larger text and no motion.',
|
|
},
|
|
] as const
|
|
|
|
type Setting = (typeof SETTINGS)[number]
|
|
|
|
const isOn = (setting: Setting) => document.documentElement.dataset[setting.attribute] === setting.on
|
|
|
|
function apply(setting: Setting, on: boolean) {
|
|
const root = document.documentElement
|
|
if (on) root.dataset[setting.attribute] = setting.on
|
|
else delete root.dataset[setting.attribute]
|
|
try {
|
|
if (on) localStorage.setItem(setting.key, setting.on)
|
|
else localStorage.removeItem(setting.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 rootRef = useRef<HTMLDivElement>(null)
|
|
const buttonRef = useRef<HTMLButtonElement>(null)
|
|
const panelId = useId()
|
|
|
|
useEffect(() => {
|
|
if (!open) return undefined
|
|
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, next)
|
|
setState((current) => ({ ...current, [setting.key]: next }))
|
|
}
|
|
|
|
return (
|
|
<div ref={rootRef} className="fixed right-4 bottom-4 z-40 print:hidden">
|
|
{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-8 w-8 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) => {
|
|
const on = state[setting.key]
|
|
const labelId = `${panelId}-${setting.attribute}`
|
|
return (
|
|
<li key={setting.key} className="flex items-start gap-3">
|
|
<button
|
|
type="button"
|
|
role="switch"
|
|
aria-checked={on}
|
|
aria-labelledby={labelId}
|
|
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'
|
|
}`}
|
|
>
|
|
<span
|
|
aria-hidden="true"
|
|
className={`absolute top-0.5 h-4 w-4 rounded-full transition-transform ${
|
|
on ? 'translate-x-5 bg-white' : 'translate-x-0.5 bg-muted'
|
|
}`}
|
|
/>
|
|
</button>
|
|
<span>
|
|
<span id={labelId} className="block font-semibold text-ink">
|
|
{setting.label}
|
|
</span>
|
|
<span className="block text-sm text-muted">{setting.help}</span>
|
|
</span>
|
|
</li>
|
|
)
|
|
})}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
|
|
<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>
|
|
</div>
|
|
)
|
|
}
|