| name | motion-react |
| description | Build React animations with Motion (formerly Framer Motion) - gestures (drag, hover, tap),
scroll effects, spring physics, layout animations, SVG, exit animations, and motion values.
Use when: building React animations, adding hover/tap/drag interactions, scroll-triggered effects,
layout transitions, shared element animations, exit animations with AnimatePresence, or working
with motion values and springs. Triggers: "animate", "motion component", "framer motion",
"gesture", "drag", "scroll animation", "layout animation", "exit animation", "spring",
"whileHover", "whileTap", "whileInView", "AnimatePresence", "layoutId", "useScroll",
"useSpring", "useAnimate", "motion value", "reorder", "parallax".
|
Motion for React
Package: motion (formerly framer-motion). Import from "motion/react".
Installation
pnpm add motion
Imports
import { motion, AnimatePresence } from "motion/react"
"use client"
import * as motion from "motion/react-client"
import { useAnimate } from "motion/react-mini"
import { LazyMotion, domAnimation, m } from "motion/react"
Motion Component
Every HTML/SVG element has a motion counterpart:
<motion.div />
<motion.button />
<motion.svg />
<motion.circle />
Custom components: wrap with motion.create():
const MotionBox = motion.create(Box)
Core Animation Props
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
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" }}
/>
Animatable Values
Motion animates any CSS value: opacity, filter, background-image, mask-image.
Independent transforms (not possible in CSS alone):
- Translate:
x, y, z
- Scale:
scale, scaleX, scaleY
- Rotate:
rotate, rotateX, rotateY, rotateZ
- Skew:
skewX, skewY
- Origin:
originX, originY, originZ
Value types: numbers, strings with units ("100px"), colors (hex/rgba/hsla), "auto" for width/height.
Hardware acceleration: set transform directly for GPU compositing:
<motion.li
initial={{ transform: "translateX(-100px)" }}
animate={{ transform: "translateX(0px)" }}
transition={{ type: "spring" }}
/>
Keyframes
Pass arrays to animate through a sequence:
<motion.div animate={{ x: [0, 100, 0] }} />
<motion.div animate={{ x: [null, 100, 0] }} />
Variants
Named animation states for orchestrated animations:
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>
Variants propagate through the tree. Children inherit animate/initial/exit from parent.
AnimatePresence — Exit Animations
import { AnimatePresence } from "motion/react"
<AnimatePresence>
{isVisible && (
<motion.div
key="modal" // REQUIRED: unique key
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
/>
)}
</AnimatePresence>
Critical rules:
- AnimatePresence must stay mounted — never wrap it in a conditional
- Direct children must have unique
key props
exit prop only works on motion components inside AnimatePresence
{show && <AnimatePresence><motion.div /></AnimatePresence>}
<AnimatePresence>{show && <motion.div key="k" />}</AnimatePresence>
Modes: "sync" (default), "wait" (sequential enter/exit), "popLayout" (pop exiting element out of flow).
Slideshow pattern — change key to trigger exit+enter:
<AnimatePresence mode="wait">
<motion.img
key={image.src}
initial={{ x: 300, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
exit={{ x: -300, opacity: 0 }}
/>
</AnimatePresence>
Dynamic exit data — pass via custom prop + usePresenceData:
<AnimatePresence custom={direction}>
<Slide key={id} />
</AnimatePresence>
const direction = usePresenceData()
Transitions
For full transition API details, see references/transitions-api.md.
Quick reference:
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 }}>
Layout Animations
For full layout animation details, see references/layout-animations.md.
<motion.div layout />
<motion.div layoutId="underline" />
<motion.div layout transition={{ layout: { duration: 0.3 } }} />
Gestures & Drag
For full gesture/drag API, see references/gestures-and-drag.md.
<motion.div
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
drag
drag="x"
dragConstraints={{ left: -100, right: 100 }}
dragElastic={0.2}
/>
Scroll Animations
For full scroll API, see references/scroll-animations.md.
<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"]
})
Hooks & Motion Values
For full hooks API, see references/hooks-and-motion-values.md.
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))
Bundle Optimization
| Approach | Size | What you get |
|---|
motion/react | ~34 KB | Full API |
LazyMotion + m | ~4.6 KB | Declarative animations, no gestures |
motion/react-mini | ~2.3 KB | useAnimate only |
import { LazyMotion, domAnimation, m } from "motion/react"
<LazyMotion features={domAnimation}>
<m.div animate={{ opacity: 1 }} />
</LazyMotion>
Accessibility
<MotionConfig reducedMotion="user">
<App />
</MotionConfig>
Options: "user" (respect OS setting), "always" (force instant), "never" (ignore).
Hook: useReducedMotion() returns true when user prefers reduced motion.
Tailwind Integration
Let each library handle its strength. Remove Tailwind transition-* classes — they conflict.
<motion.div className="transition-all duration-300" animate={{ x: 100 }} />
<motion.div className="rounded-lg bg-blue-600 p-4" whileHover={{ scale: 1.05 }} />
Next.js App Router
Motion components require client-side rendering. Use "motion/react-client" for optimal tree-shaking:
"use client"
import * as motion from "motion/react-client"
export { motion }
import { motion } from "@/components/motion-client"
<motion.div animate={{ opacity: 1 }} />
Common Pitfalls
- Exit animations not firing — AnimatePresence must stay mounted; children need unique
key
- Tailwind transition conflict — Remove
transition-* classes from motion elements
height: "auto" + display: "none" — Use visibility: "hidden" instead
- Layout animations in scrollable containers — Add
layoutScroll prop to scroll parent
- Layout animations in fixed elements — Add
layoutRoot prop to fixed parent
- Percentage transforms + layout — Convert to pixels; percentage values break FLIP calculation
popLayout mode — Custom components must use forwardRef to forward ref to DOM node
- AnimatePresence
propagate — Set to true on nested AnimatePresence to fire child exits
References