Skip to content

fix(menu-dropdown): close the open dropdown when another one opens - #3265

Merged
interactivellama merged 1 commit into
salesforce:masterfrom
zigzagdev:fix/menu-dropdown-single-open
Nov 2, 2026
Merged

interactivellama merged 1 commit into
salesforce:masterfrom
zigzagdev:fix/menu-dropdown-single-open

Conversation

@zigzagdev

@zigzagdev zigzagdev commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Additional description

Summary

Opening a MenuDropdown now closes any other MenuDropdown that is already open, so two menus can no longer stay open at the same time.

Motivation

MenuDropdown keeps track of the open dropdown in a module level variable, so that opening another one can close it.
This stopped working after the rewrite.
With a click dropdown open, hovering a dropdown that uses openOn="hover" opens the second menu while the first one stays open.
Clicking a second dropdown hides the problem, because the first dropdown's outside click handler closes it on mousedown.
Hovering does not fire mousedown, so nothing closes the first menu.

What I have done

  • handleOpen stored { handleClose }, the callback from the render before the menu opened. Inside that closure getIsOpen() still returned false, so when another dropdown called it later it did nothing. This is the root cause.
  • The checks that clear the variable compared it against handleCloseRef.current. That ref is replaced on every render, so after the menu opened they never matched and the variable kept a stale callback.
  • Each instance now creates one stable entry, openDropdownEntryRef, whose handleClose always calls the latest handler through handleCloseRef. That entry is what gets stored, and the close handler and the unmount cleanup compare against it.

Test Results

 ✓ |unit| components/menu-dropdown/__tests__/dropdown.test.jsx > SLDSMenuDropdown > Only one open dropdown > closes the open dropdown when another one opens 7ms
 Test Files  1 passed (1)
      Tests  40 passed (40)

Screen Shots

before

2026-10-29.22.28.53.mov

after

2026-10-29.22.29.20.mov

CONTRIBUTOR checklist (do not remove)

Please complete for every pull request.

Green gate — all of these must pass locally (CI runs the same):

  • npm run lint passes with 0 errors (npm run lint:fix to auto-fix). Warnings are the tracked gradual-migration backlog; do not add new errors.
  • npm run typecheck is clean (tsc --noEmit).
  • npm test -- --run passes — this runs both Vitest projects (jsdom unit + real-browser browser) and the story HTML snapshots. If you changed rendered markup on purpose, update snapshots with npm test -- --run -u and review the diff.
  • npm run build succeeds (type-check + Vite library build + .d.ts + entry shims).

Behavior & docs:

  • First-time contributors: sign the CLA (a bot will comment with instructions).
  • Added/updated Vitest + React Testing Library tests for the change (template: components/button/__tests__). New props and bug fixes should get a regression test. Tests that genuinely need a real browser (focus traversal, layout/measurement, canvas) go in a *.browser.test.jsx file (the browser project); everything else is jsdom.
  • Reviewed the component's Storybook stories: npm run storybook → http://localhost:6007/. Confirm the story actually renders and behaves (deprecated Storyshots/Karma are gone; there is no separate browser-tests server).
  • Story/example code uses the component's real prop API (e.g. slots like content / visualPickerFigure, render props like renderItem, icon props, events), not assumptions — a common source of "component looks broken" reports has been stories authored against the wrong API. Cross-check against components/<name>/__examples__.
  • Storybook Accessibility (a11y) panel shows no new violations for the affected stories.
  • If you added or changed a type, remember runtime PropTypes were removed in the TS migration — types are the contract. Ship accurate .d.ts (they're generated by the build); JS consumers rely on them via editor type-acquisition.

REVIEWER checklist (do not remove)

  • CI is green (GitHub Actions CI workflow: lint, typecheck, test, build). Chromatic visual review, if configured, has been checked.
  • Change has adequate test coverage; bug fixes include a regression test.
  • Reviewed the affected Storybook stories and their a11y panels.
  • Rendered markup still conforms to SLDS — check the story HTML snapshot diff (components/__tests__/__snapshots__/story-snapshots.test.jsx.snap).
  • Public API / package surface: if exports changed, the deep-import contract still holds (@salesforce/design-system-react/components/<name> and nested paths resolve for ESM, CJS, and types). Removing/renaming a public component or export is a breaking change → feat!:/BREAKING CHANGE: and a major version bump.

MenuDropdown tracks the open dropdown in a module level variable so that
opening another one closes it. It stored the handleClose callback from
the render before opening, whose getIsOpen() still returned false, so
calling it later did nothing and two menus could stay open (for example
a click dropdown and a hover dropdown). The identity checks that clear
the variable also stopped matching after a re-render.

Store a stable entry per instance that always calls the latest
handleClose through a ref, and compare against that entry when
clearing. The pre-rewrite class stored the instance itself. Add a
regression test.
@interactivellama
interactivellama merged commit ab24f8b into salesforce:master Nov 2, 2026
7 checks passed
@zigzagdev
zigzagdev deleted the fix/menu-dropdown-single-open branch November 2, 2026 08:35
Niznikr added a commit that referenced this pull request Nov 4, 2026
Conflicts came from #3268 (forwardRef, className on the root), #3266
(Input counter), #3270 (Carousel autoplay) and #3265 (MenuDropdown
open registration).

- Slider, VerticalNavigation, Popover, Tree: keep master's forwardRef,
  root className and classNameList/styleList, with the controlled
  value and selection from this branch.
- Input, Carousel, MenuDropdown: keep this branch's implementation,
  which already fixes the same stale closures through refs. Master's
  regression tests are kept; the uncontrolled Input maxValue test is
  rewritten for a controlled counter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants