Skip to content

fix(contextmenu): reliable Escape dismissal + focus restore - #3361

Merged
cixzhang merged 1 commit into
mainfrom
navi/fix/contextmenu-escape-focus-restore
Jul 2, 2026
Merged

cixzhang merged 1 commit into
mainfrom
navi/fix/contextmenu-escape-focus-restore

Conversation

@cixzhang

@cixzhang cixzhang commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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

Problem

Escape didn't work when the menu opened without auto-focus. ContextMenu's only Escape handler was onKeyDown on the menu <div>, which only fires when focus is inside the menu. Menus opened with hasAutoFocus={false} (the way table row context menus open) never move focus into the menu, so there was no way to dismiss them with Escape at all.

Focus fell to <body> on close. The menu never captured the previously-focused element or restored it, so after Escape or an outside-click, keyboard focus was lost.

Fix

  • Document-level Escape. While the menu is open, a document-level keydown listener closes it on Escape regardless of where focus is. It is guarded against IME composition-cancel (isComposing / keyCode === 229).
  • Focus restore. The element focused before opening is captured in handleContextMenu and restored in the layer's hide path (only if it is still in the document).

Tests

  • Closes on Escape when opened with hasAutoFocus={false} (document-level path).
  • Ignores Escape during IME composition.
  • Restores focus to the trigger button on close.
  • Full ContextMenu suite (20 tests) and Table suite (318 tests, primary ContextMenu consumer) pass; core typecheck clean.

The only Escape handler lived on the menu div's onKeyDown, which never fires
when the menu is opened with hasAutoFocus={false} (as table context menus do),
so those menus could not be dismissed with Escape at all. The menu also never
restored focus on close, dropping focus to <body>.

Add a document-level Escape listener while the menu is open (guarded against
IME composition-cancel), capture the previously focused element on open, and
restore it in the layer hide path.
@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 1:44am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 2, 2026
@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

@cixzhang
cixzhang merged commit 6d03942 into main Jul 2, 2026
15 checks passed
@github-actions
github-actions Bot deleted the navi/fix/contextmenu-escape-focus-restore branch July 12, 2026 06:57

This branch was successfully deployed

1 active deployment
Preview — a3aa9a88 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.

2 participants