-
Notifications
You must be signed in to change notification settings - Fork 1
/
animate-display.js
141 lines (113 loc) · 2.98 KB
/
animate-display.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
interface OnionElement extends HTMLElement {
onion?: {
abort?: () => void;
}
}
interface ClassList {
[key: string]: boolean;
}
function setClasses(el: Element, classes: ClassList) {
Object.entries(classes)
.filter(([key]) => key.length)
.forEach(([key, value]) => el.classList.toggle(key, value));
}
function addEndListener(el: Element, listener: () => void) {
el.addEventListener("transitionend", listener);
el.addEventListener("animationend", listener);
}
function removeEndListener(el: Element, listener: () => void) {
el.removeEventListener("transitionend", listener);
el.removeEventListener("animationend", listener);
}
function show(el: OnionElement, token: string = "") {
if (!(el instanceof HTMLElement)) return;
if (el.classList.contains("is-opening"))
return;
el.onion?.abort?.();
if (token === "is-opening")
token = "";
const handleEnd = (event?: Event) => {
if (event?.target !== el) return;
setClasses(el, {
"is-open": true,
"is-opening": true,
"is-closing": false,
[token]: false,
});
el.onion?.abort?.();
}
const timeoutID = setTimeout(handleEnd, 2000);
el.onion = Object.assign(el.onion || {}, {
abort: () => {
removeEndListener(el, handleEnd);
clearTimeout(timeoutID);
setClasses(el, { [token]: false });
delete el.onion?.abort;
}
});
addEndListener(el, handleEnd);
setClasses(el, {
"is-open": true,
"is-opening": false,
"is-closing": false,
[token]: false,
});
setTimeout(() => setClasses(el, {
"is-open": true,
"is-opening": true,
"is-closing": false,
[token]: true,
}));
}
function hide(el: OnionElement, token: string = "") {
if (!(el instanceof HTMLElement)) return;
if (el.classList.contains("is-closing") || !el.classList.contains("is-open"))
return;
el.onion?.abort?.();
if (token === "is-closing")
token = "";
const handleEnd = (event?: Event) => {
if (event?.target !== el) return;
setClasses(el, {
"is-open": false,
"is-opening": false,
"is-closing": false,
[token]: false,
});
el.onion?.abort?.();
}
const timeoutID = setTimeout(handleEnd, 2000);
el.onion = Object.assign(el.onion || {}, {
abort: () => {
removeEndListener(el, handleEnd);
clearTimeout(timeoutID);
setClasses(el, { [token]: false });
delete el.onion?.abort;
}
});
addEndListener(el, handleEnd);
setClasses(el, {
"is-open": true,
"is-opening": false,
"is-closing": true,
[token]: true,
});
}
function toggle(el: OnionElement, force?: boolean, openingToken: string = "", closingToken: string = "") {
if (!(el instanceof HTMLElement)) return;
if (typeof force === 'undefined') {
if (el.classList.contains('is-opening')) {
hide(el, closingToken);
} else {
show(el, openingToken);
}
} else {
if (force) show(el, openingToken);
else hide(el, closingToken);
}
}
export default {
show,
hide,
toggle
};