This repository was archived by the owner on Jun 24, 2026. It is now read-only.
forked from bsidelinger912/react-tooltip-lite
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathposition.js
More file actions
282 lines (236 loc) · 9.37 KB
/
Copy pathposition.js
File metadata and controls
282 lines (236 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @file positions.js
* @description some functions for position calculation
*/
import getDirection from './getDirection';
const bodyPadding = 10;
const minArrowPadding = 5;
const noArrowDistance = 3;
/**
* cross browser scroll positions
*/
function getScrollTop() {
return window.scrollY || document.documentElement.scrollTop || document.body.scrollTop || 0;
}
export function getScrollLeft() {
return window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
}
/**
* Sets tip max width safely for mobile
*/
function getTipMaxWidth() {
return (typeof document !== 'undefined') ? document.documentElement.clientWidth - (bodyPadding * 2) : 1000;
}
/**
* Parses align mode from direction if specified with hyphen, defaulting to middle if not -
* e.g. 'left-start' is mode 'start' and 'left' would be the default of 'middle'
*/
function parseAlignMode(direction) {
const directionArray = direction.split('-');
if (directionArray.length > 1) {
return directionArray[1];
}
return 'middle';
}
export function getArrowSpacing(props) {
const defaultArrowSpacing = props.arrow ? props.arrowSize : noArrowDistance;
return typeof props.distance === 'number' ? props.distance : defaultArrowSpacing;
}
/**
* Gets wrapper's left position for top/bottom tooltips as well as needed width restriction
*/
function getUpDownPosition(tip, target, direction, alignMode, props) {
let left = -10000000;
let top;
const arrowSpacing = getArrowSpacing(props);
if (tip) {
// get wrapper left position
const scrollLeft = getScrollLeft();
const targetRect = target.getBoundingClientRect();
const targetLeft = targetRect.left + scrollLeft;
const halfTargetWidth = Math.round(target.offsetWidth / 2);
const tipWidth = Math.min(getTipMaxWidth(), tip.offsetWidth);
const arrowCenter = targetLeft + halfTargetWidth;
const arrowLeft = arrowCenter - props.arrowSize;
const arrowRight = arrowCenter + props.arrowSize;
if (alignMode === 'start') {
left = props.arrow ? Math.min(arrowLeft, targetLeft) : targetLeft;
} else if (alignMode === 'end') {
const rightWithArrow = Math.max(arrowRight, (targetLeft + target.offsetWidth));
const rightEdge = props.arrow ? rightWithArrow : (targetLeft + target.offsetWidth);
left = Math.max(rightEdge - tipWidth, bodyPadding + scrollLeft);
} else {
const centeredLeft = (targetLeft + halfTargetWidth) - Math.round(tipWidth / 2);
const availableSpaceOnLeft = bodyPadding + scrollLeft;
left = Math.max(centeredLeft, availableSpaceOnLeft);
}
// check for right overhang
const rightOfTip = left + tipWidth;
const rightOfScreen = (scrollLeft + document.documentElement.clientWidth) - bodyPadding;
const rightOverhang = rightOfTip - rightOfScreen;
if (rightOverhang > 0) {
left -= rightOverhang;
}
if (direction === 'up') {
top = (targetRect.top + getScrollTop()) - (tip.offsetHeight + arrowSpacing);
} else {
top = targetRect.bottom + getScrollTop() + arrowSpacing;
}
}
return {
left,
top,
};
}
/**
* gets top position for left/right arrows
*/
function getLeftRightPosition(tip, target, direction, alignMode, props) {
let left = -10000000;
let top = 0;
const arrowSpacing = getArrowSpacing(props);
const arrowPadding = props.arrow ? minArrowPadding : 0;
if (tip) {
const scrollTop = getScrollTop();
const scrollLeft = getScrollLeft();
const targetRect = target.getBoundingClientRect();
const targetTop = targetRect.top + scrollTop;
const halfTargetHeight = Math.round(target.offsetHeight / 2);
const arrowTop = (targetTop + halfTargetHeight) - props.arrowSize;
const arrowBottom = targetRect.top + scrollTop + halfTargetHeight + props.arrowSize;
// TODO: handle close to edges better
if (alignMode === 'start') {
top = props.arrow ? Math.min(targetTop, arrowTop) : targetTop;
} else if (alignMode === 'end') {
const topForBottomAlign = (targetRect.bottom + scrollTop) - tip.offsetHeight;
top = props.arrow ? Math.max(topForBottomAlign, arrowBottom - tip.offsetHeight) : topForBottomAlign;
} else {
// default to middle, but don't go below body
const centeredTop = Math.max((targetTop + halfTargetHeight) - Math.round(tip.offsetHeight / 2), bodyPadding + scrollTop);
// make sure it doesn't go below the arrow
top = Math.min(centeredTop, arrowTop - arrowPadding);
}
// check for bottom overhang
const bottomOverhang = ((top - scrollTop) + tip.offsetHeight + bodyPadding) - window.innerHeight;
if (bottomOverhang > 0) {
// try to add the body padding below the tip, but don't offset too far from the arrow
top = Math.max(top - bottomOverhang, (arrowBottom + arrowPadding) - tip.offsetHeight);
}
if (direction === 'right') {
left = targetRect.right + arrowSpacing + scrollLeft;
} else {
left = (targetRect.left - arrowSpacing - tip.offsetWidth) + scrollLeft;
}
}
return {
left,
top,
};
}
/**
* sets the Arrow styles based on direction
*/
function getArrowStyles(target, direction, props) {
if (!target) {
return null;
}
const targetRect = target.getBoundingClientRect();
const halfTargetHeight = Math.round(target.offsetHeight / 2);
const halfTargetWidth = Math.round(target.offsetWidth / 2);
const scrollTop = getScrollTop();
const scrollLeft = getScrollLeft();
const arrowSpacing = getArrowSpacing(props);
const borderStyles = {};
switch (direction) {
case 'right':
borderStyles.borderTop = `${props.arrowSize}px solid transparent`;
borderStyles.borderBottom = `${props.arrowSize}px solid transparent`;
if (props.background) {
borderStyles.borderRight = `${props.arrowSize}px solid ${props.background}`;
} else {
borderStyles.borderRightWidth = `${props.arrowSize}px`;
borderStyles.borderRightStyle = 'solid';
}
return {
...borderStyles,
top: (targetRect.top + scrollTop + halfTargetHeight) - props.arrowSize,
left: (targetRect.right + scrollLeft + arrowSpacing) - props.arrowSize,
};
case 'left':
borderStyles.borderTop = `${props.arrowSize}px solid transparent`;
borderStyles.borderBottom = `${props.arrowSize}px solid transparent`;
if (props.background) {
borderStyles.borderLeft = `${props.arrowSize}px solid ${props.background}`;
} else {
borderStyles.borderLeftWidth = `${props.arrowSize}px`;
borderStyles.borderLeftStyle = 'solid';
}
return {
...borderStyles,
top: (targetRect.top + scrollTop + halfTargetHeight) - props.arrowSize,
left: (targetRect.left + scrollLeft) - arrowSpacing - 1,
};
case 'up':
borderStyles.borderLeft = `${props.arrowSize}px solid transparent`;
borderStyles.borderRight = `${props.arrowSize}px solid transparent`;
// if color is styled with css, we need everything except border-color, if styled with props, we add entire border rule
if (props.background) {
borderStyles.borderTop = `${props.arrowSize}px solid ${props.background}`;
} else {
borderStyles.borderTopWidth = `${props.arrowSize}px`;
borderStyles.borderTopStyle = 'solid';
}
return {
...borderStyles,
left: Math.min(getScrollLeft() + getTipMaxWidth() - bodyPadding, (targetRect.left + scrollLeft + halfTargetWidth) - props.arrowSize),
top: (targetRect.top + scrollTop) - arrowSpacing,
};
case 'down':
default:
borderStyles.borderLeft = `${props.arrowSize}px solid transparent`;
borderStyles.borderRight = `${props.arrowSize}px solid transparent`;
if (props.background) {
borderStyles.borderBottom = `10px solid ${props.background}`;
} else {
borderStyles.borderBottomWidth = `${props.arrowSize}px`;
borderStyles.borderBottomStyle = 'solid';
}
return {
...borderStyles,
left: Math.min(getScrollLeft() + getTipMaxWidth() - bodyPadding, (targetRect.left + scrollLeft + halfTargetWidth) - props.arrowSize),
top: (targetRect.bottom + scrollTop + arrowSpacing) - props.arrowSize,
};
}
}
/**
* Returns the positions style rules
*/
export default function positions(direction, tip, target, props) {
const alignMode = parseAlignMode(direction);
const trimmedDirection = direction.split('-')[0];
let realDirection = trimmedDirection;
if (tip) {
const testArrowStyles = props.arrow && getArrowStyles(target, trimmedDirection, props);
realDirection = getDirection(trimmedDirection, tip, target, props, bodyPadding, testArrowStyles);
}
const maxWidth = getTipMaxWidth();
// force the tip to display the width we measured everything at when visible, when scrolled
let width;
if (tip && getScrollLeft() > 0) {
// adding the exact width on the first render forces a bogus line break, so add 1px the first time
const spacer = tip.style.width ? 0 : 1;
width = Math.min(tip.offsetWidth, maxWidth) + spacer;
}
const tipPosition = (realDirection === 'up' || realDirection === 'down')
? getUpDownPosition(tip, target, realDirection, alignMode, props)
: getLeftRightPosition(tip, target, realDirection, alignMode, props);
return {
tip: {
...tipPosition,
maxWidth,
width,
},
arrow: tip && props.arrow ? getArrowStyles(target, realDirection, props) : null,
realDirection,
};
}