Skip to content

[menu][popover] Keep the trigger mounted on React 17 - #6359

Draft
LukasTy wants to merge 2 commits into
mui:masterfrom
LukasTy:fix/react17-trigger-key
Draft

LukasTy wants to merge 2 commits into
mui:masterfrom
LukasTy:fix/react17-trigger-key

Conversation

@LukasTy

@LukasTy LukasTy commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

On React 17, useId returns undefined on the first render. It sets the id in an effect and forces a rerender. Menu.Trigger and Popover.Trigger used this id as the key of the Fragment that wraps the trigger element (added in #3255). The key changes from undefined to base-ui-N, so React mounts a new trigger DOM node. The hover listeners stay on the old, detached node, and hover does not open the popup. A static id prop on the trigger hides the bug.

The same remount also occurs on all React versions when the id prop of the trigger changes.

Fix

  • Render the trigger element and the conditional focus guards in one Fragment, like FloatingFocusManager. The element keeps its position, so React keeps its DOM node without a key, also when the focus guards mount.
  • The other triggers (Tooltip, PreviewCard, NavigationMenu, Select, Combobox, Dialog, AlertDialog, Drawer, ContextMenu, Menubar) have no keys from generated ids.

Tests

  • New shared tests in popupConformanceTests (all popup roots): the trigger keeps the same DOM node from the first commit through open and close, and when the id prop changes.
  • New MenuTrigger.react17.test.tsx and PopoverTrigger.react17.test.tsx mock React.useId away, like MenuFilterRoot.react17.test.tsx. So the default CI runs the React 17 id fallback. Hover opens the popup on mount and after hydration.
  • The Combobox suite config now spreads props.trigger on Combobox.Input, like the other configs.
  • Without the fix, the four hover tests and the id change test fail for Menu and Popover on React 19. If the closed trigger returns the element alone, the open and close test also fails on React 19.

Test results

React 19 (default):

  • pnpm test:jsdom src/menu/ src/popover/ Root.test: 80 files pass (4021 tests).
  • pnpm test:chromium src/menu/ src/popover/ Root.test: 84 files pass (4877 tests).

React 17 (Chromium only, CI does not run React 17):

  • pnpm test:chromium src/menu/ src/popover/: 76 failures before the fix, 26 after. Most of the fixed tests are hover tests, for example "opens when the trigger is hovered and closes when it is no longer hovered".
  • The 26 remaining failures are React 17 test environment differences, not trigger remounts: act() warnings, React.startTransition in test code, synchronous commits, and flaky MenuFilterSubmenuRoot keyboard tests (they are also flaky without the fix).
  • The new conformance tests pass for all 8 popup roots, and the new hover tests pass for Menu and Popover.

To run the tests on React 17 locally:

  1. Run pnpm code-infra set-version-overrides --pkg react@17 (the same command as pnpm use-react-18).
  2. Patch packages/react/test/createRenderer.ts locally. On React 17, act() does not return the callback result, so render() returns undefined. Assign the result inside the act() callback.
  3. Use Chromium. In jsdom, Node ESM cannot resolve react/jsx-runtime from @mui/internal-test-utils on React 17.

Related: mui/material-ui#48823 (the Menu2 tests on React 17 use a static id as a workaround).

Generated with Claude Code

On React 17, the generated trigger id is undefined on the first render and
is set in an effect. The Menu and Popover triggers used this id as the key
of the Fragment that wraps the element, so React mounted a new DOM node.
The hover listeners stayed on the old node, and hover did not open the popup.

Use constant keys. The keys are local to the trigger, so they cannot collide
between triggers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LukasTy LukasTy added component: menu Changes related to the menu component. type: bug It doesn't behave as expected. component: popover Changes related to the popover component. labels Nov 26, 2026
@pkg-pr-new

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

commit: 1c23879

@code-infra-dashboard

code-infra-dashboard Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react ▼-128B(-0.02%) ▼-52B(-0.03%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · precise to ±11.1% (±16.8% at worst) · ran 3m 54s · details


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

@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 1c23879
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6b0e8c2964ee4fad7f5673d9
😎 Deploy Preview https://deploy-preview-6359--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.

Render the focus guards conditionally around the trigger element, so React keeps the element at the same position without a key. Add tests that mock `React.useId` away and check that hover opens Menu and Popover on mount and after hydration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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. component: popover Changes related to the popover component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant