forked from drgarlic/svelte-class-transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transition.svelte
169 lines (142 loc) · 4.35 KB
/
Transition.svelte
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<script>
import { onMount, tick } from 'svelte';
export let toggle = undefined;
export let transitions = '';
export let inTransition = '';
export let outTransition = inTransition;
export let inState = '';
export let onState = '';
export let outState = inState;
export let offVisible = false;
let div;
let slot;
let parent;
let mounted;
const STATE = {
IDLE: 0,
ENTERING: 1,
LEAVING: 2,
};
let state = STATE.IDLE;
onMount(async () => {
mounted = true;
await tick();
slot = div.nextElementSibling;
if (toggle === undefined) {
slot.hidden = true;
if (document.readyState === 'complete') {
setTimeout(initTogglelessTransition, 50);
} else {
window.addEventListener('load', () => {
setTimeout(initTogglelessTransition, 50);
}, { once: true });
}
} else {
initTransition();
}
});
const initTogglelessTransition = async () => {
searchParentTransition();
toggle = parent ? parent.toggle : false;
initTransition();
if (! parent) {
setTimeout(() => { toggle = true }, 200);
}
};
const searchParentTransition = () => {
let element = slot.parentElement;
while (
parent === undefined
&& element
&& document.body !== element
) {
if (element.toggle !== undefined) {
parent = element;
parentObserver();
} else {
element = element.parentElement;
}
}
};
const parentObserver = () => {
new MutationObserver((mutations) => {
for (let mutation of mutations) {
toggle = mutation.target.toggle;
}
}).observe(parent, {
attributes: true,
attributeFilter: [ 'class' ]
});
};
const setClasses = (...classes) => {
const toRemove = clean(transitions, inTransition, outTransition, inState, onState, outState);
slot.classList.value = clean(slot.classList.value).split(' ').filter(c => ! toRemove.includes(c)).join(' ') + ' ' + classes.join(' ');
}
const clean = (...args) => args.join(' ').replace(/\s+/g, ' ').trim();
const initTransition = () => {
slot.toggle = toggle;
if (toggle) {
setClasses(transitions, outTransition, onState);
transitionEndListener();
} else {
slot.hidden = ! parent && ! offVisible;
setClasses(transitions, inState);
setTimeout(() => {
setClasses(transitions, inTransition, inState);
transitionEndListener();
}, 250);
}
if (! toggle) {
slot.hidden = ! parent && ! offVisible;
}
transitionEndListener();
};
const transitionEndListener = () => {
slot.addEventListener('transitionend', (event) => {
if (
(event.target.toggle !== undefined)
&& (inTransition === '' || event.target === slot)
&& ((toggle && state === STATE.ENTERING) || (! toggle && state === STATE.LEAVING))
) {
state = STATE.IDLE;
if (! toggle) {
setClasses(transitions, inTransition, inState);
slot.hidden = ! parent && ! offVisible;
}
}
});
};
let initialized = false;
let firstToggleState = toggle;
$: firstToggleState !== toggle && (initialized = true);
$: initialized && event(toggle);
const event = (toggle) => {
slot.toggle = toggle;
toggle ? enterEvent() : leaveEvent();
};
const enterEvent = () => {
if (slot.hidden) {
slot.hidden = false;
setTimeout(enterEvent, 50);
} else {
state = STATE.ENTERING;
setClasses(transitions, inTransition, onState);
}
};
const leaveEvent = () => {
state = STATE.LEAVING;
setClasses(transitions, outTransition, outState);
};
</script>
<style>
:global([hidden]) {
display: none !important;
}
</style>
<div
bind:this={div}
hidden
/>
{#if mounted}
<slot />
{/if}