Skip to content

[popups] Fix nested hoverable popups - #4206

Merged
atomiks merged 5 commits into
mui:masterfrom
dmitri-gb:fix-nested-floating-containers
Mar 2, 2026
Merged

atomiks merged 5 commits into
mui:masterfrom
dmitri-gb:fix-nested-floating-containers

Conversation

@dmitri-gb

Copy link
Copy Markdown
Contributor

This fixes two issues that were not fixed in #3798.

  1. All popups suddenly closing when entering a child popup, after having briefly moved outside of the parent popup. Or similarly, all popups closing upon entering the child when the parent is defaultOpen.

  2. Popups not closing simultaneously when moving out of a child popup.

Videos of both issues can be seen in a comment on the previous PR.

I removed some code that seemed redundant. There was no reason to have two different mouseleave listeners that did very similar things. And the safePolygon "handler" is no longer disposed of so eagerly. It stays "alive" for as long as the popup is open.

I also made changes so that nested popups would continue to work even when no safePolygon "handler" was available (useHoverReferenceInteraction can just as well be called without handleClose: safePolygon()). The parent-child relationship check is just ~3 lines of code in safePolygon.ts, so there's really no need to exclusively rely on the "handler" for this logic.

Lastly, a small side-note — both of these bugs are inherited from @floating-ui/react (exact same behaviour is reproduced with simple floating-ui code), and so the older useHover hook (which I didn't touch) has these issues too.

This fixes two issues:

1. All popups suddenly closing when entering a child popup, after having
   briefly moved outside of the parent popup. Similarly, all popups
   closing upon entering the child when the parent is defaultOpen.

2. Popups not closing simultaneously when moving out of a child popup.
@pkg-pr-new

pkg-pr-new Bot commented Feb 26, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

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

commit: 056cdd5

@mui-bot

mui-bot commented Feb 26, 2026 •

Copy link
Copy Markdown

Bundle size report

ℹ️ Using snapshot from parent commit 0c5a1d5 (fallback from merge base 047eca4).

Bundle Parsed size Gzip size
@base-ui/react ▼-87B(-0.02%) ▼-29B(-0.02%)

Details of bundle changes


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

@netlify

netlify Bot commented Feb 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit fddcef9
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69a4fe2119a47c0008a0b52d
😎 Deploy Preview https://deploy-preview-4206--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 commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

The onScrollMouseLeave handler wasn't entirely redundant. Here's what it was trying to fix (people did report this as a real issue in Floating UI):

Screen.Recording.2026-02-27.at.2.36.33.pm.mov

It should close once you stop scrolling (does on master, here it doesn't). It does complicate nested popups though.

@atomiks atomiks added the scope: all components Widespread work has an impact on almost all components. label Feb 27, 2026
This is needed to handle cases where the mouse leaves the element due to
scrolling (no mousemove events get fired).

Delay closing of parent popups (when the last child is closed) to the
next tick, so that we get to process a mouseenter event that follows a
mouseleave. This matters for popups with closeDelay=0, where a child is
technically closed _before_ the mouse re-enters the parent.
@dmitri-gb

Copy link
Copy Markdown
Contributor Author

I see. I added back an invocation of instance.handler() that should take care of this issue.

@atomiks

atomiks commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Findings (Codex)

  1. [P1] mousemove safePolygon handler can survive after popup closes via non-hover paths.
    onOpenChangeLocal clears timers on close but does not clear instance.handler / document listener, so closing via Escape, outside click, or controlled open=false can leave a stale listener active. That stale callback can later call onClose() with old context and emit floating.closed, causing unexpected follow-up closes.
    Refs: packages/react/src/floating-ui-react/hooks/useHoverReferenceInteraction.ts:139, packages/react/src/floating-ui-react/hooks/useHoverFloatingInteraction.ts:115

  2. [P2] Untracked setTimeout in nested-close path can fire after teardown.
    onNodeClosed schedules a raw setTimeout(..., 0) with no cancellation on cleanup. If the component closes/unmounts before the tick, the callback can still run and emit floating.closed from stale state. (Try using the Timeout util / useTimeout util)
    Ref: packages/react/src/floating-ui-react/hooks/useHoverFloatingInteraction.ts:211

@dmitri-gb
dmitri-gb force-pushed the fix-nested-floating-containers branch from 79666fc to 5614494 Compare March 1, 2026 22:51
@dmitri-gb

dmitri-gb commented Mar 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Those two should be fixed now.

@atomiks atomiks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice, thanks for the contribution @dmitri-gb! Looks to have fixed all the issues correctly and I couldn't find any regressions

@atomiks
atomiks merged commit 336ade2 into mui:master Mar 2, 2026
6 checks passed
@atomiks atomiks added the type: bug It doesn't behave as expected. label Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: all components Widespread work has an impact on almost all components. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants