Skip to content

fix(core): focus trap DOM-depth Escape, aria-hidden exclusion, live-region clearing - #4375

Merged
cixzhang merged 1 commit into
meta:mainfrom
bhamodi:a11y/focus-trap-hardening
Jul 30, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
bhamodi:a11y/focus-trap-hardening

Conversation

@bhamodi

@bhamodi bhamodi commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Three hardening fixes to the shared a11y hooks that back every overlay in the system (Dialog, Popover, menus, MobileNav), from a11y scan #3, plus a doc-only WCAG note on useHotkeys:

  • useFocusTrap — Escape resolved by push order, not DOM depth. React runs child effects before parent effects, so when an outer trap (e.g. a Dialog) and a trap nested inside it (e.g. a Popover) mount in the same commit, the inner trap pushes onto the Escape stack first and the outer trap wrongly won the pure last-pushed comparison — Escape closed the outer layer while the inner one stayed open.
  • useFocusTrap — trap membership included aria-hidden subtrees (WCAG 4.1.2). A focusable element inside [aria-hidden="true"] was a Tab target in the trap cycle while assistive tech skips it, so keyboard focus could land on content that is invisible to AT.
  • useAnnounce — live regions were never cleared. The polite/assertive regions retained the last message indefinitely, leaving stale status text in the accessibility tree for users browsing the DOM later.

Changes

  • useFocusTrap.ts: the Escape stack now stores {handler, getContainer} entries. isTopEscapeHandler walks the stack in push order and keeps the DOM-deepest container as the top (if entry A's container contains entry B's, B is topmost), with push order as the tiebreaker for non-nested traps. Containers are read lazily at keydown time. hasActiveFocusTrapEscape() semantics are unchanged (Dialog still uses it for Escape deferral).
  • useFocusTrap.ts: isVisiblyFocusable now rejects elements matching or inside [aria-hidden="true"] via closest(), alongside the existing inert/hidden/computed-style checks. Runs per-Tab only.
  • useAnnounce.ts: after each announce, a per-region timeout (2s, reset on every announce) clears the region text. Explicit clearRegion and the test reset helper cancel pending timers. The clear-then-rAF re-announcement pattern is untouched; the delay comfortably outlasts the rAF re-set and test assertion windows.
  • useHotkeys.ts: JSDoc note documenting the WCAG 2.1.4 (Character Key Shortcuts) obligation for unmodified single-character shortcuts — must be remappable, disableable, or focus-scoped. No behavior change.

Test plan

  • New tests (all confirmed failing before the fixes, 6 failures pre-fix):
    • same-commit nested trap mount: outer + inner rendered together, Escape fires the INNER trap's onEscape only; after the inner unmounts, Escape falls back to the outer
    • focusable elements inside (or carrying) aria-hidden="true" are excluded from focusFirst and from the Tab wrap boundary
    • live region content survives ~1s, clears after the delay (fake timers), the clear timer resets on each announce, and re-announcement works after a clear
  • node_modules/.bin/vitest run packages/core/src/hooks --reporter=dot — 14 files, 183 tests passed
  • Full core suite (critical here — overlays + every announce consumer: CommandPalette, Toast, Selector, Calendar, Typeahead, etc.): node_modules/.bin/vitest run packages/core --reporter=dot — 222 files, 5165 tests, all passed (no announce-timing fallout, known Calendar/Typeahead flakes did not reproduce)
  • node_modules/.bin/tsc --project packages/core/tsconfig.json --noEmit — clean
  • eslint + prettier --check on changed files — clean
  • node scripts/check-changesets.mjs — 45 changesets valid

Notes

  • Vercel deployment failure on fork PRs is known infra and unrelated to this change.

@vercel

vercel Bot commented Jul 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview, Comment Jul 26, 2026 4:57am

Request Review

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Jul 26, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 0B

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | View full report

@cixzhang

Copy link
Copy Markdown
Contributor

It's crazy how simple announcements for a11y needs this complicated song and dance managing live regions.

Thanks for handling these details.

@cixzhang
cixzhang enabled auto-merge (squash) July 30, 2026 06:15
@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Jul 30, 2026
@cixzhang
cixzhang merged commit f7bce66 into meta:main Jul 30, 2026
20 checks passed
@github-actions
github-actions Bot deleted the a11y/focus-trap-hardening branch July 30, 2026 08:11

This branch was successfully deployed

1 active deployment
Preview — db22f2ed Deployed Jul 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants