Skip to content

Commit 04817a2

Browse files
authored
Adding complete (#2026)
1 parent 32312b5 commit 04817a2

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

packages/framer-motion/src/animation/GroupPlaybackControls.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ export class GroupPlaybackControls implements AnimationPlaybackControls {
5454
cancel() {
5555
this.runAll("cancel")
5656
}
57+
58+
complete() {
59+
this.runAll("complete")
60+
}
5761
}

packages/framer-motion/src/animation/__tests__/GroupPlaybackControls.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function createTestAnimationControls(
1212
then: (resolve: VoidFunction) => {
1313
return Promise.resolve().then(resolve)
1414
},
15+
complete: () => {},
1516
cancel: () => {},
1617
...partialControls,
1718
}
@@ -147,4 +148,20 @@ describe("GroupPlaybackControls", () => {
147148
expect(a.cancel).toBeCalled()
148149
expect(b.cancel).toBeCalled()
149150
})
151+
152+
test("Calls complete on all animations", async () => {
153+
const a = createTestAnimationControls({
154+
complete: jest.fn(),
155+
})
156+
const b = createTestAnimationControls({
157+
complete: jest.fn(),
158+
})
159+
160+
const controls = new GroupPlaybackControls([a, b])
161+
162+
controls.complete()
163+
164+
expect(a.complete).toBeCalled()
165+
expect(b.complete).toBeCalled()
166+
})
150167
})

packages/framer-motion/src/animation/animators/instant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function createInstantAnimation<V>({
2323
return Promise.resolve()
2424
},
2525
cancel: noop<void>,
26+
complete: noop<void>,
2627
}
2728
}
2829

packages/framer-motion/src/animation/animators/js/__tests__/animate.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,4 +1091,25 @@ describe("animate", () => {
10911091

10921092
expect(output).toEqual([0, 20, 40, 0])
10931093
})
1094+
1095+
test("Correctly completes an animation", async () => {
1096+
const output: number[] = []
1097+
1098+
const animation = animateValue({
1099+
keyframes: [0, 100],
1100+
driver: syncDriver(20),
1101+
duration: 100,
1102+
ease: linear,
1103+
onUpdate: (v) => {
1104+
output.push(v)
1105+
if (v === 40) {
1106+
animation.complete()
1107+
}
1108+
},
1109+
})
1110+
1111+
await animation
1112+
1113+
expect(output).toEqual([0, 20, 40, 100])
1114+
})
10941115
})

packages/framer-motion/src/animation/animators/js/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,6 @@ export function animateValue<V = number>({
219219
const state = frameGenerator.next(elapsed)
220220
let { value, done } = state
221221

222-
if (onUpdate) {
223-
onUpdate(
224-
mapNumbersToKeyframes ? mapNumbersToKeyframes(value) : value
225-
)
226-
}
227-
228222
if (calculatedDuration !== null) {
229223
done = time >= totalDuration
230224
}
@@ -233,6 +227,12 @@ export function animateValue<V = number>({
233227
holdTime === null &&
234228
(playState === "finished" || (playState === "running" && done))
235229

230+
if (onUpdate) {
231+
onUpdate(
232+
mapNumbersToKeyframes ? mapNumbersToKeyframes(value) : value
233+
)
234+
}
235+
236236
if (isAnimationFinished) {
237237
finish()
238238
}
@@ -321,6 +321,10 @@ export function animateValue<V = number>({
321321
if (cancelTime !== null) tick(cancelTime)
322322
cancel()
323323
},
324+
complete: () => {
325+
playState = "finished"
326+
holdTime === null
327+
},
324328
sample: (elapsed: number) => {
325329
startTime = 0
326330
return tick(elapsed)!

packages/framer-motion/src/animation/animators/waapi/create-accelerated-animation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export function createAcceleratedAnimation(
191191
}
192192
safeCancel()
193193
},
194+
complete: () => animation.finish(),
194195
cancel: safeCancel,
195196
}
196197
}

packages/framer-motion/src/animation/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface AnimationPlaybackControls {
3434
stop: () => void
3535
play: () => void
3636
pause: () => void
37+
complete: () => void
3738
cancel: () => void
3839
then: (onResolve: VoidFunction, onReject?: VoidFunction) => Promise<void>
3940
}

0 commit comments

Comments
 (0)