React Motion (Motion for React)
1. Setup
- New code:
npm i motionandimport { motion, AnimatePresence } from "motion/react". - If the project already uses
framer-motion, keep it consistent; to migrate, swap the package and change importsframer-motion→motion/react(API is the same). - Next.js App Router: components using
motion.*must be client components ("use client"). For server components, useimport * as motion from "motion/react-client". - Wrap the app once:
<MotionConfig reducedMotion="user" transition={{ duration: 0.3, ease: [0.22, 1, 0.36, 1] }}>- Bundle-size sensitive? Use
LazyMotion+mcomponents withdomAnimationfeatures.
2. Core patterns
Variants + stagger (lists, hero groups)
const list = { hidden: {}, show: { transition: { staggerChildren: 0.06, delayChildren: 0.1 } } };
const item = { hidden: { opacity: 0, y: 16 }, show: { opacity: 1, y: 0 } };
<motion.ul variants={list} initial="hidden" whileInView="show" viewport={{ once: true, amount: 0.3 }}>
{items.map(i => <motion.li key={i.id} variants={item} />)}
</motion.ul>Enter/exit (modals, toasts, dropdowns) — AnimatePresence must stay mounted; the child gets a stable key:
<AnimatePresence>
{open && (
<motion.div key="modal" initial={{ opacity: 0, scale: 0.96 }} animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.98, transition: { duration: 0.15 } }} />
)}
</AnimatePresence>Use mode="wait" when swapping one element for another (tab content, steps).
Layout animation — layout prop animates size/position changes (accordion, filtering grid, reordering). Shared element: same layoutId in two places (tab underline, card → detail). Wrap related groups in <LayoutGroup>.
Micro-interactions
<motion.button whileHover={{ y: -2 }} whileTap={{ scale: 0.97 }}
transition={{ type: "spring", stiffness: 400, damping: 25 }} />Springs for physical feel (buttons, drag, toggles); tweens with ease-out for reveals.
Scroll-linked
const ref = useRef(null);
const { scrollYProgress } = useScroll({ target: ref, offset: ["start end", "end start"] });
const y = useTransform(scrollYProgress, [0, 1], [60, -60]);
<motion.img ref={ref} style={{ y }} />For complex pinned/scrubbed storytelling, use GSAP instead (scroll-animation skill).
Drag — drag="x", dragConstraints={ref}, dragElastic, onDragEnd with velocity for swipe carousels.
3. Page transitions in Next.js
- App Router has no built-in exit animations for routes. Options, simplest first:
- Enter-only animation in
app/template.tsx(re-mounts on navigation): wrap children in amotion.divfade/slide-in. - View Transitions API (
document.startViewTransition, or the framework's experimental support) for cross-fade/morph between routes. - Full enter+exit: a client wrapper with
AnimatePresence mode="wait"keyed onusePathname()— works but needs care with the router freezing old content; test thoroughly.
- Enter-only animation in
- Pages Router:
AnimatePresencearound<Component key={router.asPath} />in_app. - Keep route transitions short (≤ 400ms) — users navigate often.
4. Rules
- Animate transforms and opacity. For size changes use
layout, not animatingheightdirectly (exception:height: "auto"for accordions is supported, use sparingly). - Don't animate on every render — stable
initial/animatevalues, memoize variants outside the component. whileInViewwithviewport={{ once: true }}for reveals; don't replay on every scroll pass.- Respect reduced motion (
MotionConfig reducedMotion="user"oruseReducedMotion()for custom logic). - Focus management in animated modals/menus still applies (see
a11y-audit): focus trap,Esc, return focus. - Don't use Motion and GSAP on the same element.
5. Verify
Click/hover/tab through each animated component; open/close quickly many times (no stuck states); check with reduced motion on; React DevTools Profiler shows no re-render storms during animation.