Skip to content

Commit 12018e8

Browse files
committed
Add AnimateView via motion/react-animate-view
1 parent 939c267 commit 12018e8

22 files changed

Lines changed: 718 additions & 21 deletions

File tree

‎dev/react-19/package.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"dependencies": {
1414
"@tanstack/react-virtual": "^3.13.22",
1515
"motion": "^13.3.0",
16-
"react": "^19.0.0",
17-
"react-dom": "^19.0.0"
16+
"react": "^19.3.0",
17+
"react-dom": "^19.3.0"
1818
},
1919
"devDependencies": {
2020
"@radix-ui/react-dialog": "^1.1.15",
@@ -29,7 +29,7 @@
2929
"resolutions": {
3030
"@types/react": "^19.0.0",
3131
"@types/react-dom": "^19.0.0",
32-
"react": "^19.0.0",
33-
"react-dom": "^19.0.0"
32+
"react": "^19.3.0",
33+
"react-dom": "^19.3.0"
3434
}
3535
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { motion } from "motion/react"
2+
import { AnimateView } from "motion/react-animate-view"
3+
import React, {
4+
Component,
5+
PropsWithChildren,
6+
startTransition,
7+
useState,
8+
} from "react"
9+
10+
class Boundary extends Component<PropsWithChildren, { error: string }> {
11+
state = { error: "" }
12+
static getDerivedStateFromError(error: Error) {
13+
return { error: error.message }
14+
}
15+
render() {
16+
return this.state.error ? (
17+
<p id="error">{this.state.error}</p>
18+
) : (
19+
this.props.children
20+
)
21+
}
22+
}
23+
24+
export function App() {
25+
const [step, setStep] = useState(0)
26+
const mode = new URLSearchParams(location.search).get("mode") || "default"
27+
const supported = !!(React as any).ViewTransition
28+
const custom = mode === "custom"
29+
30+
return (
31+
<>
32+
<div id="supported">{String(supported)}</div>
33+
<motion.div id="ordinary" animate={{ opacity: 0.5 }}>
34+
React {React.version}
35+
</motion.div>
36+
<button
37+
id="toggle"
38+
onClick={() => startTransition(() => setStep(step + 1))}
39+
>
40+
Toggle
41+
</button>
42+
<pre id="events" />
43+
<Boundary>
44+
{((supported && (mode === "share" || mode === "update")) ||
45+
step % 2 === 1) && (
46+
<AnimateView
47+
key={mode === "update" ? "update" : step}
48+
name={mode === "share" ? "shared" : undefined}
49+
transition={{ duration: 0.4, ease: "linear" }}
50+
enter={custom ? { opacity: [0, 1] } : undefined}
51+
exit={custom ? { opacity: [1, 0] } : undefined}
52+
onAnimationStart={(animation, type) => {
53+
const output = document.getElementById("events")!
54+
const animations = (animation as any).animations
55+
output.dataset[type] = JSON.stringify({
56+
duration: animation.duration,
57+
count: animations.length,
58+
})
59+
}}
60+
onAnimationComplete={(type) => {
61+
document.getElementById("events")!.dataset[
62+
`${type}Complete`
63+
] = "true"
64+
}}
65+
>
66+
<div
67+
id="view"
68+
style={{
69+
width: 100 + step * 20,
70+
height: 100,
71+
background: step % 2 ? "red" : "blue",
72+
}}
73+
>
74+
View {step}
75+
</div>
76+
</AnimateView>
77+
)}
78+
</Boundary>
79+
</>
80+
)
81+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"private": true,
3+
"types": "../dist/animate-view.d.ts",
4+
"main": "../dist/cjs/animate-view.js",
5+
"module": "../dist/es/animate-view.mjs"
6+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
describe("AnimateView entry point", () => {
2+
it("keeps ordinary Motion working and reports unsupported React versions", () => {
3+
cy.visit("?test=animate-view")
4+
cy.get("#ordinary").should("have.css", "opacity", "0.5")
5+
cy.get("#ordinary").then(($ordinary) => {
6+
if ($ordinary.text().includes("React 19")) {
7+
cy.get("#supported").should("have.text", "true")
8+
}
9+
})
10+
cy.get("#supported").then(($supported) => {
11+
if ($supported.text() === "true") return
12+
cy.on("uncaught:exception", (error) => {
13+
if (error.message.includes("AnimateView requires React 19.3"))
14+
return false
15+
})
16+
cy.get("#toggle").click()
17+
cy.get("#error").should(
18+
"contain",
19+
"AnimateView requires React 19.3"
20+
)
21+
})
22+
})
23+
24+
for (const mode of ["default", "custom", "share", "update"]) {
25+
it(`animates ${mode} views with Motion timing`, () => {
26+
cy.visit(`?test=animate-view&mode=${mode}`)
27+
cy.get("#supported").then(($supported) => {
28+
// React 18 exercises import safety and the version error above.
29+
if ($supported.text() !== "true") return
30+
cy.document().then((doc) => {
31+
expect(typeof (doc as any).startViewTransition).to.equal(
32+
"function"
33+
)
34+
})
35+
cy.get("#toggle").click()
36+
const type =
37+
mode === "share" || mode === "update" ? mode : "enter"
38+
cy.get("#events")
39+
.should("have.attr", `data-${type}`)
40+
.then((data) => {
41+
const event = JSON.parse(data as unknown as string)
42+
expect(event.duration).to.be.closeTo(0.4, 0.01)
43+
expect(event.count).to.be.greaterThan(0)
44+
if (mode === "custom") expect(event.count).to.equal(1)
45+
})
46+
cy.get("#events").should(
47+
"have.attr",
48+
`data-${type}-complete`,
49+
"true"
50+
)
51+
if (type === "enter") {
52+
cy.get("#toggle").click()
53+
cy.get("#events").should(
54+
"have.attr",
55+
"data-exit-complete",
56+
"true"
57+
)
58+
cy.get("#view").should("not.exist")
59+
}
60+
})
61+
})
62+
}
63+
})

‎packages/framer-motion/package.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
"import": "./dist/es/projection.mjs",
5353
"default": "./dist/es/projection.mjs"
5454
},
55+
"./animate-view": {
56+
"types": "./dist/animate-view.d.ts",
57+
"require": "./dist/cjs/animate-view.js",
58+
"import": "./dist/es/animate-view.mjs",
59+
"default": "./dist/cjs/animate-view.js"
60+
},
5561
"./package.json": "./package.json"
5662
},
5763
"types": "dist/index.d.ts",

