Skip to content

Commit 25abca7

Browse files
authored
fix: ensure transitions properly cancel on completion (#9778)
1 parent ef158ff commit 25abca7

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

.changeset/lazy-masks-sit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure transitions properly cancel on completion

packages/svelte/src/internal/client/transitions.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,12 @@ class TickAnimation {
177177
}
178178

179179
cancel() {
180-
const t = this.#reversed ? 1 : 0;
181180
active_tick_animations.delete(this);
182-
this.#tick_fn(t, 1 - t);
181+
const current = this.#current / this.#duration;
182+
if (current > 0 && current < 1) {
183+
const t = this.#reversed ? 1 : 0;
184+
this.#tick_fn(t, 1 - t);
185+
}
183186
}
184187

185188
finish() {
@@ -322,7 +325,7 @@ function create_transition(dom, init, direction, effect) {
322325

323326
animation.onfinish = () => {
324327
const is_outro = curr_direction === 'out';
325-
/** @type {Animation | TickAnimation} */ (animation).pause();
328+
/** @type {Animation | TickAnimation} */ (animation).cancel();
326329
if (is_outro) {
327330
run_all(subs);
328331
subs = [];

packages/svelte/tests/animation-helpers.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,18 @@ class Animation {
102102
}
103103

104104
finish() {
105+
this.onfinish();
105106
this.currentTime = this.#reversed ? 0 : this.#duration;
106107
if (this.#reversed) {
107108
raf.animations.delete(this);
108109
}
109-
this.onfinish();
110110
}
111111

112112
cancel() {
113-
this._applyKeyFrame(this.#reversed ? this.#keyframes.length - 1 : 0);
114-
raf.animations.delete(this);
113+
this.#paused = true;
114+
if (this.currentTime > 0 && this.currentTime < this.#duration) {
115+
this._applyKeyFrame(this.#reversed ? this.#keyframes.length - 1 : 0);
116+
}
115117
}
116118

117119
pause() {

0 commit comments

Comments
 (0)