Créer et déboguer les animations Motion pour React

VérifiéSûr

Créer et déboguer des animations Motion pour React avec les bonnes pratiques d'animations.dev. Couvre les ressorts, les morphismes de layout, les animations de sortie, et plus.

Spar Skills Guide Bot
DeveloppementIntermédiaire
29/08/2026
Claude CodeCursorWindsurfCopilotCodex
#react#animation#motion#framer-motion#ui-components

Recommandé pour

Synthèse automatique

Référence pratique pour créer et déboguer des animations avec Motion for React (ex-Framer Motion), en suivant l'approche du cours Animations on the Web.

Points forts

  • Couvre l'ensemble de l'API utile : variants, AnimatePresence, layoutId, motion values, ressorts et gestes de glissement
  • Explique explicitement quand le CSS suffit et quand la bibliothèque justifie son poids dans le bundle
  • Fournit une section de débogage pour les échecs classiques : animation de sortie muette, rayon de bordure déformé lors d'un morph, glissement directionnel inversé, hauteur auto qui saute
  • Renvoie vers RECIPES.md pour des composants complets (popover, flux multi-étapes, carte App Store)

Limites

  • Suppose un projet React déjà équipé de Motion ou prêt à l'ajouter ; ce n'est pas agnostique du framework
  • Reste une synthèse de cours : ne remplace pas la documentation officielle pour les cas limites de l'API
  • Les recettes de composants vivent dans un fichier séparé non inclus ici
Quand l'utiliser

À utiliser dès que vous écrivez ou corrigez une animation Motion for React impliquant sortie de composant, morph layoutId, ressort ou valeur animée en dehors du rendu React.

Quand l'éviter

À éviter si votre animation se résout proprement en CSS pur ou si vous cherchez une référence pour une autre bibliothèque d'animation (GSAP, React Spring, CSS keyframes).

Analyse de sécurité

Sûr
Score qualité85/100

This is a documentation/reference skill for the Motion for React (Framer Motion) library. It contains no tool invocations, no shell commands, no network calls, no file operations, and no instructions to exfiltrate data or disable safety. All code samples are ordinary JSX animation examples. No declared allowed-tools means nothing is invoked beyond the agent's normal text generation.

Aucun point d'attention détecté

Comment cette analyse est faite

Exemples

Exit animation not playing
My modal's exit animation never plays when I close it with AnimatePresence — it just disappears instantly. Here's my component code, can you fix it?
Shared element morph
I want a card thumbnail to smoothly morph into a full-screen detail view using layoutId in motion/react. Can you write the two components and handle the border radius distortion during the morph?
Direction-aware slide
I'm building a multi-step flow with Motion for React and my slide transition goes the wrong way when the user hits Back. How do I make it direction-aware with variants and a custom prop?

name: motion-react description: Build animations with Motion for React (motion/react, formerly Framer Motion) the way the "Animations on the Web" course (animations.dev) teaches — and debug them when the magic misfires. Use when writing motion components, exit animations, layout or shared-element morphs, springs, motion values, or drag gestures; when deciding whether a job needs the library at all; or when an exit animation won't play, a morph distorts its border radius, a direction-aware slide points the wrong way, or an auto height jumps. Triggers on — motion/react, framer-motion, motion.div, initial animate exit, AnimatePresence, popLayout, mode wait, layout animation, layoutId, shared element transition, variants, custom prop, MotionConfig, useMotionValue, useSpring, useTransform, useMotionTemplate, useInView, useReducedMotion, useAnimate, useMeasure, animate height auto, drag, dragConstraints, dragMomentum, whileTap, spring bounce, stiffness, damping. metadata: short-description: Build and debug Motion for React animations (animations.dev)

Motion for React

The Framer Motion module of Emil Kowalski's Animations on the Web course (animations.dev), as a working reference.

Framer Motion is now Motion for React. Nothing changed but the import path:

import { motion, AnimatePresence } from "motion/react";  // was "framer-motion"

For the component builds — feedback popover, multi-step flow, trash interaction, interactive graph, App Store card — load RECIPES.md.

Reach for it only when CSS can't

Everything Motion does is possible in vanilla CSS and JS. It just takes far longer. Emil's own default is to skip the library when CSS gets there in reasonable time — enter/exit for modals and dropdowns via Radix's [data-state] attributes covers most of it, since Radix suspends unmount while a closing animation plays.

