Skip to content

Media Editor: Prevent shortcuts modal Escape from opening discard dialog - #81775

Closed
yogeshbhutkar wants to merge 2 commits into
WordPress:trunkfrom
yogeshbhutkar:fix/media-editor-key-handler
Closed

yogeshbhutkar wants to merge 2 commits into
WordPress:trunkfrom
yogeshbhutkar:fix/media-editor-key-handler

Conversation

@yogeshbhutkar

Copy link
Copy Markdown
Contributor

What, Why, and How?

Closes #81774

Key events from portaled Media Editor dialogs could also reach the editor behind them. This caused Escape in Keyboard shortcuts to open the discard dialog. The editor's key handler now scopes key events whose targets are within its DOM subtree.

Testing Instructions

  1. Create a new post.
  2. Insert an Image block and select an image from the Media Library.
  3. Select the image and click Crop in the block toolbar.
  4. Edit the image to create unsaved changes.
  5. Open Keyboard shortcuts from the Media Editor header.
  6. Press Escape.
  7. Verify that only the Keyboard shortcuts dialog closes and the discard confirmation does not appear.

Screencast

PR.mov

Use of AI Tools

AI Usage: Yes
Model: GPT-5.6 Sol with Codex
Role: Used for scoping the issue and internal code review.

@yogeshbhutkar
yogeshbhutkar force-pushed the fix/media-editor-key-handler branch from 45f3cfb to 961525e Compare August 18, 2026 07:26
@yogeshbhutkar yogeshbhutkar self-assigned this Aug 18, 2026
@yogeshbhutkar yogeshbhutkar added [Type] Bug An existing feature does not function as intended [Feature] Media Anything that impacts the experience of managing media labels Aug 18, 2026
@yogeshbhutkar
yogeshbhutkar marked this pull request as ready for review August 18, 2026 07:35
@yogeshbhutkar
yogeshbhutkar requested a review from ramonjd August 18, 2026 07:35
@github-actions

github-actions Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: hbhalodia <hbhalodia@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@hbhalodia hbhalodia left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! and works well.

Screen.Recording.2026-08-18.at.1.35.12.PM.mov

Can we add the changelog entry? I see we are maintaining that.

Comment on lines 544 to 555
};

const handleKeyDown = ( event: ReactKeyboardEvent< HTMLElement > ) => {
// Ignore events from nested components rendered outside this modal's DOM,
// such as portaled dialogs.
if ( ! event.currentTarget.contains( event.target as Node ) ) {
return;
}

const isUndoShortcut = isKeyboardEvent.primary( event, 'z' );
const isRedoShortcut =
isKeyboardEvent.primaryShift( event, 'z' ) ||

@im3dabasia im3dabasia Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix, confirmed it resolves the issue locally.

One question on the approach: would event.defaultPrevented work here instead?

Modal already guards its own Escape handling that way (handleEscapeKeyDown), and since the nested shortcuts modal calls preventDefault() before this handler runs, the keystroke is already flagged as consumed by the time it gets here.

#78433 solves a similar issue, might be a useful reference.

@yogeshbhutkar

Copy link
Copy Markdown
Contributor Author

Closing in favor of #81785

@yogeshbhutkar
yogeshbhutkar deleted the fix/media-editor-key-handler branch August 18, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Media Anything that impacts the experience of managing media [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Media Editor: Escape in keyboard shortcuts modal also opens discard confirmation dialog

3 participants