Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/onrest-cancelled-before-first-frame.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-spring/core': patch
---

Fire `onRest` when an animation is cancelled before its first frame. `SpringValue._stop` gated `onRest` on `anim.changed`, which only becomes `true` once a frame has rendered. When two `start()` calls happened within a single tick and the second returned the spring to its current value, the engine stopped the animation before any frame fired, so `onRest` was silently dropped. The same gap affected `set()` and `stop()` called synchronously after `start()`. Closes #1802.
15 changes: 15 additions & 0 deletions packages/core/src/SpringValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,21 @@ function describeEvents() {
global.mockRaf.step()
expect(onRest).toBeCalledTimes(1)
})
// https://github.com/pmndrs/react-spring/issues/1802
it('is called when two starts cancel each other before the first frame', async () => {
const onRest = vi.fn()
const spring = new SpringValue({ from: 0, onRest })

// Both starts happen synchronously, before any rAF tick.
// The second start returns to the current value, so the engine
// stops the animation before any frame has rendered.
spring.start(1)
spring.start(0)

await global.advanceUntilIdle()

expect(onRest).toBeCalled()
})
})
}

Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/SpringValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,10 +1026,8 @@ export class SpringValue<T = any> extends FrameValue<T> {
: getFinishedResult(this.get(), checkFinished(this, goal ?? anim.to))

flushCalls(this._pendingCalls, result)
if (anim.changed) {
anim.changed = false
sendEvent(this, 'onRest', result, this)
}
anim.changed = false
sendEvent(this, 'onRest', result, this)
}
}
}
Expand Down
Loading