Reach for Motion when CSS genuinely can't:

  • Real spring physics — interruptible motion that keeps momentum.
  • Layout changes, including properties CSS can't animate at all (flex-direction, justify-content).
  • Shared-element morphs — one element becoming another (layoutId).
  • Animating out unmounted components (AnimatePresence) — hard in React, because the component is already gone.

The deciding factor is usually bundle size and what the project already ships. Vercel kept Motion out of the Next.js docs and rewrote a copy-button animation in CSS to save the weight; in Clerk's dashboard, which already had it, Emil reaches for it freely to save time and keep the code readable.

The cost of all this: magic. Complex animations come out of very little code, so when something misbehaves you're debugging a black box, and the docs mostly follow the happy path. The Debugging section below is the map for that.

Basics

<motion.div
  initial={{ opacity: 0, scale: 0.95 }}
  animate={{ opacity: 1, scale: 1 }}
/>

initial is the start state, animate the end state. Values are interpolated in JavaScript, outside React's render cycle — an animating component doesn't re-render, which is what makes 60fps achievable.

Transitions and springs

Without a transition, Motion picks per value type: physical values (x, scale) get a spring, everything else (opacity, color) gets a tween.

transition={{ duration: 0.3, ease: "easeOut" }}                          // tween
transition={{ type: "spring", duration: 0.3, bounce: 0 }}                // state swaps, no overshoot
transition={{ type: "spring", duration: 0.5, bounce: 0.2 }}              // slight, lively overshoot
transition={{ type: "spring", stiffness: 100, damping: 10, mass: 0.75 }} // physics form

Pull repeated spring configs into a constant. When a spring looks wrong — jittery, overshooting oddly — raise damping; that fixes it most of the time. Lower mass makes it track an input more tightly (a cursor follower wants mass: 0.1).

MotionConfig sets a default transition for a whole subtree, so a component reads as one entity instead of a pile of individually-tuned parts:

<MotionConfig transition={{ type: "spring", duration: 0.5, bounce: 0.2 }}>…</MotionConfig>

AnimatePresence

<AnimatePresence mode="popLayout" initial={false} custom={direction}>
  <motion.span
    key={state}          // REQUIRED
    custom={direction}   // pass again, or the exiting element reads stale props
    initial={{ opacity: 0, y: -25 }}
    animate={{ opacity: 1, y: 0 }}
    exit={{ opacity: 0, y: 25 }}
    transition={{ type: "spring", duration: 0.3, bounce: 0 }}
  />
</AnimatePresence>
  • A key is mandatory. Without it the component never unmounts and the exit never fires. When an exit animation does nothing, check the key first.
  • mode="wait" — the old element leaves completely before the new one enters. For icon swaps (copy → checkmark) and morphing one rich state into another.
  • mode="popLayout" — old and new animate simultaneously while siblings reflow around them. The right default for state swaps inside a button, step transitions, and crossfades. If an exit animation looks broken, try changing mode before anything else.
  • initial={false} skips the mount animation, so a state swap doesn't animate on first paint.
  • An exiting element's props are stale. It has already been removed from the tree, so it can't see the newest state. Pass custom to both AnimatePresence and the motion element to feed it fresh data — this is what makes direction-aware slides work.

Variants

Named target sets, reusable across initial/animate/exit. Their real power is the function form, which receives custom:

const variants = {
  initial: (direction) => ({ x: `${110 * direction}%`, opacity: 0 }),
  active:  { x: "0%", opacity: 1 },
  exit:    (direction) => ({ x: `${-110 * direction}%`, opacity: 0 }),
};

<motion.div variants={variants} initial="initial" animate="active" exit="exit" custom={direction} />

Store the direction in state (setDirection(1) on next, -1 on back) at the same time you change the step.

Layout and shared-element animations

