@@ -6,7 +6,9 @@ import { useTooltip } from 'components/TooltipProvider'
66import useIsomorphicLayoutEffect from 'utils/use-isomorphic-layout-effect'
77import { getScrollParent } from 'utils/get-scroll-parent'
88import { computeTooltipPosition } from 'utils/compute-positions'
9+ import type { IComputedPosition } from 'utils/compute-positions-types'
910import { cssTimeToMs } from 'utils/css-time-to-ms'
11+ import { deepEqual } from 'utils/deep-equal'
1012import coreStyles from './core-styles.module.css'
1113import styles from './styles.module.css'
1214import type {
@@ -15,7 +17,6 @@ import type {
1517 GlobalCloseEvents ,
1618 IPosition ,
1719 ITooltip ,
18- PlacesType ,
1920 TooltipImperativeOpenOptions ,
2021} from './TooltipTypes'
2122
@@ -70,9 +71,11 @@ const Tooltip = ({
7071 const tooltipShowDelayTimerRef = useRef < NodeJS . Timeout | null > ( null )
7172 const tooltipHideDelayTimerRef = useRef < NodeJS . Timeout | null > ( null )
7273 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+ } )
7679 const [ show , setShow ] = useState ( false )
7780 const [ rendered , setRendered ] = useState ( false )
7881 const [ imperativeOptions , setImperativeOptions ] = useState < TooltipImperativeOpenOptions | null > (
@@ -239,6 +242,14 @@ const Tooltip = ({
239242 }
240243 } , [ show ] )
241244
245+ const handleComputedPosition = ( newComputedPosition : IComputedPosition ) => {
246+ setComputedPosition ( ( oldComputedPosition ) =>
247+ deepEqual ( oldComputedPosition , newComputedPosition )
248+ ? oldComputedPosition
249+ : newComputedPosition ,
250+ )
251+ }
252+
242253 const handleShowTooltipDelayed = ( delay = delayShow ) => {
243254 if ( tooltipShowDelayTimerRef . current ) {
244255 clearTimeout ( tooltipShowDelayTimerRef . current )
@@ -335,13 +346,7 @@ const Tooltip = ({
335346 middlewares,
336347 border,
337348 } ) . 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 )
345350 } )
346351 }
347352
@@ -439,13 +444,7 @@ const Tooltip = ({
439444 // invalidate computed positions after remount
440445 return
441446 }
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 )
449448 } )
450449 } , [
451450 show ,
@@ -819,7 +818,7 @@ const Tooltip = ({
819818 } , [ delayShow ] )
820819
821820 const actualContent = imperativeOptions ?. content ?? content
822- const canShow = show && Object . keys ( inlineStyles ) . length > 0
821+ const canShow = show && Object . keys ( computedPosition . tooltipStyles ) . length > 0
823822
824823 useImperativeHandle ( forwardRef , ( ) => ( {
825824 open : ( options ) => {
@@ -849,7 +848,7 @@ const Tooltip = ({
849848 }
850849 } ,
851850 activeAnchor,
852- place : actualPlacement ,
851+ place : computedPosition . place ,
853852 isOpen : Boolean ( rendered && ! hidden && actualContent && canShow ) ,
854853 } ) )
855854
@@ -863,7 +862,7 @@ const Tooltip = ({
863862 styles [ 'tooltip' ] ,
864863 styles [ variant ] ,
865864 className ,
866- `react-tooltip__place-${ actualPlacement } ` ,
865+ `react-tooltip__place-${ computedPosition . place } ` ,
867866 coreStyles [ canShow ? 'show' : 'closing' ] ,
868867 canShow ? 'react-tooltip__show' : 'react-tooltip__closing' ,
869868 positionStrategy === 'fixed' && coreStyles [ 'fixed' ] ,
@@ -882,7 +881,7 @@ const Tooltip = ({
882881 } }
883882 style = { {
884883 ...externalStyles ,
885- ...inlineStyles ,
884+ ...computedPosition . tooltipStyles ,
886885 opacity : opacity !== undefined && canShow ? opacity : undefined ,
887886 } }
888887 ref = { tooltipRef }
@@ -897,7 +896,7 @@ const Tooltip = ({
897896 noArrow && coreStyles [ 'noArrow' ] ,
898897 ) }
899898 style = { {
900- ...inlineArrowStyles ,
899+ ...computedPosition . tooltipArrowStyles ,
901900 background : arrowColor
902901 ? `linear-gradient(to right bottom, transparent 50%, ${ arrowColor } 50%)`
903902 : undefined ,
0 commit comments