-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.js
51 lines (48 loc) · 1.17 KB
/
utils.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
const hasDocument = typeof document !== 'undefined';
const vendorEvents = [
{
hidden: 'hidden',
event: 'visibilitychange',
state: 'visibilityState',
},
{
hidden: 'webkitHidden',
event: 'webkitvisibilitychange',
state: 'webkitVisibilityState',
},
{
hidden: 'mozHidden',
event: 'mozvisibilitychange',
state: 'mozVisibilityState',
},
{
hidden: 'msHidden',
event: 'msvisibilitychange',
state: 'msVisibilityState',
},
{
hidden: 'oHidden',
event: 'ovisibilitychange',
state: 'oVisibilityState',
},
];
export const isSupported = hasDocument && Boolean(document.addEventListener);
export const visibility = (() => {
if (!isSupported) {
return null;
}
for (let event of vendorEvents) {
if (event.hidden in document) {
return event;
}
}
// otherwise it's not supported
return null;
})();
export const getHandlerArgs = () => {
if (!visibility) {
return [true, 'visible'];
}
const { hidden, state } = visibility;
return [!document[hidden], document[state]];
};