aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.mjs')
-rw-r--r--src/utils.mjs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/utils.mjs b/src/utils.mjs
new file mode 100644
index 0000000..4c58dc1
--- /dev/null
+++ b/src/utils.mjs
@@ -0,0 +1,48 @@
1// Disconnect MutationObserver if element is removed
2export const onRemove = (element, callback) => {
3 const parent = element.parentNode;
4 if (!parent) throw new Error("The node must already be attached");
5
6 const obs = new MutationObserver(mutations => {
7 for (const mutation of mutations) {
8 for (const el of mutation.removedNodes) {
9 if (el === element) {
10 obs.disconnect();
11 callback();
12 }
13 }
14 }
15 });
16 obs.observe(parent, { childList: true, });
17}
18
19// Animation for new rectangle
20export const animateRectTransition = (child, rect, resume = false) => {
21 const { width: w1, height: h1, left: x1, top: y1 } = rect
22 const { width: w2, height: h2, left: x2, top: y2 } = child.getBoundingClientRect()
23
24 const rw = w1 / w2
25 const rh = h1 / h2
26 const dx = x1 - x2;
27 const dy = y1 - y2;
28
29 if (dx === 0 && dy === 0) {
30 return;
31 }
32
33 const transform1 = `translate(0, 0) scale(1, 1)`;
34 const transform2 = `translate(${dx}px, ${dy}px) scale(${rw}, ${rh})`;
35 const keyframes = [
36 { transform: transform1, opacity: 1 },
37 { transform: transform2, opacity: 0.3 },
38 ]
39
40 return child.animate(
41 resume
42 ? keyframes.reverse()
43 : keyframes,
44 {
45 duration: 300,
46 easing: 'ease-in-out',
47 });
48}