Repository navigation
[menu] Fix duplicate onOpenChange calls when closing a submenu - #5178
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,371.31 ms -8.66 ms(-0.6%) | Renders: 78 (+0) | Paint: 2,116.49 ms -18.95 ms(-0.9%)
13 tests within noise — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
flaviendelangle
left a comment
There was a problem hiding this comment.
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
dee40fd to
d01cca4
Compare
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):closeevent emitted by the item press.onParentCloserelay inMenuPositionercloses the already-closed submenu again. The redundancy guard insetOpendoesn't catch it because the relayed event arrives in the same tick as the first close (the render-closureopenis stilltrue), and itstriggeris compared againstactiveTriggerElementbefore the trigger normalization runs.trigger-hoverclose from a pending hover timer, which passes the guard because its reason differs from the last one.Fix
setOpennow 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 theopenselector resolves toopenProp ?? 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
onOpenChangereceives exactly onefalsecall in the nested item-click scenario; it fails 2/2 (contained + detached triggers) without the fix. The item click is dispatched withfireEventbecause the synthetic pointer teleport ofuser.clickcloses the submenu via hover before the click lands.