@@ -5,11 +5,19 @@ type Props = {
5
5
tooltipContentArgs ?: object
6
6
}
7
7
8
- class TooltipPositioner extends React . Component < Props > {
8
+ type State = {
9
+ offset : object ,
10
+ tooltipContainerInitialDimensions : object ,
11
+ tooltipContentArgsCurrent : object
12
+ }
13
+
14
+ class TooltipPositioner extends React . Component < Props , State > {
9
15
private containerRef = React . createRef < HTMLDivElement > ( )
10
16
11
17
state = {
12
- offset : null
18
+ offset : null ,
19
+ tooltipContainerInitialDimensions : null ,
20
+ tooltipContentArgsCurrent : null
13
21
}
14
22
15
23
// simple heuristics to check if the tooltip container exceeds the viewport
@@ -19,7 +27,8 @@ class TooltipPositioner extends React.Component<Props> {
19
27
x : 0 ,
20
28
y : 0
21
29
}
22
- const { right, left, top, bottom } = this . containerRef . current . getBoundingClientRect ( )
30
+ const tooltipContainerInitialDimensions = this . containerRef . current . getBoundingClientRect ( )
31
+ const { right, left, top, bottom } = tooltipContainerInitialDimensions
23
32
const containerWidth = right - left
24
33
const containerHeight = bottom - top
25
34
@@ -37,7 +46,9 @@ class TooltipPositioner extends React.Component<Props> {
37
46
}
38
47
39
48
this . setState ( {
40
- offset
49
+ offset,
50
+ tooltipContainerInitialDimensions,
51
+ tooltipContentArgsCurrent : this . props . tooltipContentArgs
41
52
} )
42
53
}
43
54
@@ -51,7 +62,8 @@ class TooltipPositioner extends React.Component<Props> {
51
62
// if new args, reset offset state
52
63
if ( pp . tooltipContentArgs !== this . props . tooltipContentArgs ) {
53
64
this . setState ( {
54
- offset : null
65
+ offset : null ,
66
+ tooltipContainerInitialDimensions : null
55
67
} )
56
68
}
57
69
else if ( this . containerRef . current && ! this . state . offset ) {
@@ -66,21 +78,28 @@ class TooltipPositioner extends React.Component<Props> {
66
78
} = this . props
67
79
68
80
const {
69
- offset
81
+ offset,
82
+ tooltipContainerInitialDimensions,
83
+ tooltipContentArgsCurrent
70
84
} = this . state
71
85
72
- const containerStyle = offset ?
86
+ const containerStyle = offset && ( tooltipContentArgsCurrent === tooltipContentArgs ) ?
73
87
{
74
88
transform : `translate(${ offset . x } px,${ offset . y } px)`
75
89
} :
76
90
{
77
91
opacity : 0
78
92
}
79
93
94
+ const tooltipContainerAttributes = {
95
+ offset : offset || { x :0 , y :0 } ,
96
+ tooltipContainerInitialDimensions
97
+ }
98
+
80
99
return (
81
100
< div ref = { this . containerRef } style = { containerStyle } >
82
101
{ tooltipContent ( { ...tooltipContentArgs ,
83
- tooltipContainerOffset : offset ? offset : { x : 0 , y : 0 } } ) }
102
+ tooltipContainerAttributes } ) }
84
103
</ div >
85
104
)
86
105
}
0 commit comments