Skip to content

Commit 1903b88

Browse files
authored
Merge pull request #1293 from gogakoreli/fix/tooltip-removal
fix: remove a tooltip that closes before it shows
2 parents 20b2259 + bd8525c commit 1903b88

4 files changed

Lines changed: 385 additions & 45 deletions

File tree

‎src/components/Tooltip/Tooltip.tsx‎

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,15 @@ import React, {
99
} from 'react'
1010
import { createPortal } from 'react-dom'
1111
import clsx from 'clsx'
12-
import {
13-
useIsomorphicLayoutEffect,
14-
computeTooltipPosition,
15-
cssTimeToMs,
16-
clearTimeoutRef,
17-
} from '../../utils'
12+
import { useIsomorphicLayoutEffect, computeTooltipPosition, clearTimeoutRef } from '../../utils'
1813
import type { IComputedPosition } from '../../utils'
1914
import coreStyles from './core-styles.module.css'
2015
import styles from './styles.module.css'
2116
import useTooltipAnchors from './use-tooltip-anchors'
2217
import useTooltipEvents from './use-tooltip-events'
18+
import useTooltipRemoval from './use-tooltip-removal'
2319
import type { IPosition, ITooltip, TooltipImperativeOpenOptions } from './TooltipTypes'
2420

25-
// Shared across all tooltip instances — the CSS variable is on :root and never changes per-instance
26-
let globalTransitionShowDelay: number | null = null
27-
2821
const Tooltip = ({
2922
// props
3023
forwardRef,
@@ -76,13 +69,13 @@ const Tooltip = ({
7669
const tooltipShowDelayTimerRef = useRef<NodeJS.Timeout | null>(null)
7770
const tooltipHideDelayTimerRef = useRef<NodeJS.Timeout | null>(null)
7871
const tooltipAutoCloseTimerRef = useRef<NodeJS.Timeout | null>(null)
79-
const missedTransitionTimerRef = useRef<NodeJS.Timeout | null>(null)
8072
const [computedPosition, setComputedPosition] = useState<IComputedPosition>({
8173
tooltipStyles: {},
8274
tooltipArrowStyles: {},
8375
place,
8476
})
8577
const [show, setShow] = useState(false)
78+
const [showRequested, setShowRequested] = useState(false)
8679
const [rendered, setRendered] = useState(false)
8780
const [imperativeOptions, setImperativeOptions] = useState<TooltipImperativeOpenOptions | null>(
8881
null,
@@ -121,6 +114,7 @@ const Tooltip = ({
121114
if (!mounted.current) {
122115
return
123116
}
117+
setShowRequested(value)
124118
if (value) {
125119
setRendered(true)
126120
}
@@ -199,33 +193,23 @@ const Tooltip = ({
199193
if (show === wasShowing.current) {
200194
return
201195
}
202-
clearTimeoutRef(missedTransitionTimerRef)
203196
wasShowing.current = show
204197
if (show) {
205198
afterShow?.()
206-
} else {
207-
/**
208-
* see `onTransitionEnd` on tooltip wrapper
209-
*/
210-
if (globalTransitionShowDelay === null) {
211-
const style = getComputedStyle(document.body)
212-
globalTransitionShowDelay = cssTimeToMs(
213-
style.getPropertyValue('--rt-transition-show-delay'),
214-
)
215-
}
216-
const transitionShowDelay = globalTransitionShowDelay
217-
missedTransitionTimerRef.current = setTimeout(() => {
218-
/**
219-
* if the tooltip switches from `show === true` to `show === false` too fast
220-
* the transition never runs, so `onTransitionEnd` callback never gets fired
221-
*/
222-
setRendered(false)
223-
setImperativeOptions(null)
224-
afterHide?.()
225-
// +25ms just to make sure `onTransitionEnd` (if it gets fired) has time to run
226-
}, transitionShowDelay + 25)
227199
}
228-
}, [afterHide, afterShow, show])
200+
}, [afterShow, show])
201+
202+
const handleTransitionEnd = useTooltipRemoval({
203+
rendered,
204+
show,
205+
// a controlled tooltip shows when `isOpen` says so, whatever was requested
206+
showRequested: isOpen === undefined ? showRequested : isOpen,
207+
afterHide,
208+
onRemove: () => {
209+
setRendered(false)
210+
setImperativeOptions(null)
211+
},
212+
})
229213

230214
useEffect(() => {
231215
clearTimeoutRef(tooltipAutoCloseTimerRef)
@@ -550,7 +534,6 @@ const Tooltip = ({
550534
clearTimeoutRef(tooltipShowDelayTimerRef)
551535
clearTimeoutRef(tooltipHideDelayTimerRef)
552536
clearTimeoutRef(tooltipAutoCloseTimerRef)
553-
clearTimeoutRef(missedTransitionTimerRef)
554537
}
555538
}, [defaultIsOpen, handleShow])
556539

@@ -639,7 +622,6 @@ const Tooltip = ({
639622
clearTimeoutRef(tooltipShowDelayTimerRef)
640623
clearTimeoutRef(tooltipHideDelayTimerRef)
641624
clearTimeoutRef(tooltipAutoCloseTimerRef)
642-
clearTimeoutRef(missedTransitionTimerRef)
643625
}
644626
}, [])
645627

@@ -660,15 +642,7 @@ const Tooltip = ({
660642
positionStrategy === 'fixed' && coreStyles['fixed'],
661643
clickable && coreStyles['clickable'],
662644
)}
663-
onTransitionEnd={(event: TransitionEvent) => {
664-
clearTimeoutRef(missedTransitionTimerRef)
665-
if (show || event.propertyName !== 'opacity') {
666-
return
667-
}
668-
setRendered(false)
669-
setImperativeOptions(null)
670-
afterHide?.()
671-
}}
645+
onTransitionEnd={handleTransitionEnd}
672646
style={tooltipStyle}
673647
ref={tooltipRef}
674648
>

‎src/components/Tooltip/core-styles.module.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
display: none;
2828
}
2929

30-
.clickable {
30+
/* only while it shows: a tooltip that is not showing is invisible and must not take the pointer */
31+
.clickable.show {
3132
pointer-events: auto;
3233
}
3334

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { useCallback, useEffect, useRef, useState } from 'react'
2+
import { cssTimeToMs, useIsomorphicLayoutEffect } from '../../utils'
3+
4+
// Shared across all tooltip instances — the CSS variable is on :root and never changes per-instance
5+
let globalTransitionShowDelay: number | null = null
6+
7+
const getMissedTransitionDelay = () => {
8+
if (globalTransitionShowDelay === null) {
9+
const style = getComputedStyle(document.body)
10+
globalTransitionShowDelay = cssTimeToMs(style.getPropertyValue('--rt-transition-show-delay'))
11+
}
12+
// +25ms just to make sure `onTransitionEnd` (if it gets fired) has time to run
13+
return globalTransitionShowDelay + 25
14+
}
15+
16+
/**
17+
* Takes a tooltip out of the DOM once it is neither showing nor asked to show,
18+
* and calls `afterHide` if it had shown.
19+
*
20+
* The tooltip is rendered a moment before it shows, and stays rendered while it fades out.
21+
* The closing transition ends that (see the returned `onTransitionEnd` handler).
22+
* A timer ends it when no transition runs:
23+
* - the tooltip showed and closed too fast for the transition to start
24+
* - the tooltip closed before it showed, so `show` never changed
25+
* - a controlled `isOpen` never let the tooltip show
26+
*/
27+
const useTooltipRemoval = ({
28+
rendered,
29+
show,
30+
showRequested,
31+
afterHide,
32+
onRemove,
33+
}: {
34+
rendered: boolean
35+
show: boolean
36+
/** the tooltip was asked to show, and `show` follows a moment later */
37+
showRequested: boolean
38+
afterHide?: () => void
39+
onRemove: () => void
40+
}) => {
41+
/**
42+
* the timer and the transition end only mark the removal as due.
43+
* either can run in the same turn as a request to show that has not rendered yet,
44+
* and removing the tooltip right there would take it out just as it shows again
45+
*/
46+
const [removalDue, setRemovalDue] = useState(false)
47+
// the tooltip showed, and `afterHide` has not been called for it yet
48+
const afterHidePending = useRef(false)
49+
50+
useEffect(() => {
51+
if (show) {
52+
afterHidePending.current = true
53+
}
54+
}, [show])
55+
56+
useEffect(() => {
57+
if (show || showRequested || (!rendered && !afterHidePending.current)) {
58+
return undefined
59+
}
60+
const timer = setTimeout(() => {
61+
setRemovalDue(true)
62+
}, getMissedTransitionDelay())
63+
return () => {
64+
clearTimeout(timer)
65+
}
66+
}, [rendered, show, showRequested])
67+
68+
/**
69+
* `show` and `showRequested` are current here, so this is where a due removal
70+
* is carried out or dropped. a layout effect, so the tooltip is gone before the next paint
71+
*/
72+
useIsomorphicLayoutEffect(() => {
73+
if (!removalDue) {
74+
return
75+
}
76+
setRemovalDue(false)
77+
if (show || showRequested) {
78+
return
79+
}
80+
onRemove()
81+
if (afterHidePending.current) {
82+
afterHidePending.current = false
83+
afterHide?.()
84+
}
85+
}, [removalDue, show, showRequested, onRemove, afterHide])
86+
87+
return useCallback(
88+
(event: TransitionEvent) => {
89+
if (!show && event.propertyName === 'opacity') {
90+
setRemovalDue(true)
91+
}
92+
},
93+
[show],
94+
)
95+
}
96+
97+
export default useTooltipRemoval

0 commit comments

Comments
 (0)