Skip to content

[popups] Fix nested hoverable popups - #3798

Merged
flaviendelangle merged 9 commits into
mui:masterfrom
flaviendelangle:nested-preview-card
Feb 13, 2026
Merged

flaviendelangle merged 9 commits into
mui:masterfrom
flaviendelangle:nested-preview-card

Conversation

@flaviendelangle

@flaviendelangle flaviendelangle commented Jan 20, 2026 •

Copy link
Copy Markdown
Member

Atempt to fix #3673

The hardest part was to now close the root Preview Card when hovering through its children Preview Card

Preview: https://deploy-preview-3798--base-ui.netlify.app/experiments/preview-card/nested

@flaviendelangle flaviendelangle self-assigned this Jan 20, 2026
@flaviendelangle flaviendelangle added type: bug It doesn't behave as expected. component: preview card Changes related to the preview card component. labels Jan 20, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jan 20, 2026 •

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

  • vite-css-base-ui-example

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

commit: 4243104

@mui-bot

mui-bot commented Jan 20, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+146B(+0.03%) 🔺+20B(+0.01%)

Details of bundle changes


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

@netlify

netlify Bot commented Jan 20, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 4243104
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/698eda81917a860008a7501d
😎 Deploy Preview https://deploy-preview-3798--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.

@flaviendelangle
flaviendelangle marked this pull request as ready for review January 20, 2026 13:36
@greptile-apps

greptile-apps Bot commented Jan 20, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR successfully implements support for nested preview cards by integrating FloatingTree support, following the same pattern already established in the Popover component. The implementation enables preview cards to be nested within other preview cards while maintaining proper interaction behavior.

Key changes:

  • PreviewCardRoot now conditionally wraps itself in FloatingTree when not already within a preview card context
  • PreviewCardPositioner registers itself as a FloatingNode to enable tree tracking
  • useHoverFloatingInteraction modified to preserve the safePolygon handler during mouse enter events, allowing it to detect nested children and prevent premature parent closure
  • Added comprehensive test coverage for click, hover, and pointer interactions with nested cards
  • Included experiment demos for manual testing

The implementation correctly addresses the issue where hovering through nested preview cards would close parent cards. The safePolygon handler's logic (lines 147-153 in safePolygon.ts) checks for open children in the floating tree and prevents closure when appropriate.

Confidence Score: 4/5

  • Safe to merge with minor concern about hover interaction edge cases
  • The implementation follows established patterns from Popover and includes comprehensive tests. However, the modification to useHoverFloatingInteraction that removes the cleanupMouseMoveHandler() call on mouse enter could potentially leave handlers active longer than necessary, though this appears intentional for nested popup detection.
  • Pay attention to packages/react/src/floating-ui-react/hooks/useHoverFloatingInteraction.ts for potential memory or event handler cleanup issues in complex nested scenarios

Important Files Changed

Filename Overview
packages/react/src/preview-card/root/PreviewCardRoot.tsx Adds FloatingTree support for nested preview cards by wrapping the component conditionally when not already in a preview card context
packages/react/src/floating-ui-react/hooks/useHoverFloatingInteraction.ts Modified hover interaction logic to preserve safePolygon handler for nested popup detection, preventing premature parent closure
packages/react/src/preview-card/positioner/PreviewCardPositioner.tsx Registers positioner as FloatingNode with unique nodeId to enable floating tree tracking for nested scenarios
packages/react/src/preview-card/root/PreviewCardRoot.test.tsx Adds comprehensive test coverage for nested preview card interactions including click, hover, and pointer events

// The handler needs to remain active to detect when mouse leaves to
// a nested portaled popup. The safePolygon logic checks for open children
// in the floating tree and prevents closing when appropriate.
handlerRef.current?.(event);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@atomiks I think this fixes the problem.
If we clean the mouse move events when opening the nested popover / preview card, then safePolygon can't properly do its job.

Tbh I don't know the codebase enough yet to be fully confident of the fix.
My experiment of both the nested Popover AND the nested Preview Card now work at least.
And without introducing additional logic that safePolygon should already handle.

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.

Yeah that looks to be a difference between the two hooks.

