Skip to content

Commit

Permalink
Merge pull request #509 from torioLuz/tooltip-edge-detection
Browse files Browse the repository at this point in the history
add more tooltip attributes for more flexible customization
  • Loading branch information
emeeks authored Nov 11, 2019
2 parents 7a80418 + c69a8b5 commit 3e5e444
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/components/TooltipPositioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ type Props = {
tooltipContentArgs?: object
}

class TooltipPositioner extends React.Component<Props> {
type State = {
offset: object,
tooltipContainerInitialDimensions: object,
tooltipContentArgsCurrent: object
}

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

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

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

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

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

Expand All @@ -51,7 +62,8 @@ class TooltipPositioner extends React.Component<Props> {
// 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 @@ -66,21 +78,28 @@ class TooltipPositioner extends React.Component<Props> {
} = this.props

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

const containerStyle = offset?
const containerStyle = offset && (tooltipContentArgsCurrent===tooltipContentArgs)?
{
transform: `translate(${offset.x}px,${offset.y}px)`
} :
{
opacity: 0
}

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

return (
<div ref={this.containerRef} style={containerStyle}>
{tooltipContent({...tooltipContentArgs,
tooltipContainerOffset: offset? offset: {x:0, y:0}})}
tooltipContainerAttributes})}
</div>
)
}
Expand Down

0 comments on commit 3e5e444

Please sign in to comment.