引言
无障碍不是发布前的检查项,而是组件契约的一部分。一个视觉上正确的组件,如果键盘不可操作或屏幕阅读器无法理解,就是坏的。
从语义开始
优先使用原生元素:按钮用 button,导航用 nav,标题按层级使用。原生元素自带键盘行为、焦点和角色。只有在没有合适原生元素时才使用 ARIA,并遵循“不要用 ARIA 修复语义问题”的原则。
键盘与焦点
每个交互都必须能用键盘完成:Enter/Space 激活、方向键在复合组件内移动、Escape 关闭浮层。焦点必须可见,浮层关闭后应恢复到触发元素。用焦点陷阱管理模态框,但不要困住用户。
ARIA 与测试
正确设置 role、state 和 property,并随交互更新。测试分三层:自动化工具捕获明显问题,手动键盘测试验证流程,屏幕阅读器测试确认真实朗读。还要支持 reduced motion 和高对比模式。
结论
无障碍的核心是尊重用户的操作方式和辅助技术。把语义、键盘和焦点作为组件 API 的一部分,并在文档中记录使用约束。
代码示例
<!-- Good: semantic, keyboard-operable, announced correctly. -->
<button type="button" onclick="save()">Save</button>
<!-- Bad: a div pretending to be a button. Inaccessible by default. -->
<div class="btn" onclick="save()">Save</div>
function MenuButton() {
const [open, setOpen] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
return (
<>
<button
ref={buttonRef}
aria-haspopup="menu"
aria-expanded={open}
onClick={() => setOpen(o => !o)}
onKeyDown={(e) => { if (e.key === "ArrowDown") { setOpen(true); /* move focus to first item */ } }}
>
Menu
</button>
{open && (
<ul role="menu" onKeyDown={(e) => {
if (e.key === "Escape") { setOpen(false); buttonRef.current?.focus(); }
// ArrowUp/ArrowDown move between items, Home/End to first/last
}}>
<li role="menuitem" tabIndex={-1}>Open</li>
<li role="menuitem" tabIndex={-1}>Save</li>
</ul>
)}
</>
);
}
/* A visible, high-contrast focus indicator. */
:focus-visible {
outline: 2px solid var(--color-focus);
outline-offset: 2px;
}
function Modal({ open, onClose, children, triggerRef }) {
const dialogRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (open) {
const first = dialogRef.current?.querySelector<HTMLElement>("[autofocus], button, a, input");
first?.focus(); // move focus into the modal
} else {
triggerRef.current?.focus(); // restore focus to the trigger
}
}, [open]);
return open ? (
<div ref={dialogRef} role="dialog" aria-modal="true" aria-labelledby="modal-title">
<h2 id="modal-title">Title</h2>
{children}
</div>
) : null;
}
<!-- A toggle expressed with ARIA state. -->
<button id="theme-toggle" aria-pressed="false" onclick="toggleTheme()">
Dark mode
</button>
// A Playwright test that verifies keyboard operability.
test("menu opens with ArrowDown and closes with Escape", async ({ page }) => {
await page.goto("/");
await page.keyboard.press("Tab"); // focus the trigger
await page.keyboard.press("ArrowDown"); // open the menu
await expect(page.getByRole("menuitem", { name: "Open" })).toBeFocused();
await page.keyboard.press("Escape"); // close
await expect(page.getByRole("button", { name: "Menu" })).toBeFocused();
});
<!-- A div with an onClick is in the DOM but not the accessibility tree as a control. -->
<div class="card" onclick="toggle()">Click me</div>
<!-- The button is in both, with role="button" and name="Click me", keyboard-operable by default. -->
<button class="card" onclick="toggle()">Click me</button>
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
button {
background: var(--color-primary);
color: var(--color-on-primary);
/* A border that survives forced-colors mode, where the background may be replaced. */
border: 1px solid CanvasText;
}