useHover

    function onFloatingMouseEnter() {
      timeout.clear();
      clearPointerEvents();
    }

useHoverFloatingInteraction

    function onFloatingMouseEnter(event: MouseEvent) {
      openChangeTimeout.clear();
      clearPointerEvents();
      handlerRef.current?.(event);
      cleanupMouseMoveHandler();
    }

Though the old one still doesn't call handlerRef.current

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't get any failing test and the nested popover and preview cards still behavior correctly if I comment the handlerRef.current?.(event); line.

But that's a change that could easily not be covered by any test.
cc @michaldudak since you did the initial useHoverFloatingInteraction implementation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like the point 3 in Cisco's comment is caused by this handlerRef.current call
Are we fine removing it?

The diff is here: 7a96601

@flaviendelangle

Copy link
Copy Markdown
Member Author

@greptileai

@CiscoFran10 CiscoFran10 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.

@flaviendelangle I found a couple of issues while testing nested previews:


1. Parent trigger does not regain focus when closing with Esc

When pressing Esc, only the child preview returns focus to its trigger.
The parent trigger loses focus and does not regain it.

cursorful-video-1770002383055.mp4

2. Mouse-leave close logic is scoped per preview instead of globally

Each preview popup appears to compute its own “mouse outside” state.
This means the user must move the mouse outside of each preview to close them, even if the cursor is already outside all previews.

Video.sem.titulo.Feito.com.o.Clipchamp.1.mp4

3. Unclear but reproducible odd behavior

Steps to reproduce

  1. Hover the parent trigger to open the parent preview.
  2. Hover the child trigger to open the child preview.
  3. Move the mouse outside of all previews.
  4. Before the parent preview finishes closing (there seems to be a small close delay), move the mouse back into the parent preview.
  5. Hover the child trigger again.

Actual result
Both parent and child previews close.

Expected result
The parent preview should remain open, and the child preview should open again normally.

This feels like a race condition between delayed close timers and hover-open logic, but I’m not fully sure.

Video.sem.titulo.Feito.com.o.Clipchamp.2.mp4

@atomiks

atomiks commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

@CiscoFran10 number 1 is because preview cards don't have focus management (same as tooltip), I would say this is not a concern in practice. For number 2, this is a known issue that Navigation Menu with submenus also has - definitely needs investigating but technically a separate issue. For number 3, not sure on this one, might be the difference between useHover

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 3, 2026
@flaviendelangle

Copy link
Copy Markdown
Member Author

I'll have a look at the 3rd issue when I have some time then 👍

@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 9, 2026
@atomiks atomiks changed the title [preview card] Support nested preview cards [popups] Fix nested hoverable popups Feb 12, 2026
@atomiks atomiks added the component: popover Changes related to the popover component. label Feb 12, 2026
@atomiks

atomiks commented Feb 12, 2026 •

Copy link
Copy Markdown
Contributor

Doesn't seem like 7a96601 addressed the third point @CiscoFran10 pointed out as I can still reproduce it in the experiments link. (Edit: actually, it made it worse as the "hover-out" step is no longer needed).

Separate issue as well: traversing through the triangle area closes it unexpectedly once you land on the popup.

Screen.Recording.2026-02-12.at.11.18.46.am.mov

@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.

While not perfect this solves the openOnHover regression for Popover.

@CiscoFran10's second and third points still apply, but both of them have a decent workaround by using closeDelay={0} on the triggers, since mousemove events are typically fired for both root and child when moving outside, since the child closes instantly and more than 1 mousemove event is usually fired.

I had a prototype working to solve this robustly, but it adds a lot of code and it's not clear how worth it is over using a 0 close delay.

@flaviendelangle
flaviendelangle enabled auto-merge (squash) February 13, 2026 08:03
@flaviendelangle
flaviendelangle merged commit cd8becb into mui:master Feb 13, 2026
7 checks passed
@dmitri-gb dmitri-gb mentioned this pull request Feb 26, 2026
1 task done
@flaviendelangle
flaviendelangle deleted the nested-preview-card branch August 19, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: popover Changes related to the popover component. component: preview card Changes related to the preview card component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[preview card] nested preview cards don't work

4 participants