|
11 | 11 |
|
12 | 12 | let { |
13 | 13 | class: klass = '', |
14 | | - as = 'g' as T, |
15 | | - initial = (node) => ({}), |
16 | | - enter = (node) => ({}), |
17 | | - exit = (node) => ({}), |
18 | | - animate = (node) => {}, |
| 14 | + as = 'div' as T, |
| 15 | + global = true, |
| 16 | + initial = undefined, |
| 17 | + enter = undefined, |
| 18 | + exit = undefined, |
| 19 | + animate = undefined, |
19 | 20 | onmount = undefined, |
20 | 21 | ondestroy = undefined, |
21 | 22 | children = undefined, |
|
24 | 25 |
|
25 | 26 | let node = $state<Element>(); |
26 | 27 |
|
27 | | - let hasIntroTransition = $state(false); |
28 | | - let isEntered = false; |
29 | | -
|
30 | | - const checkIfEntered = () => isEntered; |
| 28 | + let hasIntroTransitionStarted: boolean | undefined = $state(undefined); |
| 29 | + let skipFirstAnimate = true; |
31 | 30 |
|
32 | 31 | $effect(() => { |
| 32 | + if (!node) return; |
| 33 | +
|
33 | 34 | const unmount = untrack(() => onmount?.(node!)); |
34 | 35 |
|
35 | | - if (!hasIntroTransition) { |
36 | | - isEntered = true; |
| 36 | + if (!enter || typeof hasIntroTransitionStarted === 'undefined') { |
| 37 | + skipFirstAnimate = false; |
37 | 38 | } |
38 | 39 |
|
39 | 40 | return () => { |
40 | | - if (typeof unmount === 'function') unmount(node); |
| 41 | + if (typeof unmount === 'function') unmount(node!); |
41 | 42 | ondestroy?.(node!); |
42 | 43 | }; |
43 | 44 | }); |
44 | 45 |
|
| 46 | + $effect(() => { |
| 47 | + if (!node) return; |
| 48 | +
|
| 49 | + if (skipFirstAnimate) { |
| 50 | + skipFirstAnimate = false; |
| 51 | + return; |
| 52 | + } |
| 53 | +
|
| 54 | + animate?.(node); |
| 55 | + }); |
| 56 | +
|
45 | 57 | const elementProps = $derived({ |
46 | 58 | [createAttachmentKey()]: (n: Element) => { |
47 | 59 | node = n; |
48 | 60 | }, |
49 | | - [createAttachmentKey()]: _animate, |
50 | | -
|
51 | 61 | class: cn(toClassValue(klass)), |
52 | 62 | onintrostart: () => { |
53 | | - hasIntroTransition = true; |
54 | | - isEntered = false; |
| 63 | + hasIntroTransitionStarted = true; |
55 | 64 | }, |
56 | 65 | onintroend: () => { |
57 | | - isEntered = true; |
58 | | - }, |
59 | | - onoutroend: () => { |
60 | | - isEntered = false; |
| 66 | + hasIntroTransitionStarted = false; |
61 | 67 | }, |
62 | 68 | ...restProps |
63 | 69 | }); |
64 | 70 |
|
65 | 71 | function _enter(node: Element) { |
66 | 72 | initial?.(node); |
67 | | -
|
68 | | - return () => enter(node); |
| 73 | + return () => { |
| 74 | + const { duration = 0, delay = 0, easing = undefined } = enter?.(node) ?? {}; |
| 75 | + return { duration, delay, easing }; |
| 76 | + }; |
69 | 77 | } |
70 | 78 |
|
71 | 79 | function _exit(node: Element) { |
72 | | - return () => exit(node); |
73 | | - } |
74 | | -
|
75 | | - function _animate(node: Element) { |
76 | | - if (!checkIfEntered()) return; |
77 | | -
|
78 | | - return animate(node); |
| 80 | + return () => { |
| 81 | + const { duration = 0, delay = 0, easing = undefined } = exit?.(node) ?? {}; |
| 82 | + return { duration, delay, easing }; |
| 83 | + }; |
79 | 84 | } |
80 | 85 | </script> |
81 | 86 |
|
|
0 commit comments