Skip to content

Commit 0a0584b

Browse files
committed
Keep values when an element is shown again only to exit
When a Suspense reveal removes an AnimatePresence child, the child mounts again with isPresent: false. VisualElement.mount() reset its motion values to `initial` before the exit started, so with the common pattern of `initial` and `exit` sharing a value (e.g. opacity: 0) the child vanished in one frame instead of animating out from what it showed before the boundary suspended. An element that mounts again already exiting is only shown to animate out, so it now keeps its values, as keepValuesOnRemount already does for AnimateSuspense. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016xzo3bJroXe9dmJ5nuBJQm
1 parent b8944fd commit 0a0584b

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎packages/framer-motion/src/components/AnimatePresence/__tests__/mount-while-exiting.test.tsx‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function waitForRemoval(query: () => Element | null) {
2020
* detaches the hidden tree's refs while it's suspended and attaches them
2121
* again on reveal, so the exiting child mounts again already exiting.
2222
*/
23-
function renderItems(isStrict: boolean) {
23+
function renderItems(isStrict: boolean, duration = 0.05) {
2424
const cache = new Map<string, Item>([
2525
["A", { name: "A", hasAction: true }],
2626
["B", { name: "B", hasAction: false }],
@@ -55,7 +55,7 @@ function renderItems(isStrict: boolean) {
5555
initial={{ opacity: 0 }}
5656
animate={{ opacity: 1 }}
5757
exit={{ opacity: 0 }}
58-
transition={{ duration: 0.05 }}
58+
transition={{ duration }}
5959
onAnimationStart={onAnimationStart}
6060
>
6161
Action for {item.name}
@@ -127,6 +127,34 @@ describe.each([true, false])(
127127
expect(action()).toBeNull()
128128
})
129129

130+
test("exits from the values it showed before the boundary suspended", async () => {
131+
const { action, select, resolve } = renderItems(isStrict, 1)
132+
133+
// Wait for the enter animation to finish
134+
for (let i = 0; i < 80 && action()?.style.opacity !== "1"; i++) {
135+
await act(nextFrame)
136+
}
137+
expect(action()!.style.opacity).toBe("1")
138+
139+
await select("C")
140+
await resolve()
141+
142+
/**
143+
* The action is only shown again to animate out, so it mustn't
144+
* be reset to initial={{ opacity: 0 }} first, which would hide
145+
* it without a visible exit.
146+
*/
147+
let maxOpacity = 0
148+
for (let i = 0; i < 120 && action(); i++) {
149+
await act(nextFrame)
150+
const opacity = action()?.style.opacity
151+
if (opacity) maxOpacity = Math.max(maxOpacity, +opacity)
152+
}
153+
154+
expect(maxOpacity).toBeGreaterThan(0.5)
155+
expect(action()).toBeNull()
156+
})
157+
130158
test("a motion component first rendered by an exiting child exits", async () => {
131159
const onAnimationStart = jest.fn()
132160

‎packages/motion-dom/src/render/VisualElement.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,15 @@ export abstract class VisualElement<
451451
* If this element has been mounted before (e.g. after a Suspense
452452
* unmount/remount), reset motion values to their initial state
453453
* so animations replay correctly from initial → animate, unless
454-
* the presence asks to keep them.
454+
* the presence asks to keep them or the element is only shown
455+
* again to exit (#4049).
455456
*/
456-
if (this.hasBeenMounted && !this.presenceContext?.keepValuesOnRemount) {
457+
const presence = this.presenceContext
458+
if (
459+
this.hasBeenMounted &&
460+
presence?.isPresent !== false &&
461+
!presence?.keepValuesOnRemount
462+
) {
457463
for (const key in this.initialValues) {
458464
this.values.get(key)?.jump(this.initialValues[key])
459465
this.latestValues[key] = this.initialValues[key]

0 commit comments

Comments
 (0)