Skip to content

[menu] Fix duplicate onOpenChange calls when closing a submenu - #5178

Merged
atomiks merged 2 commits into
mui:masterfrom
atomiks:claude/nice-cori-fc8ee6
Jul 17, 2026
Merged

atomiks merged 2 commits into
mui:masterfrom
atomiks:claude/nice-cori-fc8ee6

Conversation

@atomiks

@atomiks atomiks commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

When a submenu item is clicked, the submenu's onOpenChange(false) currently fires three times for a single close (reproducible on published 1.6.0 and master with trusted pointer input):

  1. The legit close from the tree-wide close event emitted by the item press.
  2. A duplicate: when the root closes, the child's onParentClose relay in MenuPositioner closes the already-closed submenu again. The redundancy guard in setOpen doesn't catch it because the relayed event arrives in the same tick as the first close (the render-closure open is still true), and its trigger is compared against activeTriggerElement before the trigger normalization runs.
  3. A stale trigger-hover close from a pending hover timer, which passes the guard because its reason differs from the last one.

Fix

setOpen now ignores a close request when the menu is already closed, reading the synchronous store state (store.select('open')) rather than the render closure so same-tick relays are caught too. Since the open selector resolves to openProp ?? state.open, a controlled menu whose owner rejects a close keeps receiving close events, and the intentional same-open refire used for detached-trigger switching is unaffected (the guard only applies when the next state is closed).

The regression test asserts each of the root's and submenu's onOpenChange receives exactly one false call in the nested item-click scenario; it fails 2/2 (contained + detached triggers) without the fix. The item click is dispatched with fireEvent because the synthetic pointer teleport of user.click closes the submenu via hover before the click lands.

@atomiks atomiks added component: menu Changes related to the menu component. type: bug It doesn't behave as expected. labels Jul 7, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 7, 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@5178
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5178
    

commit: d01cca4

@code-infra-dashboard

code-infra-dashboard Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+23B(+0.01%) 🔺+6B(0.00%)

Details of bundle changes

Performance

Total duration: 1,371.31 ms -8.66 ms(-0.6%) | Renders: 78 (+0) | Paint: 2,116.49 ms -18.95 ms(-0.9%)

Test Duration Renders
Combobox type — 500 items, narrows to ~11 (type "Row 25") 34.79 ms ▼-9.03 ms(-20.6%) 17 (+0)

13 tests within noise — details


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

@netlify

netlify Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit d01cca4
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a59edd1580f1200084e5bd0
😎 Deploy Preview https://deploy-preview-5178--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@atomiks
atomiks marked this pull request as ready for review July 7, 2026 07:06

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR review

This PR extracts the three duplicated trigger slip-out boundary checks (menu, select, combobox) into a single shared isMouseWithinBounds util and widens the drift tolerance from 2px to 5px. The refactor is faithful: all three call sites already null-guard the trigger element before calling, the boundary math is unchanged apart from the intended widening, and no inline copies remain. Nothing merge-blocking.

Bugs (0)

No findings.

Tests (1)

1. ℹ️ Menu and select tolerance change covered only indirectly

Location: packages/react/src/utils/getPseudoElementBounds.test.ts:60

The behavior change (2px → 5px) applies to MenuTrigger and SelectTrigger too, but only ComboboxTrigger gained boundary integration tests. The widened math is exercised by the new shared-util unit test, so menu/select are covered at the logic level — just not at the component-interaction level.

Failure scenario: A future change to how MenuTrigger/SelectTrigger wire up handleMouseUp (e.g. passing the wrong element or dropping the call) would not be caught by a dedicated test; the shared unit test would still pass. Low risk given the parallel combobox tests.

Fix: Optional — mirror the combobox within/outside 5px tests for menu and select, or leave as-is since the shared util is directly tested.

Simplifications (0)

No findings.

Docs (0)

No findings. The new JSDoc and the tolerance comment accurately describe the util.

Verdict

Approve — clean, well-tested consolidation; the only note is an optional test-coverage parity suggestion for menu/select.


🤖 Review generated with Claude Code

@atomiks
atomiks force-pushed the claude/nice-cori-fc8ee6 branch from dee40fd to d01cca4 Compare July 17, 2026 08:54
@atomiks
atomiks merged commit 50b2f31 into mui:master Jul 17, 2026
6 checks passed
@atomiks
atomiks deleted the claude/nice-cori-fc8ee6 branch July 17, 2026 11:52
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: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants