Skip to content

Commit 669c243

Browse files
authored
Merge pull request #127 from wwayne/position
Fix stack overflow in some edge case of position calculation
2 parents 7f1f896 + c91a21d commit 669c243

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class ReactTooltip extends Component {
248248
const node = ReactDOM.findDOMNode(this)
249249

250250
const result = getPosition(currentEvent, currentTarget, node, place, effect, offset)
251+
251252
if (result.isNewState) {
252253
// Switch to reverse placement
253254
return this.setState(result.newState, () => {

src/utils/getPosition.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,37 @@ export default function (e, target, node, place, effect, offset) {
4444

4545
// Judge if the tooltip has over the window(screen)
4646
const outsideVertical = () => {
47-
// Check for horazontal tooltip, if their vertical out of screen
4847
let result = false
4948
let newPlace
50-
if (getTipOffsetTop('left') < 0 && getTipOffsetBottom('left') <= windowHeight) {
49+
if (getTipOffsetTop('left') < 0 &&
50+
getTipOffsetBottom('left') <= windowHeight &&
51+
getTipOffsetBottom('bottom') <= windowHeight) {
5152
result = true
5253
newPlace = 'bottom'
53-
} else if (getTipOffsetBottom('left') > windowHeight && getTipOffsetTop('left') >= 0) {
54+
} else if (getTipOffsetBottom('left') > windowHeight &&
55+
getTipOffsetTop('left') >= 0 &&
56+
getTipOffsetTop('top') >= 0) {
5457
result = true
5558
newPlace = 'top'
5659
}
57-
if (result && outsideHorizontal().result) result = false
5860
return {result, newPlace}
5961
}
6062
const outsideLeft = () => {
61-
// For horizontal tooltip, if vertical out of screen, change the vertical place
62-
let {result, newPlace} = outsideVertical()
63+
let {result, newPlace} = outsideVertical() // Deal with vertical as first priority
64+
if (result && outsideHorizontal().result) {
65+
return {result: false} // No need to change, if change to vertical will out of space
66+
}
6367
if (!result && getTipOffsetLeft('left') < 0 && getTipOffsetRight('right') <= windowWidth) {
64-
result = true
68+
result = true // If vertical ok, but let out of side and right won't out of side
6569
newPlace = 'right'
6670
}
6771
return {result, newPlace}
6872
}
6973
const outsideRight = () => {
7074
let {result, newPlace} = outsideVertical()
75+
if (result && outsideHorizontal().result) {
76+
return {result: false} // No need to change, if change to vertical will out of space
77+
}
7178
if (!result && getTipOffsetRight('right') > windowWidth && getTipOffsetLeft('left') >= 0) {
7279
result = true
7380
newPlace = 'left'
@@ -78,19 +85,24 @@ export default function (e, target, node, place, effect, offset) {
7885
const outsideHorizontal = () => {
7986
let result = false
8087
let newPlace
81-
if (getTipOffsetLeft('top') < 0 && getTipOffsetRight('top') <= windowWidth) {
88+
if (getTipOffsetLeft('top') < 0 &&
89+
getTipOffsetRight('top') <= windowWidth &&
90+
getTipOffsetRight('right') <= windowWidth) {
8291
result = true
8392
newPlace = 'right'
84-
} else if (getTipOffsetRight('top') > windowWidth && getTipOffsetLeft('top') >= 0) {
93+
} else if (getTipOffsetRight('top') > windowWidth &&
94+
getTipOffsetLeft('top') >= 0 &&
95+
getTipOffsetLeft('left') >= 0) {
8596
result = true
8697
newPlace = 'left'
8798
}
88-
89-
if (result && outsideVertical().result) result = false
9099
return {result, newPlace}
91100
}
92101
const outsideTop = () => {
93102
let {result, newPlace} = outsideHorizontal()
103+
if (result && outsideVertical().result) {
104+
return {result: false}
105+
}
94106
if (!result && getTipOffsetTop('top') < 0 && getTipOffsetBottom('bottom') <= windowHeight) {
95107
result = true
96108
newPlace = 'bottom'
@@ -99,6 +111,9 @@ export default function (e, target, node, place, effect, offset) {
99111
}
100112
const outsideBottom = () => {
101113
let {result, newPlace} = outsideHorizontal()
114+
if (result && outsideVertical().result) {
115+
return {result: false}
116+
}
102117
if (!result && getTipOffsetBottom('bottom') > windowHeight && getTipOffsetTop('top') >= 0) {
103118
result = true
104119
newPlace = 'top'

0 commit comments

Comments
 (0)