forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathtooltip.ts
378 lines (340 loc) · 10.2 KB
/
tooltip.ts
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
import { Utils } from './utils';
import { Bounding } from './bounding';
import { Component, BaseOptions, InitElements, MElement } from './component';
export type TooltipPosition = 'top' | 'right' | 'bottom' | 'left';
export interface TooltipOptions extends BaseOptions {
/**
* Delay time before tooltip disappears.
* @default 200
*/
exitDelay: number;
/**
* Delay time before tooltip appears.
* @default 0
*/
enterDelay: number;
/**
* Element Id for the tooltip.
* @default ""
*/
tooltipId?: string;
/**
* Text string for the tooltip.
* @default ""
*/
text: string;
/**
* Set distance tooltip appears away from its activator
* excluding transitionMovement.
* @default 5
*/
margin: number;
/**
* Enter transition duration.
* @default 300
*/
inDuration: number;
/**
* Opacity of the tooltip.
* @default 1
*/
opacity: number;
/**
* Exit transition duration.
* @default 250
*/
outDuration: number;
/**
* Set the direction of the tooltip.
* @default 'bottom'
*/
position: TooltipPosition;
/**
* Amount in px that the tooltip moves during its transition.
* @default 10
*/
transitionMovement: number;
}
const _defaults: TooltipOptions = {
exitDelay: 200,
enterDelay: 0,
text: '',
margin: 5,
inDuration: 250,
outDuration: 200,
position: 'bottom' as TooltipPosition,
transitionMovement: 10,
opacity: 1
};
export class Tooltip extends Component<TooltipOptions> {
/**
* If tooltip is open.
*/
isOpen: boolean;
/**
* If tooltip is hovered.
*/
isHovered: boolean;
/**
* If tooltip is focused.
*/
isFocused: boolean;
tooltipEl: HTMLElement;
private _exitDelayTimeout: string | number | NodeJS.Timeout;
private _enterDelayTimeout: string | number | NodeJS.Timeout;
xMovement: number;
yMovement: number;
constructor(el: HTMLElement, options: Partial<TooltipOptions>) {
super(el, options, Tooltip);
this.el['M_Tooltip'] = this;
this.options = {
...Tooltip.defaults,
...this._getAttributeOptions(),
...options
};
this.isOpen = false;
this.isHovered = false;
this.isFocused = false;
this._appendTooltipEl();
this._setupEventHandlers();
}
static get defaults(): TooltipOptions {
return _defaults;
}
/**
* Initializes instance of Tooltip.
* @param el HTML element.
* @param options Component options.
*/
static init(el: HTMLElement, options?: Partial<TooltipOptions>): Tooltip;
/**
* Initializes instances of Tooltip.
* @param els HTML elements.
* @param options Component options.
*/
static init(els: InitElements<MElement>, options?: Partial<TooltipOptions>): Tooltip[];
/**
* Initializes instances of Tooltip.
* @param els HTML elements.
* @param options Component options.
*/
static init(
els: HTMLElement | InitElements<MElement>,
options: Partial<TooltipOptions> = {}
): Tooltip | Tooltip[] {
return super.init(els, options, Tooltip);
}
static getInstance(el: HTMLElement): Tooltip {
return el['M_Tooltip'];
}
destroy() {
this.tooltipEl.remove();
this._removeEventHandlers();
this.el['M_Tooltip'] = undefined;
}
_appendTooltipEl() {
this.tooltipEl = document.createElement('div');
this.tooltipEl.classList.add('material-tooltip');
const tooltipContentEl = this.options.tooltipId
? document.getElementById(this.options.tooltipId)
: document.createElement('div');
this.tooltipEl.append(tooltipContentEl);
tooltipContentEl.style.display = '';
tooltipContentEl.classList.add('tooltip-content');
this._setTooltipContent(tooltipContentEl);
this.tooltipEl.appendChild(tooltipContentEl);
document.body.appendChild(this.tooltipEl);
}
_setTooltipContent(tooltipContentEl: HTMLElement) {
if (this.options.tooltipId) return;
tooltipContentEl.innerText = this.options.text;
}
_updateTooltipContent() {
this._setTooltipContent(this.tooltipEl.querySelector('.tooltip-content'));
}
_setupEventHandlers() {
this.el.addEventListener('mouseenter', this._handleMouseEnter);
this.el.addEventListener('mouseleave', this._handleMouseLeave);
this.el.addEventListener('focus', this._handleFocus, true);
this.el.addEventListener('blur', this._handleBlur, true);
}
_removeEventHandlers() {
this.el.removeEventListener('mouseenter', this._handleMouseEnter);
this.el.removeEventListener('mouseleave', this._handleMouseLeave);
this.el.removeEventListener('focus', this._handleFocus, true);
this.el.removeEventListener('blur', this._handleBlur, true);
}
/**
* Show tooltip.
*/
open = (isManual: boolean) => {
if (this.isOpen) return;
isManual = isManual === undefined ? true : undefined; // Default value true
this.isOpen = true;
// Update tooltip content with HTML attribute options
this.options = { ...this.options, ...this._getAttributeOptions() };
this._updateTooltipContent();
this._setEnterDelayTimeout(isManual);
};
/**
* Hide tooltip.
*/
close = () => {
if (!this.isOpen) return;
this.isHovered = false;
this.isFocused = false;
this.isOpen = false;
this._setExitDelayTimeout();
};
_setExitDelayTimeout() {
clearTimeout(this._exitDelayTimeout);
this._exitDelayTimeout = setTimeout(() => {
if (this.isHovered || this.isFocused) return;
this._animateOut();
}, this.options.exitDelay);
}
_setEnterDelayTimeout(isManual) {
clearTimeout(this._enterDelayTimeout);
this._enterDelayTimeout = setTimeout(() => {
if (!this.isHovered && !this.isFocused && !isManual) return;
this._animateIn();
}, this.options.enterDelay);
}
_positionTooltip() {
const tooltip: HTMLElement = this.tooltipEl;
const origin = this.el as HTMLElement,
originHeight = origin.offsetHeight,
originWidth = origin.offsetWidth,
tooltipHeight = tooltip.offsetHeight,
tooltipWidth = tooltip.offsetWidth,
margin = this.options.margin;
this.xMovement = 0;
this.yMovement = 0;
let targetTop = origin.getBoundingClientRect().top + Utils.getDocumentScrollTop();
let targetLeft = origin.getBoundingClientRect().left + Utils.getDocumentScrollLeft();
if (this.options.position === 'top') {
targetTop += -tooltipHeight - margin;
targetLeft += originWidth / 2 - tooltipWidth / 2;
this.yMovement = -this.options.transitionMovement;
} else if (this.options.position === 'right') {
targetTop += originHeight / 2 - tooltipHeight / 2;
targetLeft += originWidth + margin;
this.xMovement = this.options.transitionMovement;
} else if (this.options.position === 'left') {
targetTop += originHeight / 2 - tooltipHeight / 2;
targetLeft += -tooltipWidth - margin;
this.xMovement = -this.options.transitionMovement;
} else {
targetTop += originHeight + margin;
targetLeft += originWidth / 2 - tooltipWidth / 2;
this.yMovement = this.options.transitionMovement;
}
const newCoordinates = this._repositionWithinScreen(
targetLeft,
targetTop,
tooltipWidth,
tooltipHeight
);
tooltip.style.top = newCoordinates.y + 'px';
tooltip.style.left = newCoordinates.x + 'px';
}
_repositionWithinScreen(x: number, y: number, width: number, height: number) {
const scrollLeft = Utils.getDocumentScrollLeft();
const scrollTop = Utils.getDocumentScrollTop();
let newX = x - scrollLeft;
let newY = y - scrollTop;
const bounding: Bounding = {
left: newX,
top: newY,
width: width,
height: height
};
const offset = this.options.margin + this.options.transitionMovement;
const edges = Utils.checkWithinContainer(document.body, bounding, offset);
if (edges.left) {
newX = offset;
} else if (edges.right) {
newX -= newX + width - window.innerWidth;
}
if (edges.top) {
newY = offset;
} else if (edges.bottom) {
newY -= newY + height - window.innerHeight;
}
return {
x: newX + scrollLeft,
y: newY + scrollTop
};
}
_animateIn() {
this._positionTooltip();
this.tooltipEl.style.visibility = 'visible';
const duration = this.options.inDuration;
// easeOutCubic
this.tooltipEl.style.transition = `
transform ${duration}ms ease-out,
opacity ${duration}ms ease-out`;
setTimeout(() => {
this.tooltipEl.style.transform = `translateX(${this.xMovement}px) translateY(${this.yMovement}px)`;
this.tooltipEl.style.opacity = (this.options.opacity || 1).toString();
}, 1);
}
_animateOut() {
const duration = this.options.outDuration;
// easeOutCubic
this.tooltipEl.style.transition = `
transform ${duration}ms ease-out,
opacity ${duration}ms ease-out`;
setTimeout(() => {
this.tooltipEl.style.transform = `translateX(0px) translateY(0px)`;
this.tooltipEl.style.opacity = '0';
}, 1);
/*
anim.remove(this.tooltipEl);
anim({
targets: this.tooltipEl,
opacity: 0,
translateX: 0,
translateY: 0,
duration: this.options.outDuration,
easing: 'easeOutCubic'
});
*/
}
_handleMouseEnter = () => {
this.isHovered = true;
this.isFocused = false; // Allows close of tooltip when opened by focus.
this.open(false);
};
_handleMouseLeave = () => {
this.isHovered = false;
this.isFocused = false; // Allows close of tooltip when opened by focus.
this.close();
};
_handleFocus = () => {
if (Utils.tabPressed) {
this.isFocused = true;
this.open(false);
}
};
_handleBlur = () => {
this.isFocused = false;
this.close();
};
_getAttributeOptions(): Partial<TooltipOptions> {
const attributeOptions: Partial<TooltipOptions> = {};
const tooltipTextOption = this.el.getAttribute('data-tooltip');
const tooltipId = this.el.getAttribute('data-tooltip-id');
const positionOption = this.el.getAttribute('data-position');
if (tooltipTextOption) {
attributeOptions.text = tooltipTextOption;
}
if (positionOption) {
attributeOptions.position = positionOption as TooltipPosition;
}
if (tooltipId) {
attributeOptions.tooltipId = tooltipId;
}
return attributeOptions;
}
}