How should native popovers behave when their anchors scroll behind sticky headers?
Asked by
letterpress_ink
I'm using the native Popover API for UI such as tooltips, comboboxes, and dropdowns. Because an open popover is rendered in the top layer, it can remain visible above a sticky header even after its trigger or anchor has scrolled underneath it. What's the best way to handle this? One idea is to observe the trigger with an IntersectionObserver, using a negative rootMargin matching the sticky header's height, then close the popover when the trigger enters that covered area or is no longer visible. Is dismissing the popover in this situation considered good practice, or should it be repositioned or temporarily hidden instead?
Answers
If you dismiss based on an intersection threshold, add a small delay or debounce so the popover doesnโt flicker when someone scrolls back and forth around the boundary. IntersectionObserver is asynchronous, so very fast scrolling may briefly leave the popover visible over the header, but thatโs usually acceptable. If you need more detailed state, a positioning library can distinguish between the reference being hidden and the popover escaping the viewport.
Reply to this answer
Using an IntersectionObserver is a reasonable solution. Treat the area behind the sticky header as unsafe, and call hidePopover() when the anchor leaves the usable content area or the viewport. Thatโs normal behavior for floating menus and tooltips, not a hack. You can also calculate the header height and expose it through a custom property if you need to adjust the popoverโs position dynamically. CSS anchor positioning may help with flipping, depending on browser support.
Reply to this answer
It helps to distinguish between closing and hiding. For a tooltip, closing as soon as its trigger is covered is usually ideal. For a combobox, closing may discard the userโs current interaction, so keeping it open while hiding or repositioning it can provide a better experience. Anchor positioning and fallback positions can move it below the header when thereโs room, with dismissal as the fallback when the anchor is completely gone.
Reply to this answer
Add your answer
Answers are reviewed before publication.
pollen-free zone: The anchor-visibility features donโt necessarily solve sticky-header overlap. They generally respond to an anchor being clipped by a scroll container or viewport, while a sticky header can simply overlap the anchor without clipping it. You may still need to account for the headerโs height yourself.