‎packages/framer-motion/rollup.config.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,14 @@ const cjs = Object.assign({}, config, {
143143
/**
144144
* Bundle separately so bundles don't share common modules
145145
*/
146+
const cjsAnimateView = Object.assign({}, cjs, { input: "lib/animate-view.js" })
146147
const cjsDebug = Object.assign({}, cjs, { input : "lib/debug.js" })
147148
const cjsDom = Object.assign({}, cjs, { input : "lib/dom.js" })
148149
const cjsMini = Object.assign({}, cjs, { input : "lib/mini.js" })
149150
const cjsDomMini = Object.assign({}, cjs, { input : "lib/dom-mini.js" })
150151

151152
export const es = Object.assign({}, config, {
152-
input: ["lib/index.js", "lib/mini.js", "lib/debug.js", "lib/dom.js", "lib/dom-mini.js", "lib/client.js", "lib/m.js","lib/projection.js"],
153+
input: ["lib/index.js", "lib/mini.js", "lib/debug.js", "lib/dom.js", "lib/dom-mini.js", "lib/client.js", "lib/m.js","lib/projection.js", "lib/animate-view.js"],
153154
output: {
154155
entryFileNames: "[name].mjs",
155156
format: "es",
@@ -181,6 +182,7 @@ function createTypes(input, file) {
181182
}
182183
}
183184

185+
const animateViewTypes = createTypes("types/animate-view.d.ts", "dist/animate-view.d.ts")
184186
const indexTypes = createTypes("types/index.d.ts", "dist/index.d.ts")
185187
const clientTypes = createTypes("types/client.d.ts", "dist/client.d.ts")
186188
const miniTypes = createTypes("types/mini.d.ts", "dist/mini.d.ts")
@@ -199,10 +201,12 @@ export default [
199201
umdDomMiniProd,
200202
cjs,
201203
cjsDebug,
204+
cjsAnimateView,
202205
cjsMini,
203206
cjsDom,
204207
cjsDomMini,
205208
es,
209+
animateViewTypes,
206210
indexTypes,
207211
clientTypes,
208212
debugTypes,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use client"
2+
3+
export { AnimateView } from "./components/AnimateView"
4+
export type { AnimateViewProps } from "./components/AnimateView/types"
5+
export type {
6+
ViewAnimationType,
7+
ViewAnimationStartCallback,
8+
ViewAnimationCompleteCallback,
9+
} from "motion-dom"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useInsertionEffect } from "react"
2+
3+
let hasInsertedTransitionResetStyle = false
4+
5+
export function useResetViewTransitions(): void {
6+
useInsertionEffect(() => {
7+
if (hasInsertedTransitionResetStyle) return
8+
hasInsertedTransitionResetStyle = true
9+
const style = document.createElement("style")
10+
style.textContent = `
11+
::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) {
12+
animation-timing-function: linear !important;
13+
}
14+
`
15+
document.head.appendChild(style)
16+
}, [])
17+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"use client"
2+
3+
import { animateView, ViewAnimationType } from "motion-dom"
4+
import React, {
5+
ComponentType,
6+
PropsWithChildren,
7+
useInsertionEffect,
8+
} from "react"
9+
import { useResetViewTransitions } from "./hooks/use-reset-view-transitions"
10+
import { sharedProps } from "./shared-props"
11+
import { AnimateViewProps } from "./types"
12+
13+
type ViewTransitionEvent = (
14+
instance: { name: string },
15+
types: string[]
16+
) => VoidFunction
17+
18+
interface ViewTransitionProps {
19+
name?: string
20+
onEnter: ViewTransitionEvent
21+
onExit: ViewTransitionEvent
22+
onShare: ViewTransitionEvent
23+
onUpdate: ViewTransitionEvent
24+
}
25+
26+
// Keep the entry point importable with React 18 and its type definitions.
27+
const ViewTransition = (
28+
React as unknown as {
29+
ViewTransition?: ComponentType<PropsWithChildren<ViewTransitionProps>>
30+
}
31+
).ViewTransition
32+
33+
/**
34+
* Animate enter, exit, update and shared view transitions.
35+
* Requires React and React DOM 19.3 or later.
36+
*/
37+
export function AnimateView({
38+
children,
39+
...props
40+
}: PropsWithChildren<AnimateViewProps>): React.ReactElement {
41+
if (!ViewTransition) {
42+
throw new Error("AnimateView requires React 19.3 or later.")
43+
}
44+
45+
useResetViewTransitions()
46+
const { name } = props
47+
48+
useInsertionEffect(() => {
49+
if (!name) return
50+
sharedProps.set(name, props)
51+
return () => {
52+
if (sharedProps.get(name) === props) sharedProps.delete(name)
53+
}
54+
})
55+
56+
const createAnimation =
57+
(type: ViewAnimationType): ViewTransitionEvent =>
58+
({ name: viewName }, types) =>
59+
animateView(
60+
viewName,
61+
type,
62+
sharedProps.get(viewName) || props,
63+
types
64+
)
65+
66+
return (
67+
<ViewTransition
68+
name={name}
69+
onEnter={createAnimation("enter")}
70+
onExit={createAnimation("exit")}
71+
onShare={createAnimation("share")}
72+
onUpdate={createAnimation("update")}
73+
>
74+
{children}
75+
</ViewTransition>
76+
)
77+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { AnimateViewProps } from "./types"
2+
3+
export const sharedProps = new Map<string, AnimateViewProps>()

0 commit comments

Comments
 (0)