Repository navigation
fix(contextmenu): reliable Escape dismissal + focus restore - #3361
Merged
Merged
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR No new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
cixzhang
marked this pull request as ready for review
July 2, 2026 15:16
cixzhang
requested review from
ejhammond,
imdreamrunner,
josephfarina,
marie-lucas,
nynexman4464 and
thedjpetersen
as code owners
July 2, 2026 15:16
josephfarina
approved these changes
Jul 2, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the accessibility & keyboard-management program (#3343). Fixes findings
menus-2andmenus-3.Problem
Escape didn't work when the menu opened without auto-focus. ContextMenu's only Escape handler was
onKeyDownon the menu<div>, which only fires when focus is inside the menu. Menus opened withhasAutoFocus={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
keydownlistener closes it on Escape regardless of where focus is. It is guarded against IME composition-cancel (isComposing/keyCode === 229).handleContextMenuand restored in the layer's hide path (only if it is still in the document).Tests
hasAutoFocus={false}(document-level path).