Skip to content

[useDismiss] Bug: MacOS Safari and Firefox minimize when closing popups via ESC key - #4695

Merged
atomiks merged 2 commits into
mui:masterfrom
arturbien:fix/escape-key-prevent-default
Apr 27, 2026
Merged

atomiks merged 2 commits into
mui:masterfrom
arturbien:fix/escape-key-prevent-default

Conversation

@arturbien

@arturbien arturbien commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In MacOS Safari and Firefox, when the browser window is in fullscreen (maximized) mode, pressing Escape while a popup is open (popover, dropdown menu, select, dialog, etc.) exits the maximized/fullscreen mode which leads to a really poor UX for a very common user behavior.

BEFORE:

Screen.Recording.2026-04-27.at.20.39.08.mov
Screen.Recording.2026-04-27.at.20.57.41.mov

AFTER:

Screen.Recording.2026-04-27.at.20.39.33.mov
Screen.Recording.2026-04-27.at.20.58.19.mov

Root cause

The closeOnEscapeKeyDown handler in useDismiss was calling event.stopPropagation() but not event.preventDefault(). While stopPropagation prevents other JavaScript event listeners from seeing the event, only preventDefault prevents the browser's native behavior for that key. Safari's and Firefox native Escape behavior in fullscreen is to exit fullscreen, and without preventDefault, that fires before (or instead of) the popup closing.

Fix

Added event.preventDefault() to the Escape keydown handler in useDismiss, gated on !eventDetails.isCanceled — so if a consumer calls cancel() in their onOpenChange callback, the browser's default Escape behavior is preserved.

Prior art

Radix UI had the exact same bug and fix:

Test plan

  • Added test verifying event.defaultPrevented is true after Escape dismisses a floating element.
  • Added test verifying event.defaultPrevented remains false when the consumer cancels the close via cancel().
  • All 47 existing useDismiss tests continue to pass.

Manual verification

  1. Open any Base UI popup component (Popover, Menu, Select, Dialog) in Safari
  2. Enter fullscreen mode (Globe + F or green button)
  3. Open the popup
  4. Press Escape
  5. Before fix: Safari and Firefox exits fullscreen and popup closes
  6. After fix: Popup closes; Safari stays in fullscreen

@atomiks lmk if there's anything misssing 🫡

In Safari, pressing Escape while the window is fullscreen exits
fullscreen instead of closing the popup, because the handler only
called stopPropagation without preventDefault. This adds
preventDefault to the Escape keydown handler so the browser does not
process the key after the popup has been dismissed.

Made-with: Cursor
@pkg-pr-new

pkg-pr-new Bot commented Apr 27, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4695
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4695
    

commit: c4f500d

@code-infra-dashboard

code-infra-dashboard Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+33B(+0.01%) 🔺+17B(+0.01%)

Details of bundle changes

Performance

Total duration: 1,337.60 ms ▼-75.71 ms(-5.4%) | Renders: 53 (+0) | Paint: 2,072.63 ms ▼-98.09 ms(-4.5%)

Test Duration Renders
Menu mount (300 instances) 163.83 ms 🔺+7.04 ms(+4.5%) 2 (+0)
Tabs mount (200 instances) 263.97 ms 🔺+3.89 ms(+1.5%) 4 (+0)
Mixed surface mount (app-like density) 95.30 ms 🔺+2.02 ms(+2.2%) 5 (+0)
Popover mount (300 instances) 101.89 ms ▼-25.48 ms(-20.0%) 2 (+0)
Select mount (200 instances) 147.97 ms ▼-16.50 ms(-10.0%) 3 (+0)

...and 7 more. View full report

Details of benchmark changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c4f500d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69efb06f716ae2000867d62f
😎 Deploy Preview https://deploy-preview-4695--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@arturbien arturbien changed the title [useDismiss] Prevent browser default on Escape key dismiss [useDismiss] MacOS: Safari and Firefox minimize when closing popups via ESC key Apr 27, 2026
Made-with: Cursor
@arturbien
arturbien marked this pull request as ready for review April 27, 2026 19:01
@arturbien arturbien changed the title [useDismiss] MacOS: Safari and Firefox minimize when closing popups via ESC key [useDismiss] Bug: MacOS Safari and Firefox minimize when closing popups via ESC key Apr 27, 2026
@atomiks atomiks added scope: all components Widespread work has an impact on almost all components. type: bug It doesn't behave as expected. labels Apr 27, 2026

@atomiks atomiks 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.

Thanks for the fix @arturbien

@atomiks
atomiks merged commit 774cf66 into mui:master Apr 27, 2026
8 of 9 checks passed
@arturbien

Copy link
Copy Markdown
Contributor Author

@atomiks thanks for lightning fast review and merge 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: all components Widespread work has an impact on almost all components. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants