Skip to content

[tooltip] Close when scroll moves the trigger away from the cursor - #49815

Closed
YehudaMenahem wants to merge 2 commits into
mui:masterfrom
YehudaMenahem:cursor/tooltip-close-on-scroll-66fb
Closed

YehudaMenahem wants to merge 2 commits into
mui:masterfrom
YehudaMenahem:cursor/tooltip-close-on-scroll-66fb

Conversation

@YehudaMenahem

Copy link
Copy Markdown

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.

Co-authored-by: Yehuda Menahem <jmenahem@gmail.com>
@code-infra-dashboard

code-infra-dashboard Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-49815--material-ui.netlify.app/
QR code for https://deploy-preview-49815--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+561B(+0.10%) 🔺+179B(+0.11%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

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

@zannager zannager added the scope: tooltip Changes related to the tooltip. label Nov 17, 2026
@ZeeshanTamboli

Copy link
Copy Markdown
Member

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

@aarongarciah

Copy link
Copy Markdown
Member

@ZeeshanTamboli, not for v10 but a future version.

@ZeeshanTamboli ZeeshanTamboli changed the title [Tooltip] Close when scroll moves the trigger away from the cursor [tooltip] Close when scroll moves the trigger away from the cursor Nov 27, 2026
@aarongarciah

Copy link
Copy Markdown
Member

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

  1. Hide the tooltip when its trigger is clipped by a scroll container or scrolled off screen. This covers the table and Data Grid case:
.MuiTooltip-popper[data-popper-reference-hidden] {
  visibility: hidden;
  pointer-events: none;
}
  1. Close the tooltip as soon as a scroll moves its trigger, with a drop-in wrapper for uncontrolled tooltips:
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 @mui/material 9.6.0 in Chromium.

@YehudaMenahem

Copy link
Copy Markdown
Author

Thanks for the thorough investigation and workarounds, Aaron. I'll follow base-ui#1447.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tooltip Changes related to the tooltip.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Tooltip] Stays visible when scrolling away

4 participants