-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip.js
420 lines (358 loc) · 13.8 KB
/
tooltip.js
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
import addGlobalEventListener from './utils/addGlobalEventListener.js'
const DEFAULT_SPACING = 0
const POSITION_ORDER = ['topRight', 'topLeft', 'bottomLeft', 'bottomRight', 'top', 'bottom', 'left', 'right']
const POSITION_TO_FUNCTION_MAP = {
bottomLeft: positionTooltipBottomLeft,
bottomRight: positionTooltipBottomRight,
topLeft: positionTooltipTopLeft,
topRight: positionTooltipTopRight,
top: positionTooltipTop,
bottom: positionTooltipBottom,
left: positionTooltipLeft,
right: positionTooltipRight
}
//定位tooltip用的container
const tooltipContainer = document.createElement('div')
tooltipContainer.classList.add('tooltip-container')
document.body.append(tooltipContainer)
addGlobalEventListener('mouseover', '[data-tooltip]', (e) => {
if (e.target.dataset.tooltip.trim() === '') return
tooltipContainer.innerHTML = creatTooltipHTML(e.target.dataset)
const tooltip = tooltipContainer.children[0]
setupTooltipStyle(e.target)
positionTooltip(tooltip, e.target)
e.target.addEventListener(
'mouseleave',
() => {
tooltip.remove()
},
{ once: true }
)
})
function creatTooltipHTML({ tooltip, arrow = "➤" } = {}) {
return `
<div class="tooltip-outer">
<div class="tooltip">${tooltip}</div>
<div class="tooltip-arrow tooltip-arrow-top ">${arrow}</div>
<div class="tooltip-arrow tooltip-arrow-right">${arrow}</div>
<div class="tooltip-arrow tooltip-arrow-bottom">${arrow}</div>
<div class="tooltip-arrow tooltip-arrow-left">${arrow}</div>
<div class="tooltip-arrow tooltip-arrow-bottom-left">${arrow}</div>
<div class="tooltip-arrow tooltip-arrow-top-right">${arrow}</div>
<div class="tooltip-arrow tooltip-arrow-top-left">${arrow}</div>
<div class="tooltip-arrow tooltip-arrow-bottom-right">${arrow}</div>
</div>`
}
/**
* mouseover時定位tooltip
* @param {Element} tooltip mouseover時建立的tooltip
* @param {Element} element mouseover到的element
*/
function positionTooltip(tooltip, element) {
const elementRect = element.getBoundingClientRect()
const preferredPositions = (element.dataset.positions || '').split('|')
const spacing = element.dataset.spacing ? parseInt(element.dataset.spacing) : DEFAULT_SPACING
const positions = [...preferredPositions, ...POSITION_ORDER]
//找對應的定位function執行
for (let i = 0; i < positions.length; i++) {
const func = POSITION_TO_FUNCTION_MAP[positions[i]]
if (func?.(tooltip, elementRect, spacing)) return
// 定位成功時要中斷迴圈
}
}
/**
* 檢查主要位置正確,但側邊超出時,調整側邊位置是否導致它的對邊超出spacing範圍
* @param {Element} tooltip mouseover時建立的tooltip
* @param {String} position 要修正的位置 上下左右
* @param {Number} spacing element.dataset.spacing 或 DEFAULT_SPACING
* @returns {Boolean}
*/
function isLessThanSpacing(tooltip, position, spacing) {
const posValue = parseFloat(getComputedStyle(tooltip)[position])
return posValue < spacing
}
/**
* 把tooltip顯示在element上方
* @param {Element} tooltip mouseover時建立的tooltip
* @param {Object} elementRect mouseover到的element的位置
* @param {String} spacing element.dataset.spacing 或 DEFAULT_SPACING
* @returns {Boolean} 正確定位時回傳true,告訴positionTooltip中斷迴圈
*/
function positionTooltipTop(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
const arrowToShow = 'bottom'
tooltip.style.top = `${elementRect.top - tooltipRect.height - spacing}px`
tooltip.style.left = `${elementRect.left + elementRect.width / 2 - tooltipRect.width / 2}px`
const bounds = isOutOfBound(tooltip, spacing)
if (bounds.top) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow(arrowToShow)
if (bounds.right) {
//tooltip右邊超出範圍,把右邊位置拉進範圍
tooltip.style.right = `${spacing}px`
//消除一開始定位tooltip所設的left值
tooltip.style.left = 'initial'
//調整tooltip箭頭位置
tooltip.style.setProperty('--left', `${tooltipRect.width - elementRect.width + spacing}px`)
//處理因為重設右邊位置造成左邊超出邊界的狀況
if (isLessThanSpacing(tooltip, 'left', spacing)) {
tooltip.style.left = `${spacing}px`
}
//調整箭頭位置
repositionArrow(tooltip, arrowToShow, 'justify-self', "end")
}
if (bounds.left) {
//tooltip左邊超出範圍,把左邊位置拉近範圍
tooltip.style.left = `${spacing}px`
//調整tooltip箭頭位置
tooltip.style.setProperty('--left', `${elementRect.width - spacing}px`)
//處理因為重設左邊位置而造成右邊超出邊界的狀況
if (isLessThanSpacing(tooltip, 'right', spacing)) {
tooltip.style.right = `${spacing}px`
}
//調整箭頭位置
repositionArrow(tooltip, arrowToShow, 'justify-self', "start")
}
return true
}
/**
* 把tooltip顯示在element下方
* @param {Element} tooltip mouseover時建立的tooltip
* @param {Object} elementRect mouseover到的element的位置
* @param {String} spacing element.dataset.spacing 或 DEFAULT_SPACING
* @returns {Boolean} 正確定位時回傳true,告訴positionTooltip中斷迴圈
*/
function positionTooltipBottom(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
const arrowToShow = "top"
tooltip.style.top = `${elementRect.bottom + spacing}px`
tooltip.style.left = `${elementRect.left + elementRect.width / 2 - tooltipRect.width / 2}px`
const bounds = isOutOfBound(tooltip, spacing)
if (bounds.bottom) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow(arrowToShow)
if (bounds.right) {
tooltip.style.right = `${spacing}px`
tooltip.style.left = 'initial'
tooltip.style.setProperty('--left', `${tooltipRect.width - elementRect.width + spacing}px`)
if (isLessThanSpacing(tooltip, 'left', spacing)) {
tooltip.style.left = `${spacing}px`
}
repositionArrow(tooltip, arrowToShow, 'justify-self', "end")
}
if (bounds.left) {
tooltip.style.left = `${spacing}px`
tooltip.style.setProperty('--left', `${elementRect.width - spacing}px`)
if (isLessThanSpacing(tooltip, 'right', spacing)) {
tooltip.style.right = `${spacing}px`
}
repositionArrow(tooltip, arrowToShow, 'justify-self', "start")
}
return true
}
/**
* 把tooltip顯示在element左方
* @param {Element} tooltip mouseover時建立的tooltip
* @param {Object} elementRect mouseover到的element的位置
* @param {String} spacing element.dataset.spacing 或 DEFAULT_SPACING
* @returns {Boolean} 正確定位時回傳true,告訴positionTooltip中斷迴圈
*/
function positionTooltipLeft(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
tooltip.style.top = `${elementRect.top + elementRect.height / 2 - tooltipRect.height / 2}px`
tooltip.style.left = `${elementRect.left - tooltipRect.width - spacing}px`
const bounds = isOutOfBound(tooltip, spacing)
const arrowToShow = "right"
if (bounds.left) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow(arrowToShow)
if (bounds.bottom) {
tooltip.style.bottom = `${spacing}px`
tooltip.style.top = 'initial'
if (isLessThanSpacing(tooltip, 'top', spacing)) {
tooltip.style.top = `${spacing}px`
}
repositionArrow(tooltip, arrowToShow, "align-self", "end")
}
if (bounds.top) {
tooltip.style.top = `${spacing}px`
if (isLessThanSpacing(tooltip, 'bottom', spacing)) {
tooltip.style.bottom = `${spacing}px`
}
repositionArrow(tooltip, arrowToShow, "align-self", "start")
}
return true
}
/**
* 把tooltip顯示在element右方
* @param {Element} tooltip mouseover時建立的tooltip
* @param {Object} elementRect mouseover到的element的位置
* @param {String} spacing element.dataset.spacing 或 DEFAULT_SPACING
* @returns {Boolean} 正確定位時回傳true,告訴positionTooltip中斷迴圈
*/
function positionTooltipRight(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
tooltip.style.top = `${elementRect.top + elementRect.height / 2 - tooltipRect.height / 2}px`
tooltip.style.left = `${elementRect.right + spacing}px`
const bounds = isOutOfBound(tooltip, spacing)
const arrowToShow = "left"
if (bounds.right) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow(arrowToShow)
if (bounds.bottom) {
tooltip.style.bottom = `${spacing}px`
tooltip.style.top = 'initial'
if (isLessThanSpacing(tooltip, 'top', spacing)) {
tooltip.style.top = `${spacing}px`
}
repositionArrow(tooltip, arrowToShow, 'align-self', "end")
}
if (bounds.top) {
tooltip.style.top = `${spacing}px`
if (isLessThanSpacing(tooltip, 'bottom', spacing)) {
tooltip.style.bottom = `${spacing}px`
}
repositionArrow(tooltip, arrowToShow, 'align-self', "start")
}
return true
}
/**
* 檢查最初定位的位置是否超出邊界
* @param {Element} tooltip 其實是tooltip
* @param {Number} spacing element.dataset.spacing 或 DEFAULT_SPACING
* @returns {Object} 回傳值告訴定位functions最初定位是否超出邊界
*/
function isOutOfBound(tooltip, spacing) {
const rect = tooltip.getBoundingClientRect()
const containerRect = tooltipContainer.getBoundingClientRect()
return {
left: rect.left <= containerRect.left + spacing,
right: rect.right >= containerRect.right - spacing,
top: rect.top <= containerRect.top + spacing,
bottom: rect.bottom >= containerRect.bottom - spacing
}
}
/**
* 重置tooltip方位設定值
* @param {Element} tooltip mouseover時建立的tooltip
*/
function resetTooltipPosition(tooltip) {
tooltip.style.top = 'initial'
tooltip.style.left = 'initial'
tooltip.style.right = 'initial'
tooltip.style.bottom = 'initial'
}
function positionTooltipTopLeft(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
tooltip.style.top = `${elementRect.top - tooltipRect.height - spacing}px`
tooltip.style.left = `${elementRect.left - tooltipRect.width}px`
const bounds = isOutOfBound(tooltip, spacing)
if (bounds.top || bounds.left) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow('bottom-right')
return true
}
function positionTooltipTopRight(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
tooltip.style.top = `${elementRect.top - tooltipRect.height - spacing}px`
tooltip.style.left = `${elementRect.right}px`
const bounds = isOutOfBound(tooltip, spacing)
if (bounds.top || bounds.right) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow('bottom-left')
return true
}
function positionTooltipBottomRight(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
tooltip.style.top = `${elementRect.bottom + spacing}px`
tooltip.style.left = `${elementRect.right + spacing}px`
const bounds = isOutOfBound(tooltip, spacing)
if (bounds.bottom || bounds.right) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow('top-left')
return true
}
function positionTooltipBottomLeft(tooltip, elementRect, spacing) {
const tooltipRect = tooltip.getBoundingClientRect()
tooltip.style.top = `${elementRect.bottom + spacing}px`
tooltip.style.left = `${elementRect.left - tooltipRect.width - spacing}px`
const bounds = isOutOfBound(tooltip, spacing)
if (bounds.bottom || bounds.left) {
resetTooltipPosition(tooltip)
return false
}
showTooltipArrow('top-right')
return true
}
/**
* 顯示tooltip箭頭
* @param {String} direction 要顯示的箭頭位置
*/
function showTooltipArrow(direction) {
const arrow = tooltipContainer.querySelector(`.tooltip-arrow-${direction}`)
arrow.classList.add('animate-tooltip')
}
/**
* 獲取物件的dataset,設定tooltip的外觀
* @param {Object} element 要顯示tooltip的物件
*/
function setupTooltipStyle(element) {
const { bgColor, fgColor, fontSize, arrowSize, spacing = "0", arrowDirection = "right" } = element.dataset
const ARROW_ROTATE_DEGREE = {
up: 90,
down: 270,
right: 0,
left: 180
}
const degree = `${ARROW_ROTATE_DEGREE[arrowDirection]}deg`
const start = `var(--arrow-${arrowDirection}-translate-start)`
const end = `var(--arrow-${arrowDirection}-translate-end)`
const flashArrow = `flash-arrow-${arrowDirection}`
const bgDegree = `${ARROW_ROTATE_DEGREE[arrowDirection] - 90}deg`
const arrow = tooltipContainer.querySelector('.tooltip-arrow')
const arrowFontSize = parseFloat(getComputedStyle(arrow).getPropertyValue('font-size'))
const defaultAnimateDuration = 3.5
const animateDuration = (arrowFontSize / 16) < defaultAnimateDuration
? defaultAnimateDuration + 's'
: (arrowFontSize / 16) + 's'
const customProperties = {
'--tooltip-bg-clr': bgColor ? bgColor : "#ffffff",
'--tooltip-fg-clr': fgColor ? fgColor : "#000000",
'--tooltip-fs': fontSize ? fontSize : "1rem",
'--tooltip-arrow-fs': arrowSize ? arrowSize : "1.5rem",
'--tooltip-base-rotation': degree,
'--tooltip-spacing': `${spacing / 50}rem`,
'--arrow-translate-start': start,
'--arrow-translate-end': end,
'--flash-arrow': flashArrow,
'--bg-degree': bgDegree,
'--animate-duration': animateDuration
}
for (const [property, value] of Object.entries(customProperties)) {
tooltipContainer.style.setProperty(property, value)
}
}
/**
* 在主方向沒超出邊界,沒跳下一個positionTooltip時,調整箭頭位置
* @param {Object} tooltip tooltip物件
* @param {String} arrowDirection 箭頭的位置
* @param {String} property justify-self或align-self
* @param {String} value start或end
*/
function repositionArrow(tooltip, arrowDirection, property, value) {
tooltip.parentElement.querySelector(`.tooltip-arrow-${arrowDirection}`).style.setProperty(property, value)
}