Skip to content

[menu] Fix submenu stuck glitch - #3783

Merged
atomiks merged 1 commit into
mui:masterfrom
atomiks:fix/menu-submenu-glitches
Jan 20, 2026
Merged

atomiks merged 1 commit into
mui:masterfrom
atomiks:fix/menu-submenu-glitches

Conversation

@atomiks

@atomiks atomiks commented Jan 18, 2026 •

Copy link
Copy Markdown
Contributor

The allowMouseEnter condition is part of the useHoverReferenceInteraction enabled state in <Menu.Trigger>. This can undesirably switch to false while hovering submenu triggers, which causes the event listener registration effect to cleanup then re-execute. During this phase (high CPU load/many submenu triggers makes it more obvious), mouseleave may not fire since the event listener is temporarily not registered - this prevents the mousemove listener for safePolygon() from registering, leaving pointer-events: none; stuck since it gets cleaned up/removed by that module.

Fixes #3774. The performance issue seems ~related to this but performance is somewhat subjective here as to how it should feel, at least without comparison with other libraries

https://deploy-preview-3783--base-ui.netlify.app/experiments/menu/menu-submenus

@atomiks atomiks added component: menu Changes related to the menu component. type: regression A bug, but worse, it used to behave as expected. labels Jan 18, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jan 18, 2026 •

Copy link
Copy Markdown
  • vite-css-base-ui-example

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

commit: c42b91b

@mui-bot

mui-bot commented Jan 18, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react ▼-333B(-0.08%) ▼-73B(-0.05%)

Details of bundle changes


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

@netlify

netlify Bot commented Jan 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit c42b91b
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/696f7060fc2353000816426a
😎 Deploy Preview https://deploy-preview-3783--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.

@atomiks
atomiks force-pushed the fix/menu-submenu-glitches branch 2 times, most recently from e205047 to 6a89a8a Compare January 18, 2026 07:47
@atomiks
atomiks force-pushed the fix/menu-submenu-glitches branch 3 times, most recently from a56e0e0 to 111ad3c Compare January 19, 2026 04:58
@atomiks
atomiks marked this pull request as ready for review January 19, 2026 05:08
@greptile-apps

greptile-apps Bot commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a race condition that caused submenus to get stuck with pointer-events: none; when hovering across multiple submenu triggers.

Root Cause

The allowMouseEnter flag was included in the enabled condition of useHoverReferenceInteraction. When this flag toggled during hover interactions, it caused the hook's effect to cleanup and re-execute. During this brief window, the mouseleave event listener was unregistered, which could prevent the safePolygon() mousemove handler from being set up. This left pointer-events: none; stuck on the body element.

Solution

  • MenuSubmenuTrigger: Changed to read allowMouseEnter from parent menu store instead of submenu store, and moved it from the enabled condition to the delay parameter (lines 115, 118, 123). This keeps event listeners stable while still controlling hover behavior.
  • MenuStore: Removed the parent-child sync logic for allowMouseEnter (removed selector propagation and observer), and changed the initial value from true to false (line 171).
  • Test Update: Improved test to use userEvent.hover() and waitFor() for more realistic async testing.
  • Experiment Page: Added stress-test page with 50 submenus to verify the fix under high load.

The fix maintains the same hover behavior while preventing the event listener registration race condition.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk.
  • The fix is well-targeted and addresses a specific race condition without changing the overall architecture. The changes are minimal, focused, and include both a test update and a stress-test experiment page. The logic change (reading allowMouseEnter from parent store and using it for delay instead of enabled state) correctly prevents the event listener cleanup race while preserving the intended behavior.
  • No files require special attention

Important Files Changed

Filename Overview
packages/react/src/menu/submenu-trigger/MenuSubmenuTrigger.tsx Fixed allowMouseEnter condition to prevent event listener cleanup race condition. Now reads from parent store and controls delay instead of enabled state.
packages/react/src/menu/store/MenuStore.ts Removed parent-child allowMouseEnter sync logic and changed initial value from true to false. Selector no longer propagates from parent.
packages/react/src/menu/root/MenuRoot.test.tsx Updated test to use userEvent.hover() and waitFor() instead of fireEvent for more realistic async hover behavior testing.
docs/src/app/(private)/experiments/menu/menu-submenus.tsx Added new experiment page with 50 submenus (20 items each) to reproduce and test the performance issue and stuck submenu glitch.

@michaldudak

Copy link
Copy Markdown
Member

I updated the playground to have an additional level of menu nesting and was able to break it a few times. I can't reliably reproduce it, though. I was randomly hovering in and out, and a couple of times the menu didn't close.

Screen.Recording.2026-01-19.at.20.45.32.mov

I also noticed that sometimes an error appears:
flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task.
I was able to reproduce it on master as well, though.

@atomiks
atomiks force-pushed the fix/menu-submenu-glitches branch from 111ad3c to baa2a56 Compare January 20, 2026 00:29
@atomiks

atomiks commented Jan 20, 2026

Copy link
Copy Markdown
Contributor Author

@michaldudak I was able to reproduce the flushSync error (fixed), but I couldn't get a stuck submenu stuck at all, updating to 3 levels of nesting. Previously, it was happening quite frequently

If you're able to reproduce & debug, that'd be helpful, otherwise this at least fixes the vast majority of the issue in the common cases

@michaldudak

Copy link
Copy Markdown
Member

I can't reproduce it reliably, unfortunately. It happens quite rarely. I'm OK with leaving it as is in this PR.

@atomiks
atomiks force-pushed the fix/menu-submenu-glitches branch from baa2a56 to c42b91b Compare January 20, 2026 12:09
@atomiks
atomiks merged commit 7e2813b into mui:master Jan 20, 2026
@atomiks
atomiks deleted the fix/menu-submenu-glitches branch January 20, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: menu Changes related to the menu component. type: regression A bug, but worse, it used to behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[menu] Performance issues and bugs in sub-menus

3 participants