@@ -21,54 +21,116 @@ export default function (e, target, node, place, effect, offset) {
21
21
const defaultOffset = getDefaultPosition ( effect , target . clientWidth , target . clientHeight , tipWidth , tipHeight )
22
22
const { extraOffset_X, extraOffset_Y} = calculateOffset ( offset )
23
23
24
- const widnowWidth = window . innerWidth
24
+ const windowWidth = window . innerWidth
25
25
const windowHeight = window . innerHeight
26
26
27
27
// Get the edge offset of the tooltip
28
28
const getTipOffsetLeft = ( place ) => {
29
- const offset_X = defaultOffset [ place ] . x
29
+ const offset_X = defaultOffset [ place ] . l
30
+ return mouseX + offset_X + extraOffset_X
31
+ }
32
+ const getTipOffsetRight = ( place ) => {
33
+ const offset_X = defaultOffset [ place ] . r
30
34
return mouseX + offset_X + extraOffset_X
31
35
}
32
36
const getTipOffsetTop = ( place ) => {
33
- const offset_Y = defaultOffset [ place ] . y
37
+ const offset_Y = defaultOffset [ place ] . t
38
+ return mouseY + offset_Y + extraOffset_Y
39
+ }
40
+ const getTipOffsetBottom = ( place ) => {
41
+ const offset_Y = defaultOffset [ place ] . b
34
42
return mouseY + offset_Y + extraOffset_Y
35
43
}
36
44
37
45
// Judge if the tooltip has over the window(screen)
46
+ const outsideVertical = ( ) => {
47
+ // Check for horazontal tooltip, if their vertical out of screen
48
+ let result = false
49
+ let newPlace
50
+ if ( getTipOffsetTop ( 'left' ) < 0 && getTipOffsetBottom ( 'left' ) <= windowHeight ) {
51
+ result = true
52
+ newPlace = 'bottom'
53
+ } else if ( getTipOffsetBottom ( 'left' ) > windowHeight && getTipOffsetTop ( 'left' ) >= 0 ) {
54
+ result = true
55
+ newPlace = 'top'
56
+ }
57
+ if ( result && outsideHorizontal ( ) . result ) result = false
58
+ return { result, newPlace}
59
+ }
38
60
const outsideLeft = ( ) => {
39
- // if switch to right will out of screen, return false because switch placement doesn't make sense
40
- return getTipOffsetLeft ( 'left' ) < 0 && getTipOffsetLeft ( 'right' ) <= widnowWidth
61
+ // For horizontal tooltip, if vertical out of screen, change the vertical place
62
+ let { result, newPlace} = outsideVertical ( )
63
+ if ( ! result && getTipOffsetLeft ( 'left' ) < 0 && getTipOffsetRight ( 'right' ) <= windowWidth ) {
64
+ result = true
65
+ newPlace = 'right'
66
+ }
67
+ return { result, newPlace}
41
68
}
42
69
const outsideRight = ( ) => {
43
- return getTipOffsetLeft ( 'right' ) > widnowWidth && getTipOffsetLeft ( 'left' ) >= 0
70
+ let { result, newPlace} = outsideVertical ( )
71
+ if ( ! result && getTipOffsetRight ( 'right' ) > windowWidth && getTipOffsetLeft ( 'left' ) >= 0 ) {
72
+ result = true
73
+ newPlace = 'left'
74
+ }
75
+ return { result, newPlace}
76
+ }
77
+
78
+ const outsideHorizontal = ( ) => {
79
+ let result = false
80
+ let newPlace
81
+ if ( getTipOffsetLeft ( 'top' ) < 0 && getTipOffsetRight ( 'top' ) <= windowWidth ) {
82
+ result = true
83
+ newPlace = 'right'
84
+ } else if ( getTipOffsetRight ( 'top' ) > windowWidth && getTipOffsetLeft ( 'top' ) >= 0 ) {
85
+ result = true
86
+ newPlace = 'left'
87
+ }
88
+
89
+ if ( result && outsideVertical ( ) . result ) result = false
90
+ return { result, newPlace}
44
91
}
45
92
const outsideTop = ( ) => {
46
- return getTipOffsetTop ( 'top' ) < 0 && getTipOffsetTop ( 'bottom' ) + tipHeight <= windowHeight
93
+ let { result, newPlace} = outsideHorizontal ( )
94
+ if ( ! result && getTipOffsetTop ( 'top' ) < 0 && getTipOffsetBottom ( 'bottom' ) <= windowHeight ) {
95
+ result = true
96
+ newPlace = 'bottom'
97
+ }
98
+ return { result, newPlace}
47
99
}
48
100
const outsideBottom = ( ) => {
49
- return getTipOffsetTop ( 'bottom' ) + tipHeight > windowHeight && getTipOffsetTop ( 'top' ) >= 0
101
+ let { result, newPlace} = outsideHorizontal ( )
102
+ if ( ! result && getTipOffsetBottom ( 'bottom' ) > windowHeight && getTipOffsetTop ( 'top' ) >= 0 ) {
103
+ result = true
104
+ newPlace = 'top'
105
+ }
106
+ return { result, newPlace}
50
107
}
51
108
52
109
// Return new state to change the placement to the reverse if possible
53
- if ( place === 'left' && outsideLeft ( ) ) {
110
+ const outsideLeftResult = outsideLeft ( )
111
+ const outsideRightResult = outsideRight ( )
112
+ const outsideTopResult = outsideTop ( )
113
+ const outsideBottomResult = outsideBottom ( )
114
+
115
+ if ( place === 'left' && outsideLeftResult . result ) {
54
116
return {
55
117
isNewState : true ,
56
- newState : { place : 'right' }
118
+ newState : { place : outsideLeftResult . newPlace }
57
119
}
58
- } else if ( place === 'right' && outsideRight ( ) ) {
120
+ } else if ( place === 'right' && outsideRightResult . result ) {
59
121
return {
60
122
isNewState : true ,
61
- newState : { place : 'left' }
123
+ newState : { place : outsideRightResult . newPlace }
62
124
}
63
- } else if ( place === 'top' && outsideTop ( ) ) {
125
+ } else if ( place === 'top' && outsideTopResult . result ) {
64
126
return {
65
127
isNewState : true ,
66
- newState : { place : 'bottom' }
128
+ newState : { place : outsideTopResult . newPlace }
67
129
}
68
- } else if ( place === 'bottom' && outsideBottom ( ) ) {
130
+ } else if ( place === 'bottom' && outsideBottomResult . result ) {
69
131
return {
70
132
isNewState : true ,
71
- newState : { place : 'top' }
133
+ newState : { place : outsideBottomResult . newPlace }
72
134
}
73
135
}
74
136
@@ -109,19 +171,59 @@ const getDefaultPosition = (effect, targetWidth, targetHeight, tipWidth, tipHeig
109
171
let right
110
172
let bottom
111
173
let left
112
- const disToMouse = 15
113
- const triangleHeight = 5
174
+ const disToMouse = 8
175
+ const triangleHeight = 2
114
176
115
177
if ( effect === 'float' ) {
116
- top = { x : - ( tipWidth / 2 ) , y : - ( tipHeight + disToMouse - triangleHeight ) }
117
- bottom = { x : - ( tipWidth / 2 ) , y : disToMouse }
118
- left = { x : - ( tipWidth + disToMouse - triangleHeight ) , y : - ( tipHeight / 2 ) }
119
- right = { x : disToMouse , y : - ( tipHeight / 2 ) }
178
+ top = {
179
+ l : - ( tipWidth / 2 ) ,
180
+ r : tipWidth / 2 ,
181
+ t : - ( tipHeight + disToMouse + triangleHeight ) ,
182
+ b : - disToMouse
183
+ }
184
+ bottom = {
185
+ l : - ( tipWidth / 2 ) ,
186
+ r : tipWidth / 2 ,
187
+ t : disToMouse ,
188
+ b : tipHeight + disToMouse + triangleHeight
189
+ }
190
+ left = {
191
+ l : - ( tipWidth + disToMouse + triangleHeight ) ,
192
+ r : - disToMouse ,
193
+ t : - ( tipHeight / 2 ) ,
194
+ b : tipHeight / 2
195
+ }
196
+ right = {
197
+ l : disToMouse ,
198
+ r : tipWidth + disToMouse + triangleHeight ,
199
+ t : - ( tipHeight / 2 ) ,
200
+ b : tipHeight / 2
201
+ }
120
202
} else if ( effect === 'solid' ) {
121
- top = { x : - ( tipWidth / 2 ) , y : - ( targetHeight / 2 + tipHeight ) }
122
- bottom = { x : - ( tipWidth / 2 ) , y : targetHeight / 2 }
123
- left = { x : - ( tipWidth + targetWidth / 2 ) , y : - ( tipHeight / 2 ) }
124
- right = { x : targetWidth / 2 , y : - ( tipHeight / 2 ) }
203
+ top = {
204
+ l : - ( tipWidth / 2 ) ,
205
+ r : tipWidth / 2 ,
206
+ t : - ( targetHeight / 2 + tipHeight + triangleHeight ) ,
207
+ b : - ( targetHeight / 2 )
208
+ }
209
+ bottom = {
210
+ l : - ( tipWidth / 2 ) ,
211
+ r : tipWidth / 2 ,
212
+ t : targetHeight / 2 ,
213
+ b : targetHeight / 2 + tipHeight + triangleHeight
214
+ }
215
+ left = {
216
+ l : - ( tipWidth + targetWidth / 2 + triangleHeight ) ,
217
+ r : - ( targetWidth / 2 ) ,
218
+ t : - ( tipHeight / 2 ) ,
219
+ b : tipHeight / 2
220
+ }
221
+ right = {
222
+ l : targetWidth / 2 ,
223
+ r : tipWidth + targetWidth / 2 + triangleHeight ,
224
+ t : - ( tipHeight / 2 ) ,
225
+ b : tipHeight / 2
226
+ }
125
227
}
126
228
127
229
return { top, bottom, left, right}
0 commit comments