Skip to content

Commit e835b8f

Browse files
authored
Merge pull request #4045 from motiondivision/claude/project-thread-cb06bn
Finish transforms that can't be accelerated when they end before the group builds
2 parents cc43fef + 08590ff commit e835b8f

4 files changed

Lines changed: 139 additions & 10 deletions

File tree

‎packages/framer-motion/src/motion/__tests__/waapi.test.tsx‎

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { waitFor } from "@testing-library/dom"
12
import { supportsFlags } from "motion-dom"
2-
import { act, createRef, useState } from "react"
3+
import { act, createRef, useEffect, useRef, useState } from "react"
34
import { motion, useMotionValue } from "../../"
45
import "../../animation/animators/waapi/__tests__/setup"
56
import { nextFrame } from "../../gestures/__tests__/utils"
@@ -979,4 +980,79 @@ describe("WAAPI animations", () => {
979980
)
980981
supportsFlags.linearEasing = undefined
981982
})
983+
984+
test("Finishes a transform that can't be accelerated when the main thread is blocked past its end", async () => {
985+
supportsFlags.linearEasing = true
986+
const ref = createRef<HTMLDivElement>()
987+
const completed: string[] = []
988+
let setVisible: (isVisible: boolean) => void
989+
990+
const variants = {
991+
visible: {
992+
visibility: "visible",
993+
opacity: 1,
994+
scale: 1,
995+
transition: { duration: 0.1 },
996+
},
997+
hidden: {
998+
opacity: 0,
999+
scale: 0.5,
1000+
transition: { duration: 0.1 },
1001+
transitionEnd: { visibility: "hidden" },
1002+
},
1003+
}
1004+
1005+
const Component = () => {
1006+
const [isVisible, set] = useState(false)
1007+
const isMounted = useRef(false)
1008+
setVisible = set
1009+
1010+
useEffect(() => {
1011+
/**
1012+
* Block the main thread for longer than the transition,
1013+
* before the animation's first frame.
1014+
*/
1015+
if (isMounted.current && !isVisible) {
1016+
const start = performance.now()
1017+
while (performance.now() - start < 300) {}
1018+
}
1019+
isMounted.current = true
1020+
}, [isVisible])
1021+
1022+
return (
1023+
<motion.div
1024+
ref={ref}
1025+
initial={false}
1026+
variants={variants}
1027+
animate={isVisible ? "visible" : "hidden"}
1028+
onUpdate={() => {}}
1029+
onAnimationComplete={(definition) =>
1030+
completed.push(definition as string)
1031+
}
1032+
/>
1033+
)
1034+
}
1035+
1036+
render(<Component />)
1037+
try {
1038+
act(() => setVisible(true))
1039+
await waitFor(() => expect(completed).toEqual(["visible"]))
1040+
1041+
act(() => setVisible(false))
1042+
await waitFor(() =>
1043+
expect(completed).toEqual(["visible", "hidden"])
1044+
)
1045+
expect(ref.current!.style.visibility).toBe("hidden")
1046+
expect(ref.current!.style.transform).toBe("scale(0.5)")
1047+
1048+
act(() => setVisible(true))
1049+
await waitFor(() =>
1050+
expect(completed).toEqual(["visible", "hidden", "visible"])
1051+
)
1052+
expect(ref.current!.style.visibility).toBe("visible")
1053+
expect(ref.current!.style.opacity).toBe("1")
1054+
} finally {
1055+
supportsFlags.linearEasing = undefined
1056+
}
1057+
})
9821058
})

‎packages/motion-dom/src/animation/waapi/transforms/TransformAnimation.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,15 @@ export class TransformAnimation<
450450
}
451451

452452
/**
453-
* JSAnimation finishes on its next tick, which only happens on the
454-
* main thread, so finish now.
453+
* JSAnimation finishes on its next tick, which only happens once the
454+
* group is on the main thread, so finish now. That includes an
455+
* animation that can't be accelerated but reached its end before the
456+
* group could move there.
455457
*/
456458
complete() {
457459
super.complete()
458460
const { group } = this
459-
group && !group.isJS && this.canAccelerate && this.tick(time.now())
461+
group && !group.isJS && this.tick(time.now())
460462
}
461463

