Skip to content

fix: improve global transition handling of effect cleardown #10469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-spies-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve global transition handling of effect cleardown
12 changes: 9 additions & 3 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,9 @@ export function component(anchor_node, component_fn, render_fn) {
transition.f(() => {
transitions.delete(transition);
if (transitions.size === 0) {
if (render.e !== null) {
// If the current render has changed since, then we can remove the old render
// effect as it's stale.
if (current_render !== render && render.e !== null) {
if (render.d !== null) {
remove(render.d);
render.d = null;
Expand Down Expand Up @@ -1891,7 +1893,9 @@ function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) {
transition.f(() => {
transitions.delete(transition);
if (transitions.size === 0) {
if (render.e !== null) {
// If the current render has changed since, then we can remove the old render
// effect as it's stale.
if (current_render !== render && render.e !== null) {
if (render.d !== null) {
remove(render.d);
render.d = null;
Expand Down Expand Up @@ -2051,7 +2055,9 @@ export function key(anchor_node, key, render_fn) {
transition.f(() => {
transitions.delete(transition);
if (transitions.size === 0) {
if (render.e !== null) {
// If the current render has changed since, then we can remove the old render
// effect as it's stale.
if (current_render !== render && render.e !== null) {
if (render.d !== null) {
remove(render.d);
render.d = null;
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/tests/animation-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class Animation {

_update() {
if (this.#reversed) {
this.currentTime = this.#timeline_offset + (this.#timeline_offset - raf.time);
if (this.#timeline_offset === 0) {
this.currentTime = this.#duration - raf.time;
} else {
this.currentTime = this.#timeline_offset + (this.#timeline_offset - raf.time);
}
} else {
this.currentTime = raf.time - this.#timeline_offset;
}
Expand Down Expand Up @@ -130,6 +134,9 @@ class Animation {
}

reverse() {
if (this.#paused && !raf.animations.has(this)) {
raf.animations.add(this);
}
this.#timeline_offset = this.currentTime;
this.#reversed = !this.#reversed;
this.playState = 'running';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { fade } from 'svelte/transition';
let show = $state(true);
</script>

<h1>Outside</h1>

{#if show}
<button onclick={()=> show = false} transition:fade|global={{ duration: 100 }}>Hide</button>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test } from '../../test';
import { flushSync } from 'svelte';

export default test({
async test({ assert, target, raf }) {
const btn = target.querySelector('button');

raf.tick(0);

flushSync(() => {
btn?.click();
});

assert.htmlEqual(target.innerHTML, `<h1>Outside</h1><button>Hide</button>`);

raf.tick(100);

assert.htmlEqual(target.innerHTML, `<h1>Outside</h1>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Component from "./Component.svelte"
</script>

<svelte:component this={Component} />