Skip to content

fix(overlays): topmost-only Escape dismissal + IME composition guard - #3357

Merged
cixzhang merged 1 commit into
mainfrom
navi/fix/escape-stack-ime-guard
Jul 2, 2026
Merged

cixzhang merged 1 commit into
mainfrom
navi/fix/escape-stack-ime-guard

Conversation

@cixzhang

@cixzhang cixzhang commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Part of the accessibility & keyboard-management program (#3343). Fixes findings overlays-2, overlays-3, and infra-2.

Problem

One Escape closed everything. Every active useFocusTrap attached its own unconditional document-level keydown listener, and Dialog added a separate one. So a single Escape press dismissed every open layer at once — e.g. a popover or menu opened inside a Dialog closed both the popover and the dialog.

IME users lost their overlay. There was no composition guard on any Escape handler. A CJK/IME user pressing Escape to cancel an in-progress composition inside a Dialog or popover closed the whole overlay (and their draft).

Fix

  • Topmost-only dismissal. useFocusTrap now registers each active trap on a shared module-level stack. Only the most recently activated (top-most) trap responds to Escape; its handler preventDefault()s and stopPropagation()s so an outer layer does not also dismiss on the same press.
  • Dialog coordination. Dialog consults hasActiveFocusTrapEscape() and defers to any popover/menu layered on top of it (in both the keydown and native cancel paths), so the popover closes first and the dialog stays open.
  • IME guard. A shared isImeKeyEvent() helper (isComposing / keyCode === 229) is applied in both useFocusTrap and Dialog, so Escape during composition is ignored.

The two new helpers (hasActiveFocusTrapEscape, isImeKeyEvent) are internal cross-module utilities exported from the hook file and consumed by Dialog; the public useFocusTrap API is unchanged.

Tests

New direct unit tests for useFocusTrap (the hook previously had zero):

  • single active trap responds to Escape;
  • with two active traps, only the top-most responds;
  • Escape during IME composition (isComposing and keyCode 229) is ignored, a normal Escape still works;
  • a deactivated trap no longer responds.

Full Dialog, Popover, and CommandPalette suites pass (105 tests); core typecheck clean.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 2, 2026
@vercel

vercel Bot commented Jul 2, 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 2, 2026 6:49pm

Request Review

@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

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

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

useFocusTrap attached an unconditional document-level Escape listener per
active trap, and Dialog added its own, so one Escape press closed every open
layer (a popover nested in a Dialog closed both). CJK/IME users pressing
Escape to cancel a composition also closed the overlay.

Track active focus traps in a shared module-level stack so only the top-most
trap responds to Escape; that handler now preventDefaults and stopPropagations
so an outer Dialog does not also dismiss. Dialog defers to any active popover
trap (hasActiveFocusTrapEscape) for topmost-only behavior, and both the trap
and Dialog ignore Escape while an IME composition is in progress
(isComposing / keyCode 229).
@cixzhang
cixzhang force-pushed the navi/fix/escape-stack-ime-guard branch from 5aa3e28 to a9bb486 Compare July 2, 2026 18:45
@cixzhang
cixzhang enabled auto-merge (squash) July 2, 2026 19:01
@cixzhang
cixzhang merged commit 8796e0f into main Jul 2, 2026
15 checks passed
github-actions Bot added a commit that referenced this pull request Jul 2, 2026
@github-actions
github-actions Bot deleted the navi/fix/escape-stack-ime-guard branch July 12, 2026 06:58

This branch was successfully deployed

1 active deployment
Preview — a9bb486a Deployed Jul 2, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants