Accessibility: reader view and full palette swaps; version v1.12.1-accessibility
- 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>
This commit is contained in:
parent
420a8058b4
commit
de17da7dc8
19 changed files with 507 additions and 192 deletions
217
src/components/ReaderView.tsx
Normal file
217
src/components/ReaderView.tsx
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
READER VIEW: reading mode (AccessibilityPanel.tsx)
|
||||
|
||||
The page as text: its headings, paragraphs, links and pictures,
|
||||
one column, nothing else. No buttons, cards, colors, carousels or
|
||||
forms.
|
||||
|
||||
The real page still renders, hidden (Layout.tsx), so every page
|
||||
and section works without a reading version of its own; this reads
|
||||
what it drew and writes it out plainly, again whenever it changes
|
||||
(data arriving, a new page).
|
||||
|
||||
What's kept:
|
||||
h1 to h6 headings, at their level
|
||||
p, li, td, quote... paragraphs, with their links
|
||||
a links (a card that is one link gives its
|
||||
heading the link)
|
||||
img with alt text pictures; one with empty alt is decoration
|
||||
(a card's faded background) and is left out
|
||||
a button that opens its words, as a line: a person's tile, a
|
||||
something schedule row (aria-expanded)
|
||||
What's dropped: other buttons, form fields, navigation lists, video and
|
||||
map frames, drawings, and anything hidden from screen readers
|
||||
(aria-hidden, the copies a carousel draws for its turns).
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { useEffect, useState, type ReactNode, type RefObject } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
|
||||
import { PAGE_LINKS } from '../navConfig.ts'
|
||||
|
||||
type Inline = string | { href: string; children: Inline[] }
|
||||
type Block =
|
||||
| { kind: 'heading'; level: number; content: Inline[] }
|
||||
| { kind: 'text'; content: Inline[] }
|
||||
| { kind: 'image'; src: string; alt: string }
|
||||
|
||||
const SKIP = new Set(['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'NAV', 'IFRAME', 'VIDEO', 'AUDIO', 'CANVAS', 'DIALOG', 'svg'])
|
||||
const HEADINGS = new Set(['H1', 'H2', 'H3', 'H4', 'H5', 'H6'])
|
||||
const TEXT_BLOCKS = new Set(['P', 'LI', 'DT', 'DD', 'BLOCKQUOTE', 'FIGCAPTION', 'CAPTION', 'ADDRESS', 'TH', 'TD'])
|
||||
const BLOCK_SELECTOR = 'div,section,article,header,footer,aside,main,p,ul,ol,li,dl,dt,dd,table,tr,figure,blockquote,h1,h2,h3,h4,h5,h6,img,form,fieldset'
|
||||
|
||||
/* A button that opens something (a person's tile, a schedule row):
|
||||
its words name content, so it's read as a line, not dropped. */
|
||||
const opens = (el: Element) => el.tagName === 'BUTTON' && el.hasAttribute('aria-expanded')
|
||||
|
||||
const skipped = (el: Element) =>
|
||||
(SKIP.has(el.tagName) && !opens(el)) ||
|
||||
el.getAttribute('aria-hidden') === 'true' ||
|
||||
el.hasAttribute('hidden') ||
|
||||
['button', 'switch', 'tab', 'slider', 'presentation', 'none'].includes(el.getAttribute('role') ?? '')
|
||||
|
||||
/* A link's address as the app routes it: a path for this site, so
|
||||
it opens without reloading; the full address for anywhere else. */
|
||||
function hrefOf(a: HTMLAnchorElement): string | null {
|
||||
const raw = a.getAttribute('href')
|
||||
if (!raw || raw.startsWith('javascript:')) return null
|
||||
try {
|
||||
const url = new URL(raw, location.href)
|
||||
return url.origin === location.origin ? url.pathname + url.search + url.hash : url.href
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function inline(node: Node): Inline[] {
|
||||
if (node.nodeType === Node.TEXT_NODE) return [node.textContent ?? '']
|
||||
if (!(node instanceof Element) || skipped(node) || node.tagName === 'IMG') return []
|
||||
if (node.tagName === 'BR') return [' ']
|
||||
const children = [...node.childNodes].flatMap(inline)
|
||||
if (node instanceof HTMLAnchorElement) {
|
||||
const href = hrefOf(node)
|
||||
return href ? [{ href, children }] : children
|
||||
}
|
||||
return children
|
||||
}
|
||||
|
||||
const textOf = (parts: Inline[]): string =>
|
||||
parts.map((p) => (typeof p === 'string' ? p : textOf(p.children))).join('').replace(/\s+/g, ' ').trim()
|
||||
|
||||
const isBlock = (el: Element) => opens(el) || el.matches(BLOCK_SELECTOR) || el.querySelector(BLOCK_SELECTOR) !== null
|
||||
|
||||
function extract(root: Element): Block[] {
|
||||
const blocks: Block[] = []
|
||||
const text = (content: Inline[]) => {
|
||||
if (textOf(content)) blocks.push({ kind: 'text', content })
|
||||
}
|
||||
|
||||
function walk(el: Element, link: string | null) {
|
||||
// The root is the hidden copy of the page itself: read inside it.
|
||||
if (el !== root && skipped(el)) return
|
||||
if (opens(el)) {
|
||||
// Each part on its own line in the page (a name, a title) joins
|
||||
// with a comma.
|
||||
const parts = [...el.querySelectorAll('*')]
|
||||
.filter((part) => !part.closest('[aria-hidden="true"]') && [...part.childNodes].some((n) => n.nodeType === Node.TEXT_NODE && n.textContent?.trim()))
|
||||
.map((part) => [...part.childNodes].filter((n) => n.nodeType === Node.TEXT_NODE).map((n) => n.textContent).join('').trim())
|
||||
text([parts.join(', ')])
|
||||
return
|
||||
}
|
||||
if (HEADINGS.has(el.tagName)) {
|
||||
const content = [...el.childNodes].flatMap(inline)
|
||||
if (textOf(content)) {
|
||||
blocks.push({ kind: 'heading', level: Number(el.tagName[1]), content: link ? [{ href: link, children: content }] : content })
|
||||
}
|
||||
return
|
||||
}
|
||||
if (el.tagName === 'IMG') {
|
||||
const img = el as HTMLImageElement
|
||||
const alt = img.getAttribute('alt')?.trim()
|
||||
const src = img.currentSrc || img.getAttribute('src')
|
||||
if (alt && src) blocks.push({ kind: 'image', src, alt })
|
||||
return
|
||||
}
|
||||
if (TEXT_BLOCKS.has(el.tagName) && !el.querySelector('p,li,h1,h2,h3,h4,h5,h6,img')) {
|
||||
text([...el.childNodes].flatMap(inline))
|
||||
return
|
||||
}
|
||||
if (el.tagName === 'TR') {
|
||||
const cells = [...el.children].map((cell) => [...cell.childNodes].flatMap(inline)).filter((c) => textOf(c))
|
||||
text(cells.flatMap((cell, i) => (i ? [' · ', ...cell] : cell)))
|
||||
return
|
||||
}
|
||||
// A link around a whole card: its parts, with the link on the first
|
||||
// heading (or, with none, kept as a line of its own).
|
||||
const href = el instanceof HTMLAnchorElement ? hrefOf(el) : null
|
||||
if (href && isBlock(el)) {
|
||||
const before = blocks.length
|
||||
for (const child of el.children) walk(child, href)
|
||||
if (!blocks.slice(before).some((b) => b.kind === 'heading')) {
|
||||
const label = el.getAttribute('aria-label')
|
||||
if (label) blocks.splice(before, 0, { kind: 'text', content: [{ href, children: [label] }] })
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Anything else: its loose text and inline parts make paragraphs,
|
||||
// broken where a block of its own comes.
|
||||
let run: Inline[] = []
|
||||
for (const child of el.childNodes) {
|
||||
if (child instanceof Element && !skipped(child) && isBlock(child)) {
|
||||
text(run)
|
||||
run = []
|
||||
walk(child, link)
|
||||
} else {
|
||||
run.push(...inline(child))
|
||||
}
|
||||
}
|
||||
text(run)
|
||||
}
|
||||
|
||||
walk(root, null)
|
||||
// Carousels and repeated strips can say the same line twice in a row.
|
||||
return blocks.filter((b, i) => {
|
||||
const prev = blocks[i - 1]
|
||||
return !prev || prev.kind !== b.kind || JSON.stringify(prev) !== JSON.stringify(b)
|
||||
})
|
||||
}
|
||||
|
||||
function render(parts: Inline[]): ReactNode[] {
|
||||
return parts.map((part, i) => {
|
||||
if (typeof part === 'string') return part
|
||||
const children = render(part.children)
|
||||
return part.href.startsWith('/') ? (
|
||||
<Link key={i} to={part.href}>
|
||||
{children}
|
||||
</Link>
|
||||
) : (
|
||||
<a key={i} href={part.href}>
|
||||
{children}
|
||||
</a>
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
export default function ReaderView({ source }: { source: RefObject<HTMLElement | null> }) {
|
||||
const [blocks, setBlocks] = useState<Block[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
const root = source.current
|
||||
if (!root) return undefined
|
||||
let frame = 0
|
||||
const update = () => {
|
||||
frame = 0
|
||||
setBlocks(extract(root))
|
||||
}
|
||||
const schedule = () => {
|
||||
if (!frame) frame = requestAnimationFrame(update)
|
||||
}
|
||||
update()
|
||||
const observer = new MutationObserver(schedule)
|
||||
observer.observe(root, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['src', 'alt', 'href', 'hidden', 'aria-hidden'] })
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
if (frame) cancelAnimationFrame(frame)
|
||||
}
|
||||
}, [source])
|
||||
|
||||
return (
|
||||
<article className="reader">
|
||||
<p>
|
||||
{PAGE_LINKS.map((page, i) => (
|
||||
<span key={page.path}>
|
||||
{i > 0 && ' · '}
|
||||
<Link to={page.path}>{page.label}</Link>
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
{blocks.map((block, i) => {
|
||||
if (block.kind === 'image') return <img key={i} src={block.src} alt={block.alt} loading="lazy" />
|
||||
if (block.kind === 'text') return <p key={i}>{render(block.content)}</p>
|
||||
const Heading = `h${block.level}` as 'h1'
|
||||
return <Heading key={i}>{render(block.content)}</Heading>
|
||||
})}
|
||||
</article>
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue