base-ui icon indicating copy to clipboard operation
base-ui copied to clipboard

[popover] Popover becomes unanchored when trigger is invisible

Open ryanleichty opened this issue 6 months ago • 4 comments

Bug report

Current behavior

When a popover trigger becomes invisible (via display: none or anything else that would return false via triggerElement.checkVisibility()), the popover becomes unanchored.

Image

Expected behavior

I would expect the popover to close.

Reproducible example

https://base-ui.com/react/components/popover

Open the popover, set the button display property to none (in the browser)

Base UI version

Tested in the browser in the docs

Which browser are you using?

Chrome

Which OS are you using?

Mac OS

Which assistive tech are you using (if applicable)?

None

Additional context

I'm actually implementing a popover in a non-React context and was trying to follow a similar pattern as Base UI (also using Floating UI under the hood) and ran into the same issue where I was hiding the popover trigger on desktop, causing the popover to become unanchored. I recently stumbled upon checkVisibility() and used it to solve the problem by running it in the autoUpdate function from Floating UI.

I was mainly curious to see how you might've handled this scenario.

ryanleichty avatar Jun 17 '25 04:06 ryanleichty

I recently stumbled upon checkVisibility()

Ohh I did not know about this 👀 maybe it could be used for https://github.com/mui/base-ui/pull/1907 as well @atomiks

mj12albert avatar Jun 17 '25 09:06 mj12albert

Currently, data-anchor-hidden only checks clipping but this check (checkVisibility) could be added for that attribute for the display: none case. That would allow you to hide the popup when the anchor is hidden in this scenario

atomiks avatar Jun 17 '25 10:06 atomiks

Support isn't the greatest in Safari since it's fairly new (Safari 17.4) so I created a basic polyfill to be on the safe side. Otherwise, it's fairly helpful

ryanleichty avatar Jun 17 '25 16:06 ryanleichty

Raised a PR for above issue: https://github.com/mui/base-ui/pull/2166

piyushzala158 avatar Jun 25 '25 14:06 piyushzala158