fix: make Tween with duration 0 set current to target immediately (#14937)

* fix: Make Tween duration 0 set current to target immediately

* Run prettier on test file

* tweak

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/14938/head
Matthijs 4 days ago committed by GitHub
parent a1f371e786
commit c8865bb4a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: Make Tween duration 0 set current to target immediately

@ -230,9 +230,6 @@ export class Tween {
set(value, options) { set(value, options) {
set(this.#target, value); set(this.#target, value);
let previous_task = this.#task;
let started = false;
let { let {
delay = 0, delay = 0,
duration = 400, duration = 400,
@ -240,10 +237,18 @@ export class Tween {
interpolate = get_interpolator interpolate = get_interpolator
} = { ...this.#defaults, ...options }; } = { ...this.#defaults, ...options };
if (duration === 0) {
this.#task?.abort();
set(this.#current, value);
return Promise.resolve();
}
const start = raf.now() + delay; const start = raf.now() + delay;
/** @type {(t: number) => T} */ /** @type {(t: number) => T} */
let fn; let fn;
let started = false;
let previous_task = this.#task;
this.#task = loop((now) => { this.#task = loop((now) => {
if (now < start) { if (now < start) {

@ -2,7 +2,7 @@
import '../helpers.js'; // for the matchMedia polyfill import '../helpers.js'; // for the matchMedia polyfill
import { describe, it, assert } from 'vitest'; import { describe, it, assert } from 'vitest';
import { get } from 'svelte/store'; import { get } from 'svelte/store';
import { spring, tweened } from 'svelte/motion'; import { spring, tweened, Tween } from 'svelte/motion';
describe('motion', () => { describe('motion', () => {
describe('spring', () => { describe('spring', () => {
@ -39,4 +39,20 @@ describe('motion', () => {
assert.equal(get(size), 20); assert.equal(get(size), 20);
}); });
}); });
describe('Tween', () => {
it('sets immediately when duration is 0', () => {
const size = new Tween(0);
size.set(100, { duration: 0 });
assert.equal(size.current, 100);
});
});
it('updates correctly when initialized with a `null`-ish value', () => {
const size = new Tween(undefined as unknown as number, { duration: 0 });
size.set(10);
assert.equal(size.current, 10);
});
}); });

Loading…
Cancel
Save