@@ -9,22 +9,15 @@ import React, {
99} from 'react'
1010import { createPortal } from 'react-dom'
1111import clsx from 'clsx'
12- import {
13- useIsomorphicLayoutEffect ,
14- computeTooltipPosition ,
15- cssTimeToMs ,
16- clearTimeoutRef ,
17- } from '../../utils'
12+ import { useIsomorphicLayoutEffect , computeTooltipPosition , clearTimeoutRef } from '../../utils'
1813import type { IComputedPosition } from '../../utils'
1914import coreStyles from './core-styles.module.css'
2015import styles from './styles.module.css'
2116import useTooltipAnchors from './use-tooltip-anchors'
2217import useTooltipEvents from './use-tooltip-events'
18+ import useTooltipRemoval from './use-tooltip-removal'
2319import 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-
2821const 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 >
0 commit comments