Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transitionEnd should not be applied when new animations caused the animation to stop #2265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function animateTarget(
if (transitionOverride) transition = transitionOverride

const animations: AnimationPlaybackControls[] = []
const promises: (Promise<void>)[] = []

const animationTypeState =
type &&
Expand Down Expand Up @@ -101,10 +102,19 @@ export function animateTarget(
}

animations.push(animation)
promises.push(new Promise((resolve) => {
const subscriptions: (() => void)[] = []
const unsubscribe = () => subscriptions.forEach((fn) => fn())
subscriptions.push(value.on('animationComplete', () => {
unsubscribe()
resolve()
}))
subscriptions.push(value.on('animationCancel', unsubscribe))
}))
}

if (transitionEnd) {
Promise.all(animations).then(() => {
Promise.all(promises).then(() => {
transitionEnd && setTarget(visualElement, transitionEnd)
})
}
Expand Down
27 changes: 27 additions & 0 deletions packages/framer-motion/src/motion/__tests__/animate-prop.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,33 @@ describe("animate prop as object", () => {
})
return expect(promise).resolves.toBe(300)
})
test("uses the latest styles on subsequent renders", async () => {
const promise = new Promise((resolve) => {
const x = motionValue(0)
const Component = ({ animate }: any) => (
<motion.div animate={animate} style={{ x }} />
)
const { rerender } = render(
<Component
animate={{
x: 10,
transition: { type: false },
transitionEnd: { x: 100 },
}}
/>
)
rerender(
<Component
animate={{
x: 20,
transition: { type: false },
}}
/>
)
requestAnimationFrame(() => resolve(x.get()))
})
return expect(promise).resolves.toBe(20)
})
test("animates to set prop and preserves existing initial transform props", async () => {
const promise = new Promise((resolve) => {
const onComplete = () => {
Expand Down