Skip to content

Commit 1a6e7a0

Browse files
authored
Merge pull request #1 from svelte-atoms/fix/imp-svg-element
fix: improve svg element implementation
2 parents a3b19c5 + 2ac05c8 commit 1a6e7a0

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@svelte-atoms/core",
3-
"version": "1.0.0-alpha.14",
3+
"version": "1.0.0-alpha.15",
44
"description": "A modular, accessible, and extensible Svelte UI component library.",
55
"repository": {
66
"type": "git",

src/lib/components/element/svg-element.svelte

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
1212
let {
1313
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,
1920
onmount = undefined,
2021
ondestroy = undefined,
2122
children = undefined,
@@ -24,58 +25,62 @@
2425
2526
let node = $state<Element>();
2627
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;
3130
3231
$effect(() => {
32+
if (!node) return;
33+
3334
const unmount = untrack(() => onmount?.(node!));
3435
35-
if (!hasIntroTransition) {
36-
isEntered = true;
36+
if (!enter || typeof hasIntroTransitionStarted === 'undefined') {
37+
skipFirstAnimate = false;
3738
}
3839
3940
return () => {
40-
if (typeof unmount === 'function') unmount(node);
41+
if (typeof unmount === 'function') unmount(node!);
4142
ondestroy?.(node!);
4243
};
4344
});
4445
46+
$effect(() => {
47+
if (!node) return;
48+
49+
if (skipFirstAnimate) {
50+
skipFirstAnimate = false;
51+
return;
52+
}
53+
54+
animate?.(node);
55+
});
56+
4557
const elementProps = $derived({
4658
[createAttachmentKey()]: (n: Element) => {
4759
node = n;
4860
},
49-
[createAttachmentKey()]: _animate,
50-
5161
class: cn(toClassValue(klass)),
5262
onintrostart: () => {
53-
hasIntroTransition = true;
54-
isEntered = false;
63+
hasIntroTransitionStarted = true;
5564
},
5665
onintroend: () => {
57-
isEntered = true;
58-
},
59-
onoutroend: () => {
60-
isEntered = false;
66+
hasIntroTransitionStarted = false;
6167
},
6268
...restProps
6369
});
6470
6571
function _enter(node: Element) {
6672
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+
};
6977
}
7078
7179
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+
};
7984
}
8085
</script>
8186

0 commit comments

Comments
 (0)