-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (72 loc) · 2.18 KB
/
index.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
(function () {
var currentHoverVisible = false
var modifierKeys = [
16, // shift
17, // control
18, // alt
27, // esc
91, // Windows key / left Apple cmd
93 // Windows menu / right Apple cmd
]
function supportsPassiveEvent () {
var exist = false
try {
var opts = Object.defineProperty({}, 'passive', {
get: function () { exist = true }
})
window.addEventListener('test', null, opts)
} catch (e) {}
return exist
}
function init () {
// set useCapture to true to capture all events
// some components like Boostrap Dropdown menu call stopPropagate()
var useCapture = true
var options = supportsPassiveEvent()
? { passive: true, capture: useCapture }
: useCapture
// listen pointer events (mouse, pen, touch)
if (window.PointerEvent) {
window.addEventListener('pointerover', onPointerOver, options)
// keyboard events
window.addEventListener('keydown', onKeyDown, useCapture)
return
}
// Safari (iOS and macOS) does not support pointerover events yet
if ('ontouchstart' in window) {
// on iOS Safari, we need this event to enable any :active state
window.addEventListener('touchstart', function () { }, options)
} else {
// we must set hover-visible on macOS Safari
updateDoc(true)
// force to apply the new hover style
// this is a bad trick for Safari
document.body.style.pointerEvents = 'none'
window.requestAnimationFrame(function () {
document.body.style.pointerEvents = 'auto'
})
}
}
function onPointerOver (event) {
updateDoc(event.pointerType === 'mouse')
}
function onKeyDown (event) {
if (modifierKeys.indexOf(event.which) > -1) return
updateDoc(false)
}
function updateDoc (hoverVisible) {
if (currentHoverVisible === hoverVisible) return
currentHoverVisible = hoverVisible
var body = document.body
if (hoverVisible) {
body.dataset.hoverVisible = ''
} else {
delete body.dataset.hoverVisible
}
}
if (document.readyState === 'interactive') {
init()
} else {
document.addEventListener('DOMContentLoaded', init)
}
})()