-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimateGroup.vue
93 lines (81 loc) · 2.58 KB
/
AnimateGroup.vue
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
<template>
<transition-group :move-class="smoothMove ? 'animate__move' : ''" @enter="animateCSS($event, 'in')" @leave="animateCSS($event, 'out')">
<slot></slot>
</transition-group>
</template>
<script>
module.exports = {
props: {
animationIn: {
type: String,
required: true
},
animationOut: {
type: String,
required: true
},
animationDuration: {
type: Number
},
/*
Specifies whether surrounding elements will occupy placement of inserted/removed element in smoothly way.
Animate change of position in list.
*/
smoothMove: {
type: Boolean,
default: true
},
/*
Instant move of surrounding elements on leave transition - removed element will be detached from normal document flow - by set its position to absolute.
Surrounding elements will be moved immediately - rather when animation ends.
Applied only for out transition
*/
instantMove: {
type: Boolean,
default: true
}
},
data: ()=>({
animationPrefix: "animate__"
}),
methods: {
animateCSS (element, type){
return new Promise((resolve) => {
let animationName;
if(type==="in"){
animationName = this.animationIn;
}
else if(type==="out"){
animationName = this.animationOut;
}
const classAnimationName = `${this.animationPrefix}${animationName}`;
if(this.instantMove && type==="out"){
element.style.position = "absolute";
}
if(this.animationDuration){
element.style.setProperty('--animate-duration', `${this.animationDuration}s`);
}
element.classList.add(`${this.animationPrefix}animated`, classAnimationName);
// When the animation ends, we clean the classes and resolve the Promise
function handleAnimationEnd() {
if(this.instantMove && type==="out"){
element.style.position = "static";
}
if(this.animationDuration){
element.style.removeProperty('--animate-duration', `${this.animationDuration}s`);
}
element.classList.remove(`${this.animationPrefix}animated`, classAnimationName);
resolve('Animation ended');
}
element.addEventListener('animationend', handleAnimationEnd, {once: true});
});
}
}
}
</script>
<style src="animate.css"></style>
<style>
.animate__move {
transition: transform 1.2s ease;
}
</style>