Skip to content

Commit

Permalink
Return tooltip to the original position
Browse files Browse the repository at this point in the history
  • Loading branch information
huumanoid committed Mar 20, 2017
1 parent 889985e commit 948e5c5
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class ReactTooltip extends Component {
this.setState({
placeholder,
isEmptyTip,
place: e.currentTarget.getAttribute('data-place') || this.props.place || 'top',
place: this.getPlace(e.currentTarget),
type: e.currentTarget.getAttribute('data-type') || this.props.type || 'dark',
effect: switchToSolid && 'solid' || this.getEffect(e.currentTarget),
offset: e.currentTarget.getAttribute('data-offset') || this.props.offset || {},
Expand Down Expand Up @@ -377,21 +377,35 @@ class ReactTooltip extends Component {
window.removeEventListener('scroll', this.hideTooltip)
}

getPlace(target) {
return target.getAttribute('data-place') || this.props.place || 'top'
}

// Calculation the position
updatePosition () {
const {currentEvent, currentTarget, place, effect, offset} = this.state
updatePosition (isRecursive) {
const {currentEvent, currentTarget, effect, offset} = this.state
const place = isRecursive ? this.state.place : this.getPlace(currentTarget)
const node = ReactDOM.findDOMNode(this)
const result = getPosition(currentEvent, currentTarget, node, place, effect, offset)

if (result.isNewState) {
// Switch to reverse placement
return this.setState(result.newState, () => {
this.updatePosition()
this.updatePosition(true)
})
}

// Set tooltip position
node.style.left = result.position.left + 'px'
node.style.top = result.position.top + 'px'
const setPosition = () => {
node.style.left = result.position.left + 'px'
node.style.top = result.position.top + 'px'
}

if (this.state.place !== place) {
this.setState({ place }, setPosition)
} else {
setPosition()
}
}

/**
Expand Down

0 comments on commit 948e5c5

Please sign in to comment.