forked from mmccall10/el-transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (54 loc) · 2.04 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
export async function enter(element, transitionName = null) {
element.classList.remove('hidden')
await transition('enter', element, transitionName)
}
export async function leave(element, transitionName = null) {
await transition('leave', element, transitionName)
element.classList.add('hidden')
}
export async function toggle(element, transitionName = null) {
if (element.classList.contains('hidden')) {
await enter(element, transitionName)
} else {
await leave(element, transitionName)
}
}
async function transition(direction, element, animation) {
const dataset = element.dataset
const animationClass = animation ? `${animation}-${direction}` : direction
let transition = `transition${direction.charAt(0).toUpperCase() + direction.slice(1)}`
const genesis = dataset[transition] ? dataset[transition].split(" ") : [animationClass]
const start = dataset[`${transition}Start`] ? dataset[`${transition}Start`].split(" ") : [`${animationClass}-start`]
const end = dataset[`${transition}End`] ? dataset[`${transition}End`].split(" ") : [`${animationClass}-end`]
addClasses(element, genesis)
addClasses(element, start)
await nextFrame()
removeClasses(element, start)
addClasses(element, end);
await afterTransition(element)
removeClasses(element, end)
removeClasses(element, genesis)
}
function addClasses(element, classes) {
element.classList.add(...classes)
}
function removeClasses(element, classes) {
element.classList.remove(...classes)
}
function nextFrame() {
return new Promise(resolve => {
requestAnimationFrame(() => {
requestAnimationFrame(resolve)
});
});
}
function afterTransition(element) {
return new Promise(resolve => {
// safari return string with comma separate values
const computedDuration = getComputedStyle(element).transitionDuration.split(",")[0]
const duration = Number(computedDuration.replace('s', '')) * 1000;
setTimeout(() => {
resolve()
}, duration)
});
}