8
8
getScrollParent ,
9
9
computeTooltipPosition ,
10
10
cssTimeToMs ,
11
+ clearTimeoutRef ,
11
12
} from 'utils'
12
13
import type { IComputedPosition } from 'utils'
13
14
import { useTooltip } from 'components/TooltipProvider'
@@ -223,9 +224,7 @@ const Tooltip = ({
223
224
if ( show === wasShowing . current ) {
224
225
return
225
226
}
226
- if ( missedTransitionTimerRef . current ) {
227
- clearTimeout ( missedTransitionTimerRef . current )
228
- }
227
+ clearTimeoutRef ( missedTransitionTimerRef )
229
228
wasShowing . current = show
230
229
if ( show ) {
231
230
afterShow ?.( )
@@ -257,9 +256,7 @@ const Tooltip = ({
257
256
}
258
257
259
258
const handleShowTooltipDelayed = ( delay = delayShow ) => {
260
- if ( tooltipShowDelayTimerRef . current ) {
261
- clearTimeout ( tooltipShowDelayTimerRef . current )
262
- }
259
+ clearTimeoutRef ( tooltipShowDelayTimerRef )
263
260
264
261
if ( rendered ) {
265
262
// if the tooltip is already rendered, ignore delay
@@ -273,9 +270,7 @@ const Tooltip = ({
273
270
}
274
271
275
272
const handleHideTooltipDelayed = ( delay = delayHide ) => {
276
- if ( tooltipHideDelayTimerRef . current ) {
277
- clearTimeout ( tooltipHideDelayTimerRef . current )
278
- }
273
+ clearTimeoutRef ( tooltipHideDelayTimerRef )
279
274
280
275
tooltipHideDelayTimerRef . current = setTimeout ( ( ) => {
281
276
if ( hoveringTooltip . current ) {
@@ -307,9 +302,7 @@ const Tooltip = ({
307
302
setActiveAnchor ( target )
308
303
setProviderActiveAnchor ( { current : target } )
309
304
310
- if ( tooltipHideDelayTimerRef . current ) {
311
- clearTimeout ( tooltipHideDelayTimerRef . current )
312
- }
305
+ clearTimeoutRef ( tooltipHideDelayTimerRef )
313
306
}
314
307
315
308
const handleHideTooltip = ( ) => {
@@ -322,9 +315,7 @@ const Tooltip = ({
322
315
handleShow ( false )
323
316
}
324
317
325
- if ( tooltipShowDelayTimerRef . current ) {
326
- clearTimeout ( tooltipShowDelayTimerRef . current )
327
- }
318
+ clearTimeoutRef ( tooltipShowDelayTimerRef )
328
319
}
329
320
330
321
const handleTooltipPosition = ( { x, y } : IPosition ) => {
@@ -386,9 +377,7 @@ const Tooltip = ({
386
377
return
387
378
}
388
379
handleShow ( false )
389
- if ( tooltipShowDelayTimerRef . current ) {
390
- clearTimeout ( tooltipShowDelayTimerRef . current )
391
- }
380
+ clearTimeoutRef ( tooltipShowDelayTimerRef )
392
381
}
393
382
394
383
// debounce handler to prevent call twice when
@@ -697,12 +686,8 @@ const Tooltip = ({
697
686
setRendered ( false )
698
687
handleShow ( false )
699
688
setActiveAnchor ( null )
700
- if ( tooltipShowDelayTimerRef . current ) {
701
- clearTimeout ( tooltipShowDelayTimerRef . current )
702
- }
703
- if ( tooltipHideDelayTimerRef . current ) {
704
- clearTimeout ( tooltipHideDelayTimerRef . current )
705
- }
689
+ clearTimeoutRef ( tooltipShowDelayTimerRef )
690
+ clearTimeoutRef ( tooltipHideDelayTimerRef )
706
691
return true
707
692
}
708
693
return false
@@ -790,12 +775,8 @@ const Tooltip = ({
790
775
handleShow ( true )
791
776
}
792
777
return ( ) => {
793
- if ( tooltipShowDelayTimerRef . current ) {
794
- clearTimeout ( tooltipShowDelayTimerRef . current )
795
- }
796
- if ( tooltipHideDelayTimerRef . current ) {
797
- clearTimeout ( tooltipHideDelayTimerRef . current )
798
- }
778
+ clearTimeoutRef ( tooltipShowDelayTimerRef )
779
+ clearTimeoutRef ( tooltipHideDelayTimerRef )
799
780
}
800
781
} , [ ] )
801
782
@@ -818,7 +799,11 @@ const Tooltip = ({
818
799
819
800
useEffect ( ( ) => {
820
801
if ( tooltipShowDelayTimerRef . current ) {
821
- clearTimeout ( tooltipShowDelayTimerRef . current )
802
+ /**
803
+ * if the delay changes while the tooltip is waiting to show,
804
+ * reset the timer with the new delay
805
+ */
806
+ clearTimeoutRef ( tooltipShowDelayTimerRef )
822
807
handleShowTooltipDelayed ( delayShow )
823
808
}
824
809
} , [ delayShow ] )
@@ -875,9 +860,7 @@ const Tooltip = ({
875
860
clickable && coreStyles [ 'clickable' ] ,
876
861
) }
877
862
onTransitionEnd = { ( event : TransitionEvent ) => {
878
- if ( missedTransitionTimerRef . current ) {
879
- clearTimeout ( missedTransitionTimerRef . current )
880
- }
863
+ clearTimeoutRef ( missedTransitionTimerRef )
881
864
if ( show || event . propertyName !== 'opacity' ) {
882
865
return
883
866
}
0 commit comments