The layout prop is the library's most powerful feature and the reason its animations feel native.

  • layout animates any layout change — position, size, flex-direction, justify-content. For layout animations you change the element's actual styles (className or inline), not the animate prop; Motion measures before and after and interpolates.
  • Add layout to neighbouring elements too, or they'll jump while the animating one glides.
  • layoutId connects two different elements: when one mounts carrying the same layoutId as one that just unmounted, Motion morphs between them. Tab indicators, App Store card → detail, button → popover, images flying into a bin.
  • You can't steer a shared layout animation. To add motion on top, animate the parent and let the children come along: <motion.div animate={{ y: 73 }} transition={{ delay: 0.13 }}>.
  • Always set an animated border-radius as an inline pixel value — style={{ borderRadius: 12 }}. Layout animations work by scaling, which distorts corners; Motion corrects for this only when the radius is in pixels, not from a class or a rem value.

Expect friction on complex cases — distortion, elements that won't line up. That's the magic tax. Inside Framer's Magic Motion explains the mechanism if you need to reason about it.

Animating height

Motion can animate a fixed height to auto, but not auto → auto — which is what dynamic content actually needs. Measure it:

import useMeasure from "react-use-measure";

const [ref, bounds] = useMeasure();

<motion.div animate={{ height: bounds.height ? bounds.height : null }}>
  <div ref={ref} className="inner">{content}</div>
</motion.div>
  • The ref and the animate={{ height }} must be on different elements. On the same one, the animated height sticks and the element stops reacting to content changes.
  • Put the padding on the inner element so the measurement includes it.
  • bounds.height is 0 on first render; falling back to null means auto and avoids a layout shift.
  • useMeasure is a ResizeObserver wrapper — hand-rolling one is a few lines if you'd rather not add the dependency.

Motion values

Motion values update outside React's render cycle, so a value can change 60 times a second without a single re-render. Use them for anything high-frequency — pointer tracking above all. The React-state version of a cursor follower re-renders on every mouse move; the motion-value version doesn't re-render at all.

const x = useMotionValue(0);                                  // .set() / .get()
<motion.div style={{ x }} />                                  // x/y map to translateX/translateY

| Hook | Use it for | | --- | --- | | useMotionValue | The value must track an input 1:1 — drag-to-dismiss scale, anything where a spring would feel disconnected from the finger. | | useSpring | Almost everything else. A raw motion value updates instantly and feels lifeless; a spring makes the same interaction feel alive. | | useTransform | Map one motion value's range onto another: useTransform(y, [0, 300], [1, 1.5]). The function form transforms the output instead — useful for animating a displayed number, and it stays subscribed so the rendered text keeps up. | | useMotionTemplate | Build a reactive string from motion values — a motion value dropped into a plain template literal is dead. This is how you animate clipPath, filter, or any composite string. |

const label = useTransform(angle, (v) => `${Math.round(v)}°`);          // function form
const clip  = useMotionTemplate`inset(0px ${clipValue}% 0px 0px)`;      // tagged template

useSpring genuinely changes how an interaction feels — the difference between a graph that follows your cursor and one that merely tracks it. Don't skip it to save a hook.

Gestures

<motion.div drag dragConstraints={boxRef} dragMomentum={false} whileTap={{ scale: 0.97 }} />

drag keeps momentum on release by default, which feels natural for a card being flung and wrong for a simple reposition — turn it off with dragMomentum={false}. dragConstraints takes a ref and bounds the element to it.

Accessibility

  • useReducedMotion() branches values in a component; <MotionConfig reducedMotion="user"> handles a whole app by animating only opacity and background.
  • Reduced motion means gentler, not none — keep opacity and color, drop the movement.
  • If you fake a placeholder with a real element (see the feedback popover), keep the input's actual placeholder attribute for screen readers and aria-hidden the fake one.

Debugging the magic

| Symptom | Cause | | --- | --- | | Exit animation doesn't play | Missing key, so the component never unmounts. Then: wrong mode. | | Direction-aware slide always goes the same way | The exiting element has stale props — pass custom to both AnimatePresence and the element. | | Border radius warps during a morph | Radius came from a class or rem. Move it to inline style={{ borderRadius: 12 }}. | | Height animation freezes or jumps | ref and animate={{ height }} are on the same element, or the first-render 0 isn't falling back to null. | | Neighbouring elements jump during a layout animation | They need layout too. | | A useMotionTemplate-less string never updates | Motion values aren't reactive inside plain template literals. | | Spring looks jittery or wild | Raise damping. | | Both elements visible at once when switching fast | A known AnimatePresence bug; if you can't work around it, 11.0.10 predates it. | | Animation runs on first paint when it shouldn't | Add initial={false} to AnimatePresence. |

