@@ -104,6 +104,8 @@ export class SpringValue<T = any> extends FrameValue<T> {
104
104
/** The last `scheduleProps` call that changed the `to` prop */
105
105
protected _lastToId = 0
106
106
107
+ protected _memoizedDuration = 0
108
+
107
109
constructor ( from : Exclude < T , object > , props ?: SpringUpdate < T > )
108
110
constructor ( props ?: SpringUpdate < T > )
109
111
constructor ( arg1 ?: any , arg2 ?: any ) {
@@ -191,7 +193,7 @@ export class SpringValue<T = any> extends FrameValue<T> {
191
193
return
192
194
}
193
195
194
- const elapsed = ( node . elapsedTime += dt )
196
+ let elapsed = ( node . elapsedTime += dt )
195
197
const from = anim . fromValues [ i ]
196
198
197
199
const v0 =
@@ -207,8 +209,31 @@ export class SpringValue<T = any> extends FrameValue<T> {
207
209
if ( ! is . und ( config . duration ) ) {
208
210
let p = 1
209
211
if ( config . duration > 0 ) {
210
- p = ( config . progress || 0 ) + elapsed / config . duration
212
+ /**
213
+ * Here we check if the duration has changed in the config
214
+ * and if so update the elapsed time to the percentage
215
+ * of completition so there is no jank in the animation
216
+ * https://github.com/pmndrs/react-spring/issues/1163
217
+ */
218
+ if ( this . _memoizedDuration !== config . duration ) {
219
+ // update the memoized version to the new duration
220
+ this . _memoizedDuration = config . duration
221
+
222
+ // if the value has started animating we need to update it
223
+ if ( node . durationProgress > 0 ) {
224
+ // set elapsed time to be the same percentage of progress as the previous duration
225
+ node . elapsedTime = config . duration * node . durationProgress
226
+ // add the delta so the below updates work as expected
227
+ elapsed = node . elapsedTime += dt
228
+ }
229
+ }
230
+
231
+ // calculate the new progress
232
+ p = ( config . progress || 0 ) + elapsed / this . _memoizedDuration
233
+ // p is clamped between 0-1
211
234
p = p > 1 ? 1 : p < 0 ? 0 : p
235
+ // store our new progress
236
+ node . durationProgress = p
212
237
}
213
238
214
239
position = from + config . easing ( p ) * ( to - from )
0 commit comments