Fix transitioning away
Fixes #17 The root problem here, which took me ages to find, was that `state` should not be reactive. It should only be set once the child has finished, via the nesting context.
This commit is contained in:
@@ -140,7 +140,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let _cleanup = null;
|
let _cleanup = null;
|
||||||
$: if (mounted) {
|
$: {
|
||||||
|
if (mounted) {
|
||||||
if (_cleanup) {
|
if (_cleanup) {
|
||||||
_cleanup();
|
_cleanup();
|
||||||
}
|
}
|
||||||
@@ -150,6 +151,7 @@
|
|||||||
);
|
);
|
||||||
initial = false;
|
initial = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setContext(NESTING_CONTEXT_NAME, nesting);
|
setContext(NESTING_CONTEXT_NAME, nesting);
|
||||||
let openClosedState: Writable<State> | undefined = writable(State.Closed);
|
let openClosedState: Writable<State> | undefined = writable(State.Closed);
|
||||||
@@ -164,5 +166,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div bind:this={container} {...$$restProps}>
|
<div bind:this={container} {...$$restProps}>
|
||||||
|
{#if state === TreeStates.Visible}
|
||||||
<slot />
|
<slot />
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -120,23 +120,36 @@
|
|||||||
|
|
||||||
let openClosedState: Writable<State> | undefined = getContext("OpenClosed");
|
let openClosedState: Writable<State> | undefined = getContext("OpenClosed");
|
||||||
|
|
||||||
$: shouldShow = (() => {
|
function computeShow(
|
||||||
|
show: boolean,
|
||||||
|
openClosedState: State | undefined
|
||||||
|
): boolean {
|
||||||
if (show === null && openClosedState !== undefined) {
|
if (show === null && openClosedState !== undefined) {
|
||||||
return match($openClosedState, {
|
return match(openClosedState, {
|
||||||
[State.Open]: true,
|
[State.Open]: true,
|
||||||
[State.Closed]: false,
|
[State.Closed]: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return show;
|
return show;
|
||||||
})();
|
}
|
||||||
|
|
||||||
$: if (shouldShow !== true && shouldShow !== false) {
|
let shouldShow = computeShow(
|
||||||
|
show,
|
||||||
|
openClosedState !== undefined ? $openClosedState : undefined
|
||||||
|
);
|
||||||
|
$: {
|
||||||
|
shouldShow = computeShow(
|
||||||
|
show,
|
||||||
|
openClosedState !== undefined ? $openClosedState : undefined
|
||||||
|
);
|
||||||
|
if (shouldShow !== true && shouldShow !== false) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"A <Transition /> is used but it is missing a `show={true | false}` prop."
|
"A <Transition /> is used but it is missing a `show={true | false}` prop."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$: state = shouldShow ? TreeStates.Visible : TreeStates.Hidden;
|
}
|
||||||
|
let state = shouldShow ? TreeStates.Visible : TreeStates.Hidden;
|
||||||
|
|
||||||
let nestingBag: Writable<NestingContextValues> = writable();
|
let nestingBag: Writable<NestingContextValues> = writable();
|
||||||
nestingBag.set(
|
nestingBag.set(
|
||||||
|
|||||||
Reference in New Issue
Block a user