@@ -6,7 +6,9 @@ import { useTooltip } from 'components/TooltipProvider'
6
6
import useIsomorphicLayoutEffect from 'utils/use-isomorphic-layout-effect'
7
7
import { getScrollParent } from 'utils/get-scroll-parent'
8
8
import { computeTooltipPosition } from 'utils/compute-positions'
9
+ import type { IComputedPosition } from 'utils/compute-positions-types'
9
10
import { cssTimeToMs } from 'utils/css-time-to-ms'
11
+ import { deepEqual } from 'utils/deep-equal'
10
12
import coreStyles from './core-styles.module.css'
11
13
import styles from './styles.module.css'
12
14
import type {
@@ -15,7 +17,6 @@ import type {
15
17
GlobalCloseEvents ,
16
18
IPosition ,
17
19
ITooltip ,
18
- PlacesType ,
19
20
TooltipImperativeOpenOptions ,
20
21
} from './TooltipTypes'
21
22
@@ -70,9 +71,11 @@ const Tooltip = ({
70
71
const tooltipShowDelayTimerRef = useRef < NodeJS . Timeout | null > ( null )
71
72
const tooltipHideDelayTimerRef = useRef < NodeJS . Timeout | null > ( null )
72
73
const missedTransitionTimerRef = useRef < NodeJS . Timeout | null > ( null )
73
- const [ actualPlacement , setActualPlacement ] = useState ( place )
74
- const [ inlineStyles , setInlineStyles ] = useState ( { } )
75
- const [ inlineArrowStyles , setInlineArrowStyles ] = useState ( { } )
74
+ const [ computedPosition , setComputedPosition ] = useState < IComputedPosition > ( {
75
+ tooltipStyles : { } ,
76
+ tooltipArrowStyles : { } ,
77
+ place,
78
+ } )
76
79
const [ show , setShow ] = useState ( false )
77
80
const [ rendered , setRendered ] = useState ( false )
78
81
const [ imperativeOptions , setImperativeOptions ] = useState < TooltipImperativeOpenOptions | null > (
@@ -239,6 +242,14 @@ const Tooltip = ({
239
242
}
240
243
} , [ show ] )
241
244
245
+ const handleComputedPosition = ( newComputedPosition : IComputedPosition ) => {
246
+ setComputedPosition ( ( oldComputedPosition ) =>
247
+ deepEqual ( oldComputedPosition , newComputedPosition )
248
+ ? oldComputedPosition
249
+ : newComputedPosition ,
250
+ )
251
+ }
252
+
242
253
const handleShowTooltipDelayed = ( delay = delayShow ) => {
243
254
if ( tooltipShowDelayTimerRef . current ) {
244
255
clearTimeout ( tooltipShowDelayTimerRef . current )
@@ -335,13 +346,7 @@ const Tooltip = ({
335
346
middlewares,
336
347
border,
337
348
} ) . then ( ( computedStylesData ) => {
338
- if ( Object . keys ( computedStylesData . tooltipStyles ) . length ) {
339
- setInlineStyles ( computedStylesData . tooltipStyles )
340
- }
341
- if ( Object . keys ( computedStylesData . tooltipArrowStyles ) . length ) {
342
- setInlineArrowStyles ( computedStylesData . tooltipArrowStyles )
343
- }
344
- setActualPlacement ( computedStylesData . place as PlacesType )
349
+ handleComputedPosition ( computedStylesData )
345
350
} )
346
351
}
347
352
@@ -439,13 +444,7 @@ const Tooltip = ({
439
444
// invalidate computed positions after remount
440
445
return
441
446
}
442
- if ( Object . keys ( computedStylesData . tooltipStyles ) . length ) {
443
- setInlineStyles ( computedStylesData . tooltipStyles )
444
- }
445
- if ( Object . keys ( computedStylesData . tooltipArrowStyles ) . length ) {
446
- setInlineArrowStyles ( computedStylesData . tooltipArrowStyles )
447
- }
448
- setActualPlacement ( computedStylesData . place as PlacesType )
447
+ handleComputedPosition ( computedStylesData )
449
448
} )
450
449
} , [
451
450
show ,
@@ -819,7 +818,7 @@ const Tooltip = ({
819
818
} , [ delayShow ] )
820
819
821
820
const actualContent = imperativeOptions ?. content ?? content
822
- const canShow = show && Object . keys ( inlineStyles ) . length > 0
821
+ const canShow = show && Object . keys ( computedPosition . tooltipStyles ) . length > 0
823
822
824
823
useImperativeHandle ( forwardRef , ( ) => ( {
825
824
open : ( options ) => {
@@ -849,7 +848,7 @@ const Tooltip = ({
849
848
}
850
849
} ,
851
850
activeAnchor,
852
- place : actualPlacement ,
851
+ place : computedPosition . place ,
853
852
isOpen : Boolean ( rendered && ! hidden && actualContent && canShow ) ,
854
853
} ) )
855
854
@@ -863,7 +862,7 @@ const Tooltip = ({
863
862
styles [ 'tooltip' ] ,
864
863
styles [ variant ] ,
865
864
className ,
866
- `react-tooltip__place-${ actualPlacement } ` ,
865
+ `react-tooltip__place-${ computedPosition . place } ` ,
867
866
coreStyles [ canShow ? 'show' : 'closing' ] ,
868
867
canShow ? 'react-tooltip__show' : 'react-tooltip__closing' ,
869
868
positionStrategy === 'fixed' && coreStyles [ 'fixed' ] ,
@@ -882,7 +881,7 @@ const Tooltip = ({
882
881
} }
883
882
style = { {
884
883
...externalStyles ,
885
- ...inlineStyles ,
884
+ ...computedPosition . tooltipStyles ,
886
885
opacity : opacity !== undefined && canShow ? opacity : undefined ,
887
886
} }
888
887
ref = { tooltipRef }
@@ -897,7 +896,7 @@ const Tooltip = ({
897
896
noArrow && coreStyles [ 'noArrow' ] ,
898
897
) }
899
898
style = { {
900
- ...inlineArrowStyles ,
899
+ ...computedPosition . tooltipArrowStyles ,
901
900
background : arrowColor
902
901
? `linear-gradient(to right bottom, transparent 50%, ${ arrowColor } 50%)`
903
902
: undefined ,
0 commit comments