Skip to content

Commit 25e7880

Browse files
committed
Fix bug in tweened() and re-add tests
1 parent fb4313e commit 25e7880

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

packages/svelte/src/motion/spring.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ function tick_spring(ctx, last_value, current_value, target_value) {
4848
}
4949

5050
/**
51-
* The spring function in Svelte creates a store whose value is animated, with a motion that simulates the behavior of a spring. This means when the value changes, instead of transitioning at a steady rate, it "bounces" like a spring would, depending on the physics parameters provided. This adds a level of realism to the transitions and can enhance the user experience.
51+
* The spring function in Svelte creates a store whose value is animated, with a motion that
52+
* simulates the behavior of a spring. This means when the value changes, instead of transitioning
53+
* at a steady rate, it "bounces" like a spring would, depending on the physics parameters provided.
54+
* This adds a level of realism to the transitions and can enhance the user experience.
5255
*
5356
* https://svelte.dev/docs/svelte-motion#spring
5457
* @template [T=any]

packages/svelte/src/motion/tweened.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ function get_interpolator(a, b) {
7171
}
7272

7373
/**
74-
* A tweened store in Svelte is a special type of store that provides smooth transitions between state values over time.
74+
* A tweened store in Svelte is a special type of store that provides smooth transitions between
75+
* state values over time.
7576
*
7677
* https://svelte.dev/docs/svelte-motion#tweened
7778
* @template T
@@ -89,11 +90,12 @@ export function tweened(value, defaults = {}) {
8990
* @param {import('./private').TweenedOptions<T>} [opts]
9091
*/
9192
function set(new_value, opts) {
93+
target_value = new_value;
94+
9295
if (value == null) {
9396
store.set((value = new_value));
9497
return Promise.resolve();
9598
}
96-
target_value = new_value;
9799

98100
/** @type {import('../internal/client/private').Task | null} */
99101
let previous_task = task;
@@ -123,8 +125,9 @@ export function tweened(value, defaults = {}) {
123125
if (now < start) return true;
124126
if (!started) {
125127
fn = interpolate(/** @type {any} */ (value), new_value);
126-
if (typeof duration === 'function')
128+
if (typeof duration === 'function') {
127129
duration = duration(/** @type {any} */ (value), new_value);
130+
}
128131
started = true;
129132
}
130133
if (previous_task) {

packages/svelte/tests/motion/test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, it, assert } from 'vitest';
2+
import { get } from 'svelte/store';
3+
import { spring, tweened } from 'svelte/motion';
4+
5+
describe('spring', () => {
6+
it('handles initially `undefined` values', () => {
7+
const size = spring();
8+
9+
size.set(100);
10+
assert.equal(get(size), 100);
11+
});
12+
});
13+
14+
describe('tweened', () => {
15+
it('handles initially `undefined` values', () => {
16+
const size = tweened();
17+
18+
size.set(100);
19+
assert.equal(get(size), 100);
20+
});
21+
22+
it('sets immediately when duration is 0', () => {
23+
const size = tweened(0);
24+
25+
size.set(100, { duration: 0 });
26+
assert.equal(get(size), 100);
27+
});
28+
29+
it('updates correctly when initialized with a `null`-ish value', () => {
30+
const size = tweened(undefined as unknown as number, { duration: 0 });
31+
32+
size.set(10);
33+
assert.equal(get(size), 10);
34+
35+
size.update((v) => v + 10);
36+
assert.equal(get(size), 20);
37+
});
38+
});

0 commit comments

Comments
 (0)