Loading...
Loading...
Compare original and translation side by side
motionframer-motion"motion/react"motionframer-motion"motion/react"pnpm add motionpnpm add motion// Standard React (Vite, CRA, Pages Router)
import { motion, AnimatePresence } from "motion/react"
// Next.js App Router — use "motion/react-client" for RSC tree-shaking
"use client"
import * as motion from "motion/react-client"
// Minimal bundle (2.3 KB) — imperative API only
import { useAnimate } from "motion/react-mini"
// Reduced bundle (4.6 KB) — LazyMotion + m component
import { LazyMotion, domAnimation, m } from "motion/react"// 标准React项目(Vite、CRA、Pages Router)
import { motion, AnimatePresence } from "motion/react"
// Next.js App Router — 使用 "motion/react-client" 实现RSC摇树优化
"use client"
import * as motion from "motion/react-client"
// 最小体积包(2.3 KB)—— 仅包含命令式API
import { useAnimate } from "motion/react-mini"
// 精简体积包(4.6 KB)—— 包含 LazyMotion + m 组件
import { LazyMotion, domAnimation, m } from "motion/react"motion<motion.div />
<motion.button />
<motion.svg />
<motion.circle />motion.create()const MotionBox = motion.create(Box)
// forwardRef required — the ref must reach a DOM nodemotion<motion.div />
<motion.button />
<motion.svg />
<motion.circle />motion.create()const MotionBox = motion.create(Box)
// 需要使用 forwardRef —— ref 必须能传递到DOM节点<motion.div
initial={{ opacity: 0, y: 20 }} // mount state (or false to skip)
animate={{ opacity: 1, y: 0 }} // target state
exit={{ opacity: 0, y: -20 }} // unmount state (needs AnimatePresence)
transition={{ type: "spring", bounce: 0.25 }}
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
whileFocus={{ borderColor: "#00f" }}
whileDrag={{ scale: 1.1 }}
whileInView={{ opacity: 1 }}
viewport={{ once: true, margin: "-100px" }}
/><motion.div
initial={{ opacity: 0, y: 20 }} // 挂载时的状态(设为false可跳过)
animate={{ opacity: 1, y: 0 }} // 目标状态
exit={{ opacity: 0, y: -20 }} // 卸载时的状态(需要配合AnimatePresence使用)
transition={{ type: "spring", bounce: 0.25 }}
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
whileFocus={{ borderColor: "#00f" }}
whileDrag={{ scale: 1.1 }}
whileInView={{ opacity: 1 }}
viewport={{ once: true, margin: "-100px" }}
/>opacityfilterbackground-imagemask-imagexyzscalescaleXscaleYrotaterotateXrotateYrotateZskewXskewYoriginXoriginYoriginZ"100px""auto"transform<motion.li
initial={{ transform: "translateX(-100px)" }}
animate={{ transform: "translateX(0px)" }}
transition={{ type: "spring" }}
/>opacityfilterbackground-imagemask-imagexyzscalescaleXscaleYrotaterotateXrotateYrotateZskewXskewYoriginXoriginYoriginZ"100px""auto"transform<motion.li
initial={{ transform: "translateX(-100px)" }}
animate={{ transform: "translateX(0px)" }}
transition={{ type: "spring" }}
/><motion.div animate={{ x: [0, 100, 0] }} />
// null = "use current value"
<motion.div animate={{ x: [null, 100, 0] }} /><motion.div animate={{ x: [0, 100, 0] }} />
// null = "使用当前值"
<motion.div animate={{ x: [null, 100, 0] }} />const list = {
visible: {
transition: { staggerChildren: 0.1 }
},
hidden: {}
}
const item = {
visible: { opacity: 1, y: 0 },
hidden: { opacity: 0, y: 20 }
}
<motion.ul initial="hidden" animate="visible" variants={list}>
<motion.li variants={item} />
<motion.li variants={item} />
</motion.ul>animateinitialexitconst list = {
visible: {
transition: { staggerChildren: 0.1 }
},
hidden: {}
}
const item = {
visible: { opacity: 1, y: 0 },
hidden: { opacity: 0, y: 20 }
}
<motion.ul initial="hidden" animate="visible" variants={list}>
<motion.li variants={item} />
<motion.li variants={item} />
</motion.ul>animateinitialexitimport { AnimatePresence } from "motion/react"
<AnimatePresence>
{isVisible && (
<motion.div
key="modal" // REQUIRED: unique key
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
/>
)}
</AnimatePresence>keyexitmotion// WRONG — AnimatePresence unmounts with condition
{show && <AnimatePresence><motion.div /></AnimatePresence>}
// CORRECT — condition inside AnimatePresence
<AnimatePresence>{show && <motion.div key="k" />}</AnimatePresence>"sync""wait""popLayout"key<AnimatePresence mode="wait">
<motion.img
key={image.src}
initial={{ x: 300, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
exit={{ x: -300, opacity: 0 }}
/>
</AnimatePresence>customusePresenceData<AnimatePresence custom={direction}>
<Slide key={id} />
</AnimatePresence>
// Inside Slide:
const direction = usePresenceData()import { AnimatePresence } from "motion/react"
<AnimatePresence>
{isVisible && (
<motion.div
key="modal" // 必须:唯一key
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
/>
)}
</AnimatePresence>keyexitmotion// 错误写法 —— AnimatePresence会随条件判断一起卸载
{show && <AnimatePresence><motion.div /></AnimatePresence>}
// 正确写法 —— 条件判断放在AnimatePresence内部
<AnimatePresence>{show && <motion.div key="k" />}</AnimatePresence>"sync""wait""popLayout"key<AnimatePresence mode="wait">
<motion.img
key={image.src}
initial={{ x: 300, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
exit={{ x: -300, opacity: 0 }}
/>
</AnimatePresence>customusePresenceData<AnimatePresence custom={direction}>
<Slide key={id} />
</AnimatePresence>
// Slide组件内部:
const direction = usePresenceData()// Spring (default for physical props: x, y, scale)
transition={{ type: "spring", bounce: 0.25 }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
transition={{ type: "spring", visualDuration: 0.5, bounce: 0.25 }}
// Tween (default for opacity, color)
transition={{ duration: 0.3, ease: "easeInOut" }}
// Per-value transitions
transition={{
default: { type: "spring" },
opacity: { duration: 0.2, ease: "linear" }
}}
// Orchestration
transition={{ delay: 0.5, repeat: Infinity, repeatType: "reverse" }}
// Global default
<MotionConfig transition={{ duration: 0.3 }}>// 弹簧动画(位移、缩放等物理属性的默认效果)
transition={{ type: "spring", bounce: 0.25 }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
transition={{ type: "spring", visualDuration: 0.5, bounce: 0.25 }}
// 补间动画(透明度、颜色的默认效果)
transition={{ duration: 0.3, ease: "easeInOut" }}
// 针对单个属性的过渡配置
transition={{
default: { type: "spring" },
opacity: { duration: 0.2, ease: "linear" }
}}
// 编排配置
transition={{ delay: 0.5, repeat: Infinity, repeatType: "reverse" }}
// 全局默认配置
<MotionConfig transition={{ duration: 0.3 }}>// Auto-animate any layout change
<motion.div layout />
// Shared element transitions
<motion.div layoutId="underline" />
// Customize layout transition
<motion.div layout transition={{ layout: { duration: 0.3 } }} />// 自动为所有布局变化添加动画
<motion.div layout />
// 共享元素过渡
<motion.div layoutId="underline" />
// 自定义布局过渡效果
<motion.div layout transition={{ layout: { duration: 0.3 } }} /><motion.div
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
drag // enable both axes
drag="x" // constrain to x-axis
dragConstraints={{ left: -100, right: 100 }}
dragElastic={0.2}
/><motion.div
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
drag // 开启双轴拖拽
drag="x" // 限制为x轴拖拽
dragConstraints={{ left: -100, right: 100 }}
dragElastic={0.2}
/>// Viewport-triggered
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true }}
/>
// Scroll-linked progress bar
const { scrollYProgress } = useScroll()
<motion.div style={{ scaleX: scrollYProgress }} />
// Element scroll progress
const ref = useRef(null)
const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"]
})// 视口触发动画
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true }}
/>
// 滚动关联进度条
const { scrollYProgress } = useScroll()
<motion.div style={{ scaleX: scrollYProgress }} />
// 元素滚动进度
const ref = useRef(null)
const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"]
})// Manual motion values (no re-renders)
const x = useMotionValue(0)
const opacity = useTransform(x, [-200, 0, 200], [0, 1, 0])
<motion.div drag="x" style={{ x, opacity }} />
// Smooth spring following
const springX = useSpring(x, { stiffness: 100, damping: 30 })
// Imperative animation control
const [scope, animate] = useAnimate()
animate("li", { opacity: 1 }, { stagger: 0.1 })
// Event listener (no re-render)
useMotionValueEvent(scrollY, "change", (v) => console.log(v))// 手动创建motion值(不会触发重渲染)
const x = useMotionValue(0)
const opacity = useTransform(x, [-200, 0, 200], [0, 1, 0])
<motion.div drag="x" style={{ x, opacity }} />
// 平滑弹簧跟随效果
const springX = useSpring(x, { stiffness: 100, damping: 30 })
// 命令式动画控制
const [scope, animate] = useAnimate()
animate("li", { opacity: 1 }, { stagger: 0.1 })
// 事件监听器(不会触发重渲染)
useMotionValueEvent(scrollY, "change", (v) => console.log(v))| Approach | Size | What you get |
|---|---|---|
| ~34 KB | Full API |
| ~4.6 KB | Declarative animations, no gestures |
| ~2.3 KB | |
// LazyMotion pattern
import { LazyMotion, domAnimation, m } from "motion/react"
<LazyMotion features={domAnimation}>
<m.div animate={{ opacity: 1 }} />
</LazyMotion>| 方案 | 体积 | 包含功能 |
|---|---|---|
| ~34 KB | 完整API |
| ~4.6 KB | 声明式动画,不含手势功能 |
| ~2.3 KB | 仅包含 |
// LazyMotion使用模式
import { LazyMotion, domAnimation, m } from "motion/react"
<LazyMotion features={domAnimation}>
<m.div animate={{ opacity: 1 }} />
</LazyMotion><MotionConfig reducedMotion="user">
<App />
</MotionConfig>"user""always""never"useReducedMotion()true<MotionConfig reducedMotion="user">
<App />
</MotionConfig>"user""always""never"useReducedMotion()truetransition-*// WRONG — Tailwind transition conflicts with Motion
<motion.div className="transition-all duration-300" animate={{ x: 100 }} />
// CORRECT — Tailwind for styling, Motion for animation
<motion.div className="rounded-lg bg-blue-600 p-4" whileHover={{ scale: 1.05 }} />transition-*// 错误写法 —— Tailwind过渡效果与Motion冲突
<motion.div className="transition-all duration-300" animate={{ x: 100 }} />
// 正确写法 —— Tailwind负责样式,Motion负责动画
<motion.div className="rounded-lg bg-blue-600 p-4" whileHover={{ scale: 1.05 }} />"motion/react-client"// components/motion-client.tsx
"use client"
import * as motion from "motion/react-client"
export { motion }
// app/page.tsx (Server Component)
import { motion } from "@/components/motion-client"
<motion.div animate={{ opacity: 1 }} />"motion/react-client"// components/motion-client.tsx
"use client"
import * as motion from "motion/react-client"
export { motion }
// app/page.tsx(服务端组件)
import { motion } from "@/components/motion-client"
<motion.div animate={{ opacity: 1 }} />keytransition-*height: "auto"display: "none"visibility: "hidden"layoutScrolllayoutRootpopLayoutforwardRefpropagatetruekeytransition-*height: "auto"display: "none"visibility: "hidden"layoutScrolllayoutRootpopLayoutforwardRefpropagatetrue