Skip to content

Commit

Permalink
hide tooltip when new props received
Browse files Browse the repository at this point in the history
  • Loading branch information
trisianto committed Oct 27, 2019
1 parent 276629f commit c69a8b5
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/components/TooltipPositioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ type Props = {

type State = {
offset: object,
tooltipContainerInitialDimensions: object
tooltipContainerInitialDimensions: object,
tooltipContentArgsCurrent: object
}

class TooltipPositioner extends React.Component<Props, State> {
private containerRef = React.createRef<HTMLDivElement>()

state = {
offset: null,
tooltipContainerInitialDimensions: null
tooltipContainerInitialDimensions: null,
tooltipContentArgsCurrent: null
}

// simple heuristics to check if the tooltip container exceeds the viewport
Expand Down Expand Up @@ -45,7 +47,8 @@ class TooltipPositioner extends React.Component<Props, State> {

this.setState({
offset,
tooltipContainerInitialDimensions
tooltipContainerInitialDimensions,
tooltipContentArgsCurrent: this.props.tooltipContentArgs
})
}

Expand All @@ -59,7 +62,8 @@ class TooltipPositioner extends React.Component<Props, State> {
// if new args, reset offset state
if(pp.tooltipContentArgs !== this.props.tooltipContentArgs){
this.setState({
offset: null
offset: null,
tooltipContainerInitialDimensions: null
})
}
else if(this.containerRef.current && !this.state.offset){
Expand All @@ -75,10 +79,11 @@ class TooltipPositioner extends React.Component<Props, State> {

const {
offset,
tooltipContainerInitialDimensions
tooltipContainerInitialDimensions,
tooltipContentArgsCurrent
} = this.state

const containerStyle = offset?
const containerStyle = offset && (tooltipContentArgsCurrent===tooltipContentArgs)?
{
transform: `translate(${offset.x}px,${offset.y}px)`
} :
Expand All @@ -87,7 +92,7 @@ class TooltipPositioner extends React.Component<Props, State> {
}

const tooltipContainerAttributes = {
offset,
offset: offset || {x:0, y:0},
tooltipContainerInitialDimensions
}

Expand Down

0 comments on commit c69a8b5

Please sign in to comment.