Skip to content

Commit d190d72

Browse files
committed
fix: allow the "immediate" prop to stop an animation at its current value
Closes pmndrs#884
1 parent 68933e3 commit d190d72

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

packages/core/src/SpringValue.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ function describeImmediateProp() {
116116
it.todo('still resolves the "start" promise')
117117
it.todo('never calls the "onStart" prop')
118118
it.todo('never calls the "onRest" prop')
119+
120+
it('stops animating', async () => {
121+
const spring = new SpringValue(0)
122+
spring.start(2)
123+
await advanceUntilValue(spring, 1)
124+
125+
// Use "immediate" to emulate the "stop" method. (see #884)
126+
const value = spring.get()
127+
spring.start(value, { immediate: true })
128+
129+
// The "immediate" prop waits until the next frame before going idle.
130+
mockRaf.step()
131+
132+
expect(spring.idle).toBeTruthy()
133+
expect(spring.get()).toBe(value)
134+
})
119135
})
120136
}
121137

packages/core/src/SpringValue.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,13 +640,21 @@ export class SpringValue<T = any> extends FrameValue<T> {
640640
node.setValue((value = from as T))
641641
}
642642

643+
const immediate =
644+
// Sometimes the value is not animatable.
645+
!(toConfig || is.num(goal) || is.arr(goal)) ||
646+
!!matchProp(get('immediate'), key)
647+
648+
if (immediate !== anim.immediate) {
649+
anim.immediate = immediate
650+
if (!is.und(to)) {
651+
started = true
652+
}
653+
}
654+
643655
if (started) {
644656
anim.values = node.getPayload()
645657
anim.toValues = toConfig ? null : toArray(goal)
646-
anim.immediate =
647-
// Sometimes the value is not animatable.
648-
!(toConfig || is.num(goal) || is.arr(goal)) ||
649-
!!matchProp(get('immediate'), key)
650658
}
651659

652660
// Event props are replaced on every update.

0 commit comments

Comments
 (0)