Repository navigation
[tooltip] Close when scroll moves the trigger away from the cursor - #49815
YehudaMenahem wants to merge 2 commits into
Conversation
Co-authored-by: Yehuda Menahem <jmenahem@gmail.com>
Deploy previewBundle size
PerformanceNo regressions in 32 benchmarks · precise to ±9.6% (±19.1% at worst) · ran 3m 17s · details Check out the code infra dashboard for more information about this PR. |
|
@aarongarciah In #44548 (comment) you said this would be fixed once Material UI adopts Base UI. Is that planned for v10, now that components are being built on Base UI? |
|
@ZeeshanTamboli, not for v10 but a future version. |
|
Hey @YehudaMenahem! An update on this. Current behavior. The tooltip isn't stuck open. It doesn't listen to scroll: it closes when the browser reports that the pointer left the trigger. When the pointer is still and the page scrolls, browsers report that late. In Chrome I measured anywhere from a few milliseconds to about 2 seconds after the trigger moved away, and until then the tooltip follows the trigger. Base UI. I said above that this would probably be solved once we adopt Base UI. That isn't the case today: Base UI's Tooltip works the same way. It has no scroll handling and relies on the same pointer leave event, and I measured the same timing on both docs sites. Whether it should close as soon as the scroll starts is still open in mui/base-ui#1447. In a future version, Material UI's Tooltip will follow whatever Base UI does, so that's the issue to follow and weigh in on. We're closing this. Workarounds in the meantime
.MuiTooltip-popper[data-popper-reference-hidden] {
visibility: hidden;
pointer-events: none;
}
import * as React from 'react';
import Tooltip, { TooltipProps } from '@mui/material/Tooltip';
export default function ScrollAwareTooltip(props: TooltipProps) {
const { onOpen, onClose, ...other } = props;
const [open, setOpen] = React.useState(false);
const triggerRef = React.useRef<HTMLElement>(null);
const openedByFocusRef = React.useRef(false);
React.useEffect(() => {
if (!open) {
return undefined;
}
const handleScroll = (event: Event) => {
// Keyboard users keep the tooltip until they move focus away.
if (openedByFocusRef.current) {
return;
}
// Only react to scroll containers that actually move the trigger.
const scroller = event.target;
if (scroller instanceof Node && scroller.contains(triggerRef.current)) {
setOpen(false);
onClose?.(event);
}
};
// `scroll` doesn't bubble, capture it to catch nested scroll containers.
document.addEventListener('scroll', handleScroll, { capture: true, passive: true });
return () => {
document.removeEventListener('scroll', handleScroll, { capture: true });
};
}, [open, onClose]);
return (
<Tooltip
{...other}
ref={triggerRef}
open={open}
onOpen={(event) => {
openedByFocusRef.current = event.type === 'focus';
setOpen(true);
onOpen?.(event);
}}
onClose={(event) => {
setOpen(false);
onClose?.(event);
}}
/>
);
}The wrapper also closes the tooltip when the pointer is still over the trigger after a small scroll. It opens again on the next pointer move. Tooltips opened with the keyboard are left alone. Both tested with |
|
Thanks for the thorough investigation and workarounds, Aaron. I'll follow base-ui#1447. |
Closes #44548
Hover-opened tooltips stay open when scrolling moves the trigger out from under a still cursor, because no pointer leave event fires. While open, listen for scroll (passive, capture) and close if the last pointer position is no longer over the trigger (or an interactive tooltip). Focus-, touch-, and controlled-without-hover opens are unchanged.