Skip to content

Commit 93d6f00

Browse files
Merge remote-tracking branch 'origin/main' into cursor/animate-presence-wait-transition-c98a
# Conflicts: # packages/framer-motion/cypress/integration/start-transition-presence-wait.ts Co-authored-by: Matt Perry <[email protected]>
2 parents cfae3c1 + ab5d93e commit 93d6f00

11 files changed

Lines changed: 1394 additions & 0 deletions
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { LayoutGroup, motion } from "framer-motion"
2+
import { memo, useDeferredValue, useState, useTransition } from "react"
3+
import {
4+
SlowList as SlowListBase,
5+
click,
6+
duration,
7+
linear,
8+
mode,
9+
params,
10+
runScript,
11+
update,
12+
useCommitLog,
13+
} from "./start-transition-helpers"
14+
15+
/**
16+
* Shared layout (layoutId) and LayoutGroup with transition-wrapped updates.
17+
*
18+
* ?mode=sync|transition|useTransition|deferred
19+
* ?scenario=basic|rapid|midAnimation|group
20+
*/
21+
22+
const SlowList = memo(SlowListBase)
23+
const layoutTransition = { layout: { ...linear, duration } }
24+
const tabWidth = 200
25+
26+
const scenarios: Record<string, Array<[number, () => void]>> = {
27+
basic: [[100, click("tab-2")]],
28+
rapid: [
29+
[100, click("tab-1")],
30+
[130, click("tab-2")],
31+
[160, click("tab-3")],
32+
],
33+
midAnimation: [
34+
[100, click("tab-2-urgent")],
35+
[500, click("tab-0")],
36+
],
37+
group: [[100, click("toggle-top")]],
38+
}
39+
40+
function Tabs() {
41+
const [tab, setTab] = useState(0)
42+
const [, start] = useTransition()
43+
const deferredTab = useDeferredValue(tab)
44+
const renderedTab = mode === "deferred" ? deferredTab : tab
45+
useCommitLog("tab", renderedTab)
46+
47+
return (
48+
<div>
49+
{[0, 1, 2, 3].map((i) => (
50+
<span key={i}>
51+
<button
52+
id={`tab-${i}`}
53+
onClick={() => update(() => setTab(i), start)}
54+
>
55+
{i}
56+
</button>
57+
<button id={`tab-${i}-urgent`} onClick={() => setTab(i)}>
58+
{i}!
59+
</button>
60+
</span>
61+
))}
62+
<div style={{ display: "flex", width: tabWidth * 4 }}>
63+
{[0, 1, 2, 3].map((i) => (
64+
<div
65+
key={i}
66+
style={{
67+
width: tabWidth,
68+
height: 40,
69+
position: "relative",
70+
}}
71+
>
72+
Tab {i}
73+
{renderedTab === i ? (
74+
<motion.div
75+
layoutId="underline"
76+
data-track="underline"
77+
transition={layoutTransition}
78+
style={{
79+
position: "absolute",
80+
bottom: 0,
81+
left: 0,
82+
width: tabWidth,
83+
height: 4,
84+
background: "red",
85+
}}
86+
/>
87+
) : null}
88+
</div>
89+
))}
90+
</div>
91+
<SlowList tick={renderedTab} />
92+
</div>
93+
)
94+
}
95+
96+
function Top() {
97+
const [open, setOpen] = useState(false)
98+
const [, start] = useTransition()
99+
const deferredOpen = useDeferredValue(open)
100+
const renderedOpen = mode === "deferred" ? deferredOpen : open
101+
useCommitLog("top", renderedOpen)
102+
103+
return (
104+
<motion.div
105+
layout
106+
transition={layoutTransition}
107+
data-track="top"
108+
style={{
109+
width: 200,
110+
height: renderedOpen ? 300 : 100,
111+
background: "green",
112+
}}
113+
>
114+
<button
115+
id="toggle-top"
116+
onClick={() => update(() => setOpen((o) => !o), start)}
117+
>
118+
toggle
119+
</button>
120+
<SlowList tick={renderedOpen} />
121+
</motion.div>
122+
)
123+
}
124+
125+
/**
126+
* Never re-renders after mount: it can only animate because it's in the
127+
* same LayoutGroup as Top.
128+
*/
129+
const Below = memo(() => (
130+
<motion.div
131+
layout
132+
transition={layoutTransition}
133+
data-track="below"
134+
style={{ width: 200, height: 100, background: "blue" }}
135+
/>
136+
))
137+
138+
export const App = () => {
139+
const scenario = params.get("scenario") || "basic"
140+
141+
return (
142+
<div style={{ padding: 20 }}>
143+
<button id="run" onClick={() => runScript(scenarios[scenario])}>
144+
run
145+
</button>
146+
<Tabs />
147+
<LayoutGroup>
148+
<Top />
149+
<Below />
150+
</LayoutGroup>
151+
</div>
152+
)
153+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { motion } from "framer-motion"
2+
import { memo, useDeferredValue, useState, useTransition } from "react"
3+
import {
4+
SlowList as SlowListBase,
5+
click,
6+
duration,
7+
linear,
8+
mode,
9+
params,
10+
runScript,
11+
update,
12+
useCommitLog,
13+
} from "./start-transition-helpers"
14+
15+
/**
16+
* Layout animations driven by state updates wrapped in startTransition /
17+
* useTransition / useDeferredValue, with a deliberately slow render so
18+
* concurrent renders yield across several frames.
19+
*
20+
* ?mode=sync|transition|useTransition|deferred
21+
* ?scenario=basic|rapid|interrupt|midAnimation|reverseBeforeCommit
22+
*/
23+
24+
const SlowList = memo(SlowListBase)
25+
const step = 200
26+
const layoutTransition = { layout: { ...linear, duration } }
27+
28+
const scenarios: Record<string, Array<[number, () => void]>> = {
29+
basic: [[100, click("a-next")]],
30+
rapid: [
31+
[100, click("a-next")],
32+
[130, click("a-next")],
33+
[160, click("a-next")],
34+
],
35+
interrupt: [
36+
[100, click("a-next")],
37+
[150, click("b-urgent")],
38+
],
39+
midAnimation: [
40+
[100, click("a-urgent-next")],
41+
[500, click("a-back")],
42+
],
43+
reverseBeforeCommit: [
44+
[100, click("a-next")],
45+
[140, click("a-back")],
46+
],
47+
}
48+
49+
export const App = () => {
50+
const [pos, setPos] = useState(0)
51+
const [posB, setPosB] = useState(0)
52+
const [isPending, start] = useTransition()
53+
const deferredPos = useDeferredValue(pos)
54+
const renderedPos = mode === "deferred" ? deferredPos : pos
55+
56+
useCommitLog("a", renderedPos)
57+
useCommitLog("b", posB)
58+
59+
const scenario = params.get("scenario") || "basic"
60+
61+
return (
62+
<div style={{ padding: 20 }}>
63+
<button id="run" onClick={() => runScript(scenarios[scenario])}>
64+
run
65+
</button>
66+
<button
67+
id="a-next"
68+
onClick={() => update(() => setPos((p) => p + 1), start)}
69+
>
70+
a next
71+
</button>
72+
<button
73+
id="a-back"
74+
onClick={() => update(() => setPos((p) => p - 1), start)}
75+
>
76+
a back
77+
</button>
78+
<button id="a-urgent-next" onClick={() => setPos((p) => p + 1)}>
79+
a urgent next
80+
</button>
81+
<button id="b-urgent" onClick={() => setPosB((p) => p + 1)}>
82+
b urgent
83+
</button>
84+
<span id="pending">{isPending ? "pending" : "idle"}</span>
85+
<div style={{ position: "relative", width: 1000 }}>
86+
<motion.div
87+
id="a"
88+
data-track="a"
89+
layout
90+
transition={layoutTransition}
91+
style={{
92+
width: 100,
93+
height: 100,
94+
marginLeft: renderedPos * step,
95+
background: "red",
96+
}}
97+
/>
98+
<motion.div
99+
id="b"
100+
data-track="b"
101+
layout
102+
transition={layoutTransition}
103+
style={{
104+
width: 100,
105+
height: 100,
106+
marginLeft: posB * step,
107+
background: "blue",
108+
}}
109+
/>
110+
</div>
111+
<SlowList tick={renderedPos} />
112+
</div>
113+
)
114+
}

0 commit comments

Comments
 (0)