Imperative animation

useAnimate() gives you [scope, animate] for low-level, selector-based control. It's more powerful and much harder to maintain — Emil almost never uses it. Stay declarative unless you're orchestrating many elements across events.

Du fichier au premier essai

Le téléchargement contient exactement le contenu affiché sur cette fiche. Cette copie peut différer de la dernière version du dépôt source.

Consulter la source et ses fichiers annexes

Ce téléchargement contient uniquement SKILL.md. Les scripts, références, modèles et dépendances éventuels ne sont pas inclus. Récupérez les fichiers nécessaires depuis la source en conservant leur arborescence avant de poursuivre.

1. Télécharger le fichier

Télécharger SKILL.md

Depuis la racine d’un projet de test, enregistrez le fichier sous .skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md, ou utilisez la commande suivante. Ce dossier sert à examiner la skill avant son installation.

Commande pour macOS/Linux ou WSL : un shell POSIX et curl sont nécessaires. Sous Windows sans WSL, utilisez le bouton de téléchargement et créez le dossier manuellement.

mkdir -p '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/build-and-debug-motion-for-react-animations-mte1lgw6/download' --output '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md.part' && mv '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md.part' '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md'

2. Vérifier les prérequis

Lisez SKILL.md avant de l’utiliser. Si le fichier cite des scripts ou services externes, examinez-les et préparez les accès nécessaires dans votre environnement de test.

Aucune section de prérequis reconnue dans ce fichier. Cela ne signifie pas qu’il est autonome : les bibliothèques, outils et accès peuvent être cités ailleurs dans le contenu ou dans le dépôt source.

Les plateformes de la fiche sont des indications du catalogue. Les chemins ci-dessous suivent la documentation des outils ; ils ne prouvent pas que cette skill y a été exécutée. Aucune version minimale de ces outils n’est attestée pour cette fiche.

3. Charger la skill

Après vérification, copiez le dossier complet dans le projet avec l’une des commandes ci-dessous, puis ouvrez une nouvelle session de l’outil. La commande s’arrête si un dossier du même nom existe déjà : comparez les fichiers avant de le remplacer.

Claude Code
test -f '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md' && test -s '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/motion-react' && cp -R '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6' '.claude/skills/motion-react'

Cursor
test -f '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md' && test -s '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/motion-react' && cp -R '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6' '.cursor/skills/motion-react'

Codex
test -f '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md' && test -s '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/motion-react' && cp -R '.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6' '.agents/skills/motion-react'

OpenSkills

OpenSkills doit déjà être installé. La procédure locale install/read est contrôlée avec OpenSkills 1.5.0, qui exige Node.js ≥ 20.6.0. Aucun slug du catalogue n’est résolu automatiquement.

Installation officielle d’OpenSkills
openskills install './.skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6'

openskills read 'build-and-debug-motion-for-react-animations-mte1lgw6'

La lecture doit afficher le contenu du fichier installé et son dossier de base. Elle ne lance ni la skill ni ses dépendances.

Pour un agent qui lit AGENTS.md, synchronisez ensuite l’index. Cette commande modifie AGENTS.md : relisez le diff avant de l’utiliser.

openskills sync

Mode manuel ou autre outil : joignez SKILL.md à une conversation, ou ouvrez-le dans un agent capable de lire les fichiers. Ajoutez les ressources annexes nécessaires. Il n’existe pas de dossier universel reconnu par tous les outils.

4. Faire un premier essai

Utilisez une entrée courte et fictive : un endpoint d’API, un petit document ou un brief d’interface. Dans une nouvelle conversation, joignez le fichier ou donnez son chemin à l’agent, puis adaptez cette demande :

Lis .skill-downloads/build-and-debug-motion-for-react-animations-mte1lgw6/SKILL.md et applique ses instructions à cet exemple : [décrire une entrée de test]. Indique les fichiers, dépendances ou accès qui manquent. Produis le résultat demandé par la skill sans inventer de données ni exécuter de script non examiné.

Résultat attendu : une sortie correspondant aux instructions de la skill, ou une liste précise des prérequis manquants. Une simple confirmation de lecture ne valide pas le premier usage. Si une ressource externe est indisponible, ce parcours reste à tester.

Skills similaires