Skip to content

Commit 209dbf0

Browse files
committed
feat: allow non-numeric values to be tweened by snapping immediately to new value
1 parent 4aadb34 commit 209dbf0

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.changeset/cool-apples-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': minor
3+
---
4+
5+
feat: allow non-numeric values to be tweened by snapping immediately to new value

packages/svelte/src/motion/tweened.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ function get_interpolator(a, b) {
7272
return (t) => a + t * delta;
7373
}
7474

75-
throw new Error(`Cannot interpolate ${type} values`);
75+
// for non-numeric values, snap to the final value immediately
76+
return () => b;
7677
}
7778

7879
/**

packages/svelte/tests/motion/test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import '../helpers.js'; // for the matchMedia polyfill
33
import { describe, it, assert } from 'vitest';
44
import { get } from 'svelte/store';
55
import { spring, tweened, Tween } from 'svelte/motion';
6+
import { raf } from '../animation-helpers.js';
67

78
describe('motion', () => {
89
describe('spring', () => {
@@ -38,6 +39,16 @@ describe('motion', () => {
3839
size.update((v) => v + 10);
3940
assert.equal(get(size), 20);
4041
});
42+
43+
it('updates non-numeric values immediately', () => {
44+
raf.reset();
45+
const boolean = tweened(false);
46+
47+
boolean.set(true, { duration: 100 });
48+
49+
raf.tick(1);
50+
assert.equal(get(boolean), true);
51+
});
4152
});
4253

4354
describe('Tween', () => {
@@ -47,6 +58,16 @@ describe('motion', () => {
4758
size.set(100, { duration: 0 });
4859
assert.equal(size.current, 100);
4960
});
61+
62+
it('updates non-numeric values immediately', () => {
63+
raf.reset();
64+
const boolean = new Tween(false);
65+
66+
boolean.set(true, { duration: 100 });
67+
68+
raf.tick(1);
69+
assert.equal(boolean.current, true);
70+
});
5071
});
5172

5273
it('updates correctly when initialized with a `null`-ish value', () => {

0 commit comments

Comments
 (0)