-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTooltip.jsx
429 lines (372 loc) · 15 KB
/
Tooltip.jsx
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* eslint react/no-did-update-set-state: 0 */
'use strict';
const React = require('react');
const classNames = require('classnames');
const lodashDebounce = require('lodash.debounce');
const styles = require('./styles/Tooltip.css');
const oppositeDirections = {
top: 'bottom',
bottom: 'top',
left: 'right',
right: 'left'
};
function getWindowBounds() {
return {
top: 0,
left: 0,
right: window.innerWidth,
bottom: window.innerHeight
};
}
function getHalfDimensions(element) {
return function (type) {
if (element.width && type === 'width') {
return element.width / 2;
}
if (element.height && type === 'height') {
return element.height / 2;
}
return 0;
};
}
class Tooltip extends React.Component {
constructor(props) {
super(props);
this.state = {
top: 0,
left: 0,
moved: false,
flippedFrom: null,
tooltipDirection: props.tooltipDirection,
isVisible: props.isVisible
};
this._checkOutsideClick = this._checkOutsideClick.bind(this);
}
componentDidMount() {
if (this.state.isVisible) {
this._adjustPosition();
}
if (this.props.closeOnOutsideClick) {
document.addEventListener('click', this._checkOutsideClick, true);
this.removeOutsideClickListener = () => {
document.removeEventListener('click', this._checkOutsideClick);
}
}
}
componentWillReceiveProps(nextProps) {
if (!this.props.isVisible && nextProps.isVisible) {
this.setState({
tooltipDirection: nextProps.tooltipDirection || this.state.tooltipDirection
});
}
this.setState({
isVisible: nextProps.isVisible
});
}
componentDidUpdate() {
const { isVisible, moved } = this.state;
const { debounce } = this.props;
const adjustPositionDebounced = lodashDebounce(this._adjustPosition, debounce).bind(this);
const shouldAdjust = isVisible && !moved;
if (shouldAdjust && debounce) {
adjustPositionDebounced();
} else if (shouldAdjust) {
this._adjustPosition();
} else if (!isVisible && moved) {
// make sure the position gets adjusted next time the tooltip is opened
this.setState({
moved: false,
flippedFrom: null
});
}
}
componentWillUnmount() {
if (this.removeOutsideClickListener) {
this.removeOutsideClickListener();
}
}
_checkOutsideClick(e) {
if (this.state.isVisible && !this._tooltip.contains(e.target)) {
this.props.onCloseClick();
}
}
_getInitialTooltipPosition(elementRect, tooltipDirection, rootRect) {
const { hasTriangle, triangleSize } = this.props;
const triangleSpacing = hasTriangle ? triangleSize : (triangleSize / 2);
const elementDimensions = getHalfDimensions(elementRect);
const rootDimensions = getHalfDimensions(rootRect);
const elementHalfWidth = elementDimensions('width');
const elementHalfHeight = elementDimensions('height');
const rootHalfWidth = rootDimensions('width');
const rootHalfHeight = rootDimensions('height');
const left = -1 * elementHalfWidth + rootHalfWidth;
const top = -1 * elementHalfHeight + rootHalfHeight;
const triangleLeft = elementHalfWidth - triangleSpacing;
const triangleTop = elementHalfHeight - (triangleSize / 2);
const spacing = triangleSpacing + 3;
const positions = {
top: {
top: -1 * (elementRect.height + spacing),
left,
triangleLeft
},
bottom: {
top: rootRect.height + spacing,
left,
triangleLeft
},
left: {
top,
left: -1 * (elementRect.width + spacing),
triangleTop
},
right: {
top,
left: rootRect.width + spacing,
triangleTop
}
};
return positions[tooltipDirection];
}
_getElementsPositions() {
return {
elementRect: this._tooltip.childNodes[0].getBoundingClientRect(),
rootRect: this._tooltip.parentNode.getBoundingClientRect()
};
}
/**
* Adjusts the position of the tooltip element so that it's centered if possible. If it can't be centered because
* it would go offscreen, then offset the position so the whole tooltip is still displayed onscreen.
* @param {Object} elementRect A rectangle indicating the dimensions of the tooltip element
*/
_adjustPosition() {
if (!this.state.isVisible) {
return;
}
const { elementRect, rootRect } = this._getElementsPositions();
const { tooltipDirection } = this.state;
const { positionThresholds: thresholds, triangleSize } = this.props;
const position = this._getInitialTooltipPosition(elementRect, tooltipDirection, rootRect);
// determine the area against which we are going to check the thresholds
const bounds = this.props.getBounds();
const overflowTop = rootRect.top - Math.abs(position.top) - bounds.top;
const overflowBottom = bounds.bottom - (rootRect.top + Math.abs(position.top) + elementRect.height);
const overflowLeft = (rootRect.left + position.left) - bounds.left;
const overflowRight = bounds.right - (rootRect.left + (position.left + elementRect.width));
const determineThresholds = {
top: () => overflowTop,
bottom: () => overflowBottom,
left: () => overflowLeft,
right: () => overflowRight
};
const adjustHorizontal = () => {
const thresholdsLeft = overflowLeft - thresholds.left;
const thresholdsRight = overflowRight - thresholds.right;
if (overflowLeft < thresholds.left) {
position.left -= thresholdsLeft;
position.triangleLeft += thresholdsLeft;
this.props.onThresholdPassed();
}
if (overflowRight < thresholds.right) {
position.left += thresholdsRight;
position.triangleLeft -= thresholdsRight;
this.props.onThresholdPassed();
}
};
const adjustVertical = () => {
const thresholdsTop = overflowTop - thresholds.top;
const thresholdsBottom = overflowBottom + elementRect.height / 2;
const triangleHalfSize = triangleSize / 2;
const overflowTriangleTop = elementRect.height - triangleHalfSize - triangleSize;
if (overflowTop < thresholds.top) {
position.top -= thresholdsTop;
position.triangleTop += thresholdsTop;
this.props.onThresholdPassed();
}
if (thresholdsBottom < thresholds.bottom) {
position.top += thresholdsBottom + thresholds.bottom;
position.triangleTop -= thresholdsBottom + thresholds.bottom;
this.props.onThresholdPassed();
}
if (position.triangleTop <= 0) {
position.triangleTop = triangleHalfSize;
} else if (position.triangleTop > overflowTriangleTop) {
position.triangleTop = overflowTriangleTop;
}
};
const changeDirection = direction => {
const oldState = tooltipDirection;
this.setState({
tooltipDirection: direction,
moved: false,
flippedFrom: oldState
});
this.props.onThresholdPassed();
};
/*
This has the effect that if the tooltip doesn't fit on both the top and the bottom, we default to
displaying it on the top as per the spec
*/
const canFlip = () => {
// if the previous tooltip direction was 'top' (i.e. it's on the bottom now), we can still flip back to the top
return !this.props.lockDirection && (!this.state.flippedFrom || this.state.flippedFrom === 'top');
};
const changeDirectionIfNecessary = (changed, direction) => {
if (!changed && canFlip() && (tooltipDirection === direction)) {
const overflow = determineThresholds[direction]();
if (overflow < thresholds[direction]) {
changeDirection(oppositeDirections[direction]);
changed = true;
}
}
return changed;
};
const verticalOrientations = ['top', 'bottom'];
const horizontalOrientations = ['left', 'right'];
/*
If the tooltip is displayed on the top or bottom, adjust the horizontal position so it's all displayed inside
the window and flip it from top->bottom or vice-versa if necessary
*/
if (verticalOrientations.indexOf(tooltipDirection) >= 0) {
adjustHorizontal();
if (verticalOrientations.reduce(changeDirectionIfNecessary, false)) {
return;
}
}
/*
If the tooltip is displayed on the left or right flip it from left->right or vice-versa if necessary
*/
if (horizontalOrientations.indexOf(tooltipDirection) >= 0) {
adjustVertical();
if (horizontalOrientations.reduce(changeDirectionIfNecessary, false)) {
return;
}
}
this.setState(Object.assign({
moved: true
}, position));
}
/**
* Renders the tooltip contents if this.state.isVisible is true
* @param {Object} style The style of the tooltip container
*/
_renderContents(containerStyle) {
const { className, children, hasTriangle, hasClose, containerClass, onClick } = this.props;
const { tooltipDirection } = this.state;
const tooltipClass = classNames(styles[tooltipDirection], styles.container, className);
const tooltipInnerClass = classNames(styles.inner, containerClass);
return (
<div className={tooltipClass} style={containerStyle} onClick={onClick}>
<div className={tooltipInnerClass}>
{children}
</div>
{hasTriangle ? this._renderTriangle(tooltipDirection) : null}
{hasClose ? this._renderClose() : null}
</div>
);
}
/**
* Renders the tooltip triangle if this.props.hasTriangle is true
*/
_renderTriangle(direction) {
const { triangleSize, triangleClass } = this.props;
const { triangleLeft: left, triangleTop: top } = this.state;
const triangleClassName = classNames(styles.triangle, triangleClass);
const triangleDirectionPosition = -1 * triangleSize;
const triangleStyle = {
top: {
left,
bottom: triangleDirectionPosition + 1
},
bottom: {
left,
top: triangleDirectionPosition + 1
},
left: {
top,
right: triangleDirectionPosition - 4
},
right: {
top,
left: triangleDirectionPosition - 4
}
};
return (
<svg className={triangleClassName} style={triangleStyle[direction]} width={triangleSize * 2} height={triangleSize} viewBox="0 0 20 10">
<polygon points="9.65217424 0 19.3043485 9.25000032 0 9.25000032"></polygon>
<polygon className={styles.triangleStroke} strokeLinecap="square" points="9.65723177 0.421508695 9.66228432 0.416666667 9.6572594 0.421535177 18.4448983 8.84302248 18.3478314 8.84165446 9.65723177 0.42156195 0.966632149 8.84165446 0.869565217 8.84302248 9.65720414 0.421535177 9.65217922 0.416666667"></polygon>
</svg>
);
}
/**
* Renders the tooltip close if this.props.hasClose is true
*/
_renderClose() {
const { closeClass, onCloseClick } = this.props;
const closeClassName = classNames(styles.close, closeClass);
return (
<a href="#0" onClick={onCloseClick}>
<svg className={closeClassName} viewBox="0 0 25 25">
<path d="M13.7001992,12.5 L24.7011952,23.500996 C25.0348606,23.8346614 25.0348606,24.37251 24.7011952,24.7011952 C24.3675299,25.0298805 23.8296813,25.0348606 23.500996,24.7011952 L12.5,13.7001992 L1.49900398,24.7011952 C1.16533865,25.0348606 0.62749004,25.0348606 0.298804781,24.7011952 C-0.0298804781,24.3675299 -0.0348605578,23.8296813 0.298804781,23.500996 L11.2998008,12.5 L0.298804781,1.49900398 C-0.0348605578,1.16533865 -0.0348605578,0.62749004 0.298804781,0.298804781 C0.63247012,-0.0298804781 1.17031873,-0.0348605578 1.49900398,0.298804781 L12.5,11.2998008 L23.500996,0.298804781 C23.8346614,-0.0348605578 24.37251,-0.0348605578 24.7011952,0.298804781 C25.0298805,0.63247012 25.0348606,1.17031873 24.7011952,1.49900398 L13.7001992,12.5 L13.7001992,12.5 Z"></path>
</svg>
</a>
);
}
render() {
const { top, left } = this.state;
const containerStyle = { top, left };
return (
<div ref={tooltip => { this._tooltip = tooltip; }}>
{this.state.isVisible ? this._renderContents(containerStyle) : null}
</div>
);
}
}
Tooltip.propTypes = {
children: React.PropTypes.node,
tooltipDirection: React.PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
lockDirection: React.PropTypes.bool,
isVisible: React.PropTypes.bool,
hasTriangle: React.PropTypes.bool,
hasClose: React.PropTypes.bool,
closeOnOutsideClick: React.PropTypes.bool,
className: React.PropTypes.string,
containerClass: React.PropTypes.string,
triangleClass: React.PropTypes.string,
closeClass: React.PropTypes.string,
debounce: React.PropTypes.number,
triangleSize: React.PropTypes.number,
positionThresholds: React.PropTypes.shape({
top: React.PropTypes.number,
bottom: React.PropTypes.number,
left: React.PropTypes.number,
right: React.PropTypes.number
}),
onClick: React.PropTypes.func,
onCloseClick: React.PropTypes.func,
onThresholdPassed: React.PropTypes.func,
getBounds: React.PropTypes.func
};
Tooltip.defaultProps = {
className: '',
containerClass: styles.defaultStyle,
triangleClass: styles.defaultTriangle,
debounce: 0,
triangleSize: 10,
tooltipDirection: 'top',
lockDirection: false,
positionThresholds: {
top: 10,
bottom: 10,
left: 10,
right: 10
},
onClick: () => {},
onCloseClick: () => {},
onThresholdPassed: () => {},
getBounds: getWindowBounds,
hasTriangle: true,
hasClose: false,
closeOnOutsideClick: false,
};
module.exports = Tooltip;