Skip to content

Commit 53bf025

Browse files
committed
fix: Add animationCancel event for interrupted spring animations
1 parent c053920 commit 53bf025

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/framer-motion/src/value/__tests__/use-spring.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,34 @@ describe("useSpring animation events", () => {
278278

279279
await expect(promise).resolves.toBe(true)
280280
})
281+
282+
test("triggers animationCancel event when spring animation is interrupted", async () => {
283+
const promise = new Promise<boolean>((resolve) => {
284+
const Component = () => {
285+
const x = useMotionValue(0)
286+
const springX = useSpring(x, {
287+
stiffness: 100,
288+
damping: 10,
289+
})
290+
291+
useMotionValueEvent(springX, "animationCancel", () => {
292+
resolve(true)
293+
})
294+
295+
useEffect(() => {
296+
x.set(100)
297+
// Interrupt the animation by setting a new value
298+
setTimeout(() => {
299+
x.set(50)
300+
}, 10)
301+
}, [x])
302+
303+
return null
304+
}
305+
306+
render(<Component />)
307+
})
308+
309+
await expect(promise).resolves.toBe(true)
310+
})
281311
})

packages/motion-dom/src/value/spring-value.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function attachSpring<T extends AnyResolvedKeyframe>(
5050
if (activeAnimation) {
5151
activeAnimation.stop()
5252
activeAnimation = null
53+
value['events'].animationCancel?.notify()
5354
}
5455
}
5556
const startAnimation = () => {

0 commit comments

Comments
 (0)