Skip to content

Commit 3e5e444

Browse files
authored
Merge pull request #509 from torioLuz/tooltip-edge-detection
add more tooltip attributes for more flexible customization
2 parents 7a80418 + c69a8b5 commit 3e5e444

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/components/TooltipPositioner.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ type Props = {
55
tooltipContentArgs?: object
66
}
77

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> {
915
private containerRef = React.createRef<HTMLDivElement>()
1016

1117
state = {
12-
offset: null
18+
offset: null,
19+
tooltipContainerInitialDimensions: null,
20+
tooltipContentArgsCurrent: null
1321
}
1422

1523
// simple heuristics to check if the tooltip container exceeds the viewport
@@ -19,7 +27,8 @@ class TooltipPositioner extends React.Component<Props> {
1927
x: 0,
2028
y: 0
2129
}
22-
const { right, left, top, bottom } = this.containerRef.current.getBoundingClientRect()
30+
const tooltipContainerInitialDimensions = this.containerRef.current.getBoundingClientRect()
31+
const { right, left, top, bottom } = tooltipContainerInitialDimensions
2332
const containerWidth = right - left
2433
const containerHeight = bottom - top
2534

@@ -37,7 +46,9 @@ class TooltipPositioner extends React.Component<Props> {
3746
}
3847

3948
this.setState({
40-
offset
49+
offset,
50+
tooltipContainerInitialDimensions,
51+
tooltipContentArgsCurrent: this.props.tooltipContentArgs
4152
})
4253
}
4354

@@ -51,7 +62,8 @@ class TooltipPositioner extends React.Component<Props> {
5162
// if new args, reset offset state
5263
if(pp.tooltipContentArgs !== this.props.tooltipContentArgs){
5364
this.setState({
54-
offset: null
65+
offset: null,
66+
tooltipContainerInitialDimensions: null
5567
})
5668
}
5769
else if(this.containerRef.current && !this.state.offset){
@@ -66,21 +78,28 @@ class TooltipPositioner extends React.Component<Props> {
6678
} = this.props
6779

6880
const {
69-
offset
81+
offset,
82+
tooltipContainerInitialDimensions,
83+
tooltipContentArgsCurrent
7084
} = this.state
7185

72-
const containerStyle = offset?
86+
const containerStyle = offset && (tooltipContentArgsCurrent===tooltipContentArgs)?
7387
{
7488
transform: `translate(${offset.x}px,${offset.y}px)`
7589
} :
7690
{
7791
opacity: 0
7892
}
7993

94+
const tooltipContainerAttributes = {
95+
offset: offset || {x:0, y:0},
96+
tooltipContainerInitialDimensions
97+
}
98+
8099
return (
81100
<div ref={this.containerRef} style={containerStyle}>
82101
{tooltipContent({...tooltipContentArgs,
83-
tooltipContainerOffset: offset? offset: {x:0, y:0}})}
102+
tooltipContainerAttributes})}
84103
</div>
85104
)
86105
}

0 commit comments

Comments
 (0)