462464
/**

‎packages/motion-dom/src/animation/waapi/transforms/__tests__/TransformAnimation.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,49 @@ describe("independent transform acceleration", () => {
370370
expect(element.style.transform).toContain("translateY")
371371
})
372372

373+
it("finishes a value that can't be accelerated if it ends before the group builds", async () => {
374+
const element = document.createElement("div")
375+
const onComplete = jest.fn()
376+
377+
const [animation] = animateElement(
378+
element,
379+
{ scale: [1, 0.5] },
380+
{ duration: 0.1, onUpdate: () => {}, onComplete }
381+
)
382+
383+
/**
384+
* The main thread is busy past the end, before the group builds,
385+
* as when the same click starts a long render.
386+
*/
387+
const start = performance.now()
388+
while (performance.now() - start < 150) {}
389+
390+
const finished = await Promise.race([
391+
animation.finished.then(() => true),
392+
wait(300).then(() => false),
393+
])
394+
395+
expect(finished).toBe(true)
396+
expect(onComplete).toHaveBeenCalledTimes(1)
397+
expect(element.style.transform).toBe("scale(0.5)")
398+
})
399+
400+
it("finishes a value that can't be accelerated when completed before the group builds", async () => {
401+
const element = document.createElement("div")
402+
const onComplete = jest.fn()
403+
404+
const [animation] = animateElement(
405+
element,
406+
{ scale: [1, 0.5] },
407+
{ duration: 1, onUpdate: () => {}, onComplete }
408+
)
409+
animation.complete()
410+
await nextFrame()
411+
412+
expect(onComplete).toHaveBeenCalledTimes(1)
413+
expect(element.style.transform).toBe("scale(0.5)")
414+
})
415+
373416
it("stays on the main thread for values a mixer mixes", async () => {
374417
const element = document.createElement("div")
375418
let mixed = 0

‎packages/motion/src/__tests__/remix.test.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,14 @@ describe("motion/remix", () => {
205205
})
206206

207207
it("animates variants through children and calls onAnimationComplete", async () => {
208-
const onAnimationComplete = jest.fn()
209-
const item = { hidden: { opacity: 0 }, visible: { opacity: 1 } }
208+
let onAnimationComplete!: (definition: unknown) => void
209+
const animationComplete = new Promise(
210+
(resolve) => (onAnimationComplete = resolve)
211+
)
212+
const item = {
213+
hidden: { opacity: 0 },
214+
visible: { opacity: 1, transition: { duration: 0.05 } },
215+
}
210216
const { container } = setup(
211217
() => () =>
212218
createElement(
@@ -215,9 +221,8 @@ describe("motion/remix", () => {
215221
initial: "hidden",
216222
animate: "visible",
217223
variants: {
218-
visible: { transition: { staggerChildren: 0.1 } },
224+
visible: { transition: { staggerChildren: 0.3 } },
219225
},
220-
transition: { duration: 0.05 },
221226
onAnimationComplete,
222227
},
223228
createElement(motion.li, { variants: item }),
@@ -229,10 +234,13 @@ describe("motion/remix", () => {
229234
// The second item is still waiting for its stagger delay.
230235
expect((items[1] as HTMLElement).style.opacity).toBe("0")
231236

232-
await wait(400)
237+
/**
238+
* Wait for completion rather than a fixed timer, which races the
239+
* second item's stagger delay plus transition under load.
240+
*/
241+
expect(await animationComplete).toBe("visible")
233242
expect((items[0] as HTMLElement).style.opacity).toBe("1")
234243
expect((items[1] as HTMLElement).style.opacity).toBe("1")
235-
expect(onAnimationComplete.mock.calls[0][0]).toBe("visible")
236244
})
237245

238246
it("renders motion values in style", async () => {

0 commit comments

Comments
 (0)