Skip to content

Commit 405ebe2

Browse files
authored
Merge pull request #4043 from motiondivision/claude/project-thread-8iynk0
Fix exit stuck after an open layoutId card switches to another item
2 parents e835b8f + 1534206 commit 405ebe2

3 files changed

Lines changed: 130 additions & 4 deletions

File tree

‎dev/react/src/tests/issue-4042.tsx‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { AnimatePresence, motion } from "framer-motion"
2+
import { useState } from "react"
3+
4+
/**
5+
* Issue #4042: gallery items each have layoutId={color} with a nested
6+
* layoutId={`dot-${color}`} child. The open card shares both layoutIds with
7+
* the open item. Opening red, then clicking blue while the card is open
8+
* (switching the card's layoutIds to blue's), then closing, left the card on
9+
* screen forever: its exit never completed.
10+
*/
11+
const colors = ["red", "blue"]
12+
const transition = { duration: 1 }
13+
14+
export const App = () => {
15+
const [open, setOpen] = useState<string | null>(null)
16+
17+
return (
18+
<div style={{ position: "relative", width: 600, height: 500 }}>
19+
<div style={{ display: "flex", gap: 20 }}>
20+
{colors.map((color) => (
21+
<motion.div
22+
key={color}
23+
id={color}
24+
layoutId={color}
25+
onClick={() => setOpen(color)}
26+
transition={transition}
27+
style={{ width: 100, height: 100, background: color }}
28+
>
29+
<motion.div
30+
id={`dot-${color}`}
31+
layoutId={`dot-${color}`}
32+
transition={transition}
33+
style={{
34+
width: 20,
35+
height: 20,
36+
background: "#fff",
37+
}}
38+
/>
39+
</motion.div>
40+
))}
41+
</div>
42+
<button id="close" onClick={() => setOpen(null)}>
43+
Close
44+
</button>
45+
<AnimatePresence>
46+
{open && (
47+
<motion.div
48+
key="card"
49+
id="card"
50+
layoutId={open}
51+
transition={transition}
52+
style={{
53+
position: "absolute",
54+
top: 200,
55+
left: 200,
56+
width: 300,
57+
height: 200,
58+
background: open,
59+
}}
60+
>
61+
<motion.div
62+
layoutId={`dot-${open}`}
63+
transition={transition}
64+
style={{
65+
width: 60,
66+
height: 60,
67+
background: "#fff",
68+
}}
69+
/>
70+
</motion.div>
71+
)}
72+
</AnimatePresence>
73+
</div>
74+
)
75+
}

‎packages/motion-dom/src/projection/node/create-projection-node.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,13 +2364,16 @@ function notifyLayoutUpdate(node: IProjectionNode) {
23642364
const relativeParent = node.getClosestProjectingParent()
23652365

23662366
/**
2367-
* A snapshot resumed from another element is only relative to this
2368-
* node's relativeParent if it's a layoutRoot that contains both.
2367+
* A snapshot from another element is only relative to this node's
2368+
* relativeParent if it's a layoutRoot that contains both. A node can
2369+
* hold one without resumeFrom, when the element it came from changed
2370+
* layoutId but stayed mounted (see switchLayoutId), or a remix
2371+
* layout exit cleared it (see markLayoutExit).
23692372
*/
23702373
if (
2371-
!node.resumeFrom ||
2374+
!isShared ||
23722375
(relativeParent?.options.layoutRoot &&
2373-
node.resumeFrom.path.includes(relativeParent))
2376+
node.resumeFrom?.path.includes(relativeParent))
23742377
) {
23752378
/**
23762379
* If the relativeParent is itself resuming from a different element then

‎tests/react/issue-4042.spec.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* https://github.com/motiondivision/motion/issues/4042
3+
*
4+
* An open card shares a layoutId with a gallery item, and its nested child
5+
* shares a layoutId with the item's nested child. Switching the open card to
6+
* another item's layoutIds, then closing it, must still let AnimatePresence
7+
* remove the card once its 1s exit is done.
8+
*/
9+
import { expect, Page, test } from "@playwright/test"
10+
11+
const box = (page: Page, selector: string) =>
12+
page.locator(selector).evaluate((element) => {
13+
const { x, y, width, height } = element.getBoundingClientRect()
14+
return { x, y, width, height }
15+
})
16+
17+
for (const switchItem of [true, false]) {
18+
test(`card exits ${
19+
switchItem ? "after switching to another item" : "without switching"
20+
}`, async ({ page }) => {
21+
const errors: string[] = []
22+
page.on("pageerror", (error) => errors.push(error.message))
23+
24+
await page.goto("?test=issue-4042")
25+
await expect(page.locator("#red")).toBeVisible()
26+
const redDot = await box(page, "#dot-red")
27+
28+
await page.click("#red")
29+
await page.waitForTimeout(1500)
30+
31+
if (switchItem) {
32+
await page.click("#blue", { force: true })
33+
34+
// The red item's dot animates home from the card's dot
35+
await page.waitForTimeout(300)
36+
const midway = await box(page, "#dot-red")
37+
expect(midway.x).toBeGreaterThan(redDot.x + 20)
38+
expect(midway.y).toBeGreaterThan(redDot.y + 20)
39+
40+
await page.waitForTimeout(1200)
41+
expect(await box(page, "#dot-red")).toEqual(redDot)
42+
}
43+
44+
await page.click("#close", { force: true })
45+
await expect(page.locator("#card")).toHaveCount(0, { timeout: 2500 })
46+
expect(errors).toEqual([])
47+
})
48+
}

0 commit comments

Comments
 (0)