Skip to content

Conversation

@seongminn
Copy link
Contributor

closed #2490

According to the MDN documentation, when the focus is on a submenu, pressing the ESC key should close the submenu and move focus to the submenu trigger.

Additionally, the Base UI documentation states that when the Esc key is pressed, the focus should return to the trigger element by default.

However, the current implementation has two issues:

  • When Esc is pressed in a submenu, the entire menu closes.
  • When closeParentOnEsc is set to false, the root menu remains open, but the focus is lost completely.

To resolve this, the following changes have been implemented:

  • The default value of closeParentOnEsc in Menu.SubmenuRoot is now false.
  • When the Esc key is pressed in a Submenu, the Submenu will close, and the focus will correctly move to the SubmenuTrigger.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 14, 2025

vite-css-base-ui-example

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

commit: 1980bfb

@mui-bot
Copy link

mui-bot commented Aug 14, 2025

Bundle size report

Bundle Parsed size Gzip size
@base-ui-components/react 🔺+71B(+0.02%) 🔺+25B(+0.03%)

Details of bundle changes

Generated by 🚫 dangerJS against 1980bfb

@netlify
Copy link

netlify bot commented Aug 14, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 1980bfb
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/68ac21b6b027b000086eeb0c
😎 Deploy Preview https://deploy-preview-2493--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.

@zannager zannager added the component: menu Changes related to the menu component. label Aug 14, 2025
Comment on lines 110 to 113
returnFocus={finalFocus || returnFocus}
returnFocus={finalTriggerElement || returnFocus}
Copy link
Contributor

@atomiks atomiks Aug 15, 2025

Choose a reason for hiding this comment

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

This prevents returnFocus from ever being passed, no?

It seems like finalTriggerElement needs to be part of the if branches above when calculating returnFocus

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Included in this commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems good now - also tested Menubar and the return focus issue this branch was addressing is still not present

Comment on lines +15 to +19
const { closeParentOnEsc = false } = props;

return (
<MenuSubmenuRootContext.Provider value>
<MenuRoot {...props} />
<MenuRoot closeParentOnEsc={closeParentOnEsc} {...props} />
Copy link
Contributor

Choose a reason for hiding this comment

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

macOS closes them all simultaneously; not sure if this should change as a default

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the MDN documentation, pressing ESC in a submenu should return focus to the parent menu or menuitem.
I've implemented this as the default behavior for submenus based on that recommendation.

I'm curious to hear your thoughts on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not too fussed either way, and am ok with changing it if this default is better suited in the web paradigm cc: @colmtuite

@oliviertassinari oliviertassinari changed the title [menu] fix closeParentOnEsc default value [menu] Dix closeParentOnEsc default value Aug 15, 2025
@oliviertassinari oliviertassinari changed the title [menu] Dix closeParentOnEsc default value [menu] Fix closeParentOnEsc default value Aug 15, 2025
@seongminn seongminn requested a review from atomiks August 17, 2025 07:44
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

LGTM. 👌
Thank you for the contribution. 🙏

@LukasTy LukasTy added type: bug It doesn't behave as expected. accessibility a11y labels Aug 25, 2025
@michaldudak michaldudak added the breaking change Introduces changes that are not backward compatible. label Aug 25, 2025
@LukasTy LukasTy merged commit f5fe448 into mui:master Aug 25, 2025
22 checks passed
atomiks pushed a commit to atomiks/base-ui that referenced this pull request Sep 10, 2025
Co-authored-by: Lukas Tyla <llukas.tyla@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accessibility a11y breaking change Introduces changes that are not backward compatible. component: menu Changes related to the menu component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[menu] Focus is lost when closeParentOnEsc: false

6 participants