Skip to content

Commit 4a04bef

Browse files
andreasheimclaude
andcommitted
[tooltip] Close when the pointer leaves after hovered trigger content is replaced
When the element under the pointer is removed (e.g. an IconButton icon swapped on hover or open), browsers don't dispatch `mouseout` for it. Chromium only fires `mouseover` on the new target with the trigger as `relatedTarget`; WebKit fires it on the document with `relatedTarget: null`. React derives `onMouseLeave` from `mouseout`, so the trigger never receives it and the tooltip stays open (the pending enterDelay timer even opens it after the pointer left). From the first hover, listen to `mouseover` on the document and remember the element the pointer was last seen over. A `mouseover` not handled by the trigger or popper, after that element was removed, means the pointer left, so run the regular leave path. Events the trigger or popper handle or deliberately ignore (disabled trigger, touch) are recorded as handled. No listener is added without a title, and it is removed on leave, whenever the tooltip closes, when `disableHoverListener` is enabled or the title is cleared, and on unmount. Also call `onClose` when the pointer leaves after `onOpen` but before `open` was updated by a render; previously a controlled tooltip stayed open in that window. The request is settled by a render after `onOpen`, so a parent that declines to open does not get an `onClose` later. Fixes mui#47073 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent cc60b55 commit 4a04bef

2 files changed

Lines changed: 692 additions & 1 deletion

File tree

‎packages/mui-material/src/Tooltip/Tooltip.js‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
256256
const [arrowRef, setArrowRef] = React.useState(null);
257257
const ignoreNonTouchEvents = React.useRef(false);
258258
const openedByDisabledTriggerRef = React.useRef(false);
259+
// `onOpen` was called but `open` hasn't been updated by a render yet.
260+
const openRequestedRef = React.useRef(false);
261+
const [, checkOpenRequest] = React.useReducer((count) => count + 1, 0);
259262

260263
const disableInteractive = disableInteractiveProp || followCursor;
261264

@@ -287,6 +290,92 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
287290

288291
React.useEffect(() => stopTouchInteraction, [stopTouchInteraction]);
289292

293+
// React derives `onMouseLeave` from `mouseout` events. When the element under the pointer is
294+
// removed from the DOM (e.g. an icon inside the trigger swapped on hover or open), browsers
295+
// don't dispatch `mouseout` for it, so React never fires `onMouseLeave` on the trigger and the
296+
// tooltip would stay open. While the pointer is over the trigger or the interactive popper, we
297+
// remember the element it was last seen over and listen to `mouseover` on the document: a
298+
// `mouseover` that wasn't handled by the trigger or the popper, after that element was removed,
299+
// means the pointer is somewhere else. https://github.com/mui/material-ui/issues/47073
300+
const hoverTrackingRef = React.useRef(null);
301+
302+
const handleDocumentMouseOver = useEventCallback((nativeEvent) => {
303+
const tracking = hoverTrackingRef.current;
304+
if (nativeEvent === tracking?.lastEvent) {
305+
return;
306+
}
307+
// While the element the pointer was last seen over is still in the document `mouseout` wasn't
308+
// lost and the regular leave handlers close the tooltip. Ignore unrelated `mouseover` events,
309+
// e.g. a pointer resting elsewhere in the document.
310+
if (!tracking || tracking.lastTarget.isConnected) {
311+
return;
312+
}
313+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
314+
handleMouseLeave(nativeEvent);
315+
});
316+
317+
const stopHoverTracking = useEventCallback(() => {
318+
const tracking = hoverTrackingRef.current;
319+
if (tracking) {
320+
tracking.doc.removeEventListener('mouseover', handleDocumentMouseOver);
321+
hoverTrackingRef.current = null;
322+
}
323+
});
324+
325+
// Records a `mouseover` the trigger or the popper handled (or deliberately ignored), so that the
326+
// document listener doesn't treat it as the pointer having left.
327+
const markHoverHandled = (event) => {
328+
const tracking = hoverTrackingRef.current;
329+
if (tracking && event.type === 'mouseover') {
330+
const nativeEvent = event.nativeEvent ?? event;
331+
tracking.lastEvent = nativeEvent;
332+
tracking.lastTarget = nativeEvent.target;
333+
}
334+
};
335+
336+
const startHoverTracking = (event) => {
337+
if (!title && title !== 0) {
338+
// There is no tooltip to close.
339+
return;
340+
}
341+
if (hoverTrackingRef.current) {
342+
markHoverHandled(event);
343+
return;
344+
}
345+
const nativeEvent = event.nativeEvent ?? event;
346+
const doc = event.currentTarget?.ownerDocument ?? document;
347+
doc.addEventListener('mouseover', handleDocumentMouseOver);
348+
hoverTrackingRef.current = {
349+
doc,
350+
lastEvent: nativeEvent,
351+
lastTarget: nativeEvent.target,
352+
};
353+
};
354+
355+
// Runs after every render: once the parent rendered after `onOpen`, `open` is up to date, whether
356+
// it accepted the request or not.
357+
React.useEffect(() => {
358+
openRequestedRef.current = open;
359+
});
360+
361+
// Tracking starts on the first hover, before the tooltip is open, so that a pending open can
362+
// be cancelled too. Once the tooltip is closed (by any route) or unmounted, stop listening.
363+
React.useEffect(() => {
364+
if (!open) {
365+
return undefined;
366+
}
367+
return stopHoverTracking;
368+
}, [open, stopHoverTracking]);
369+
370+
const hasTitle = Boolean(title) || title === 0;
371+
React.useEffect(() => {
372+
if (disableHoverListener || !hasTitle) {
373+
stopHoverTracking();
374+
}
375+
}, [disableHoverListener, hasTitle, stopHoverTracking]);
376+
377+
React.useEffect(() => stopHoverTracking, [stopHoverTracking]);
378+
290379
const handleOpen = (event) => {
291380
hystersisTimer.clear();
292381
hystersisOpen = true;
@@ -297,7 +386,10 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
297386
setOpenState(true);
298387

299388
if (onOpen && !open) {
389+
openRequestedRef.current = true;
300390
onOpen(event);
391+
// The parent may not re-render if it declines to open, so make sure the request is settled.
392+
checkOpenRequest();
301393
}
302394
};
303395

@@ -312,7 +404,10 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
312404
});
313405
setOpenState(false);
314406

315-
if (onClose && open) {
407+
// A leave right after `onOpen` can happen before `open` is updated by a render.
408+
// The parent still has to be told, otherwise a controlled tooltip stays open.
409+
if (onClose && (open || openRequestedRef.current)) {
410+
openRequestedRef.current = false;
316411
onClose(event);
317412
}
318413

@@ -327,6 +422,10 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
327422
return;
328423
}
329424

425+
if (event.type === 'mouseover') {
426+
startHoverTracking(event);
427+
}
428+
330429
// Remove the title ahead of time.
331430
// We don't want to wait for the next render commit.
332431
// We would risk displaying two tooltips at the same time (native + this one).
@@ -349,6 +448,7 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
349448
};
350449

351450
const handleTriggerMouseOver = (event) => {
451+
markHoverHandled(event);
352452
if (childNode?.disabled && !isControlled) {
353453
// A disabled trigger can open the tooltip if it receives pointer events.
354454
// However, if the trigger became disabled while the tooltip was already open,
@@ -366,6 +466,7 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
366466
};
367467

368468
const handleInteractiveWrapperMouseOver = (event) => {
469+
markHoverHandled(event);
369470
if (childNode?.disabled && !isControlled && !openedByDisabledTriggerRef.current) {
370471
return;
371472
}
@@ -374,6 +475,7 @@ const Tooltip = React.forwardRef(function Tooltip(inProps, ref) {
374475
};
375476

376477
const handleMouseLeave = (event) => {
478+
stopHoverTracking();
377479
enterTimer.clear();
378480
leaveTimer.start(leaveDelay, () => {
379481
handleClose(event);

0 commit comments

Comments
 (0)