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

@@ -10,11 +10,23 @@
</script> </script>
{#if !hasTransition && hasOpen} {#if !hasTransition && hasOpen}
<TransitionRoot {...$$props}> <TransitionRoot
{...$$props}
on:afterEnter
on:afterLeave
on:beforeEnter
on:beforeLeave
>
<slot /> <slot />
</TransitionRoot> </TransitionRoot>
{:else} {:else}
<TransitionChild {...$$props}> <TransitionChild
{...$$props}
on:afterEnter
on:afterLeave
on:beforeEnter
on:beforeLeave
>
<slot /> <slot />
</TransitionChild> </TransitionChild>
{/if} {/if}

View File

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