Fix Transition events

In Svelte the component updates/unmount happen in a different order than in React. The `mounted` guard doesn't work in Svelte and needs a different approach.
This commit is contained in:
Ryan Gossiaux
2021-12-18 15:41:18 -08:00
parent 2d6bd4658e
commit 2c8be5047d
2 changed files with 25 additions and 8 deletions

View File

@@ -60,14 +60,12 @@
export function useNesting(done?: () => void) {
let transitionableChildren: NestingContextValues["children"] = [];
let mounted = false;
onMount(() => (mounted = true));
onDestroy(() => (mounted = false));
function unregister(childId: ID, strategy = RenderStrategy.Hidden) {
let idx = transitionableChildren.findIndex(({ id }) => id === childId);
if (idx === -1) return;
let hadChildren = hasChildren(transitionableChildren);
match(strategy, {
[RenderStrategy.Unmount]() {
transitionableChildren.splice(idx, 1);
@@ -77,7 +75,7 @@
},
});
if (!hasChildren(transitionableChildren) && mounted) {
if (hadChildren && !hasChildren(transitionableChildren)) {
done?.();
}
}
@@ -182,7 +180,14 @@
</script>
{#if state === TreeStates.Visible}
<TransitionChild {...$$restProps} {unmount}>
<TransitionChild
{...$$restProps}
{unmount}
on:afterEnter
on:afterLeave
on:beforeEnter
on:beforeLeave
>
<slot />
</TransitionChild>
{/if}