File tree Expand file tree Collapse file tree 3 files changed +30
-4
lines changed Expand file tree Collapse file tree 3 files changed +30
-4
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' svelte ' : patch
3
+ ---
4
+
5
+ fix: Make Tween duration 0 set current to target immediately
Original file line number Diff line number Diff line change @@ -230,20 +230,25 @@ export class Tween {
230
230
set ( value , options ) {
231
231
set ( this . #target, value ) ;
232
232
233
- let previous_task = this . #task;
234
-
235
- let started = false ;
236
233
let {
237
234
delay = 0 ,
238
235
duration = 400 ,
239
236
easing = linear ,
240
237
interpolate = get_interpolator
241
238
} = { ...this . #defaults, ...options } ;
242
239
240
+ if ( duration === 0 ) {
241
+ this . #task?. abort ( ) ;
242
+ set ( this . #current, value ) ;
243
+ return Promise . resolve ( ) ;
244
+ }
245
+
243
246
const start = raf . now ( ) + delay ;
244
247
245
248
/** @type {(t: number) => T } */
246
249
let fn ;
250
+ let started = false ;
251
+ let previous_task = this . #task;
247
252
248
253
this . #task = loop ( ( now ) => {
249
254
if ( now < start ) {
Original file line number Diff line number Diff line change 2
2
import '../helpers.js' ; // for the matchMedia polyfill
3
3
import { describe , it , assert } from 'vitest' ;
4
4
import { get } from 'svelte/store' ;
5
- import { spring , tweened } from 'svelte/motion' ;
5
+ import { spring , tweened , Tween } from 'svelte/motion' ;
6
6
7
7
describe ( 'motion' , ( ) => {
8
8
describe ( 'spring' , ( ) => {
@@ -39,4 +39,20 @@ describe('motion', () => {
39
39
assert . equal ( get ( size ) , 20 ) ;
40
40
} ) ;
41
41
} ) ;
42
+
43
+ describe ( 'Tween' , ( ) => {
44
+ it ( 'sets immediately when duration is 0' , ( ) => {
45
+ const size = new Tween ( 0 ) ;
46
+
47
+ size . set ( 100 , { duration : 0 } ) ;
48
+ assert . equal ( size . current , 100 ) ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ it ( 'updates correctly when initialized with a `null`-ish value' , ( ) => {
53
+ const size = new Tween ( undefined as unknown as number , { duration : 0 } ) ;
54
+
55
+ size . set ( 10 ) ;
56
+ assert . equal ( size . current , 10 ) ;
57
+ } ) ;
42
58
} ) ;
You can’t perform that action at this time.
0 commit comments