Framer Motion Animations
When to use
Use this skill when building fluid UI animations in React, handling enter/exit transitions, animating modal dialogs, or creating drag-and-drop gestures.
Prerequisites
- React 18+ project environment.
framer-motion package installed (npm install framer-motion).
Step-by-step workflow
- Convert to motion elements: Replace standard HTML tags with motion equivalents (e.g.,
motion.div, motion.button).
- Define animate states: Use
initial, animate, and exit props or named variants for clean state transitions.
- Configure physics transitions: Set
transition={{ type: 'spring', stiffness: 300, damping: 25 }} for natural movement.
- Handle exit animations: Wrap conditional components with
<AnimatePresence> providing a unique key to each child.
- Respect reduced motion: Check user system preferences with
useReducedMotion() and suppress motion for sensitive users.
Commands / code examples
import React from 'react';
import { motion, AnimatePresence, useReducedMotion } from 'framer-motion';
interface ToastProps {
id: string;
message: string;
onDismiss: () => void;
}
export const Toast: React.FC<ToastProps> = ({ message, onDismiss }) => {
const shouldReduceMotion = useReducedMotion();
const variants = {
initial: { opacity: 0, y: shouldReduceMotion ? 0 : 20, scale: 0.95 },
animate: { opacity: 1, y: 0, scale: 1 },
exit: { opacity: 0, scale: 0.9, transition: { duration: 0.15 } },
};
return (
<motion.div
variants={variants}
initial="initial"
animate="animate"
exit="exit"
transition={{ type: 'spring', stiffness: 400, damping: 30 }}
className="bg-gray-900 text-white p-4 rounded-lg shadow-xl flex items-center justify-between"
>
<span>{message}</span>
<button className="ml-4 text-sm text-gray-400 hover:text-white">
Close
</button>
</motion.div>
);
};
Common mistakes & pitfalls
- Forgetting to provide a stable, unique
key to children of <AnimatePresence>, breaking exit animation detection.
- Animating non-performant properties like
width, height, or top instead of GPU-accelerated transform and opacity.
- Neglecting
useReducedMotion(), triggering vestibular disorders for sensitive users.
Verification checklist
Related skills
- react-fundamentals
- react-hooks-patterns
- shadcn-ui-radix-primitives
- wcag-22-guidelines-conformance-levels
1---2name: framer-motion-animations3description: Use this skill when implementing declarative physics-based micro-interactions, layout transitions, gestures, and exit animations in React.4metadata:5 source: https://skillmd.com/skills/nextboxis/framer-motion-animations6---78# Framer Motion Animations910## When to use11Use this skill when building fluid UI animations in React, handling enter/exit transitions, animating modal dialogs, or creating drag-and-drop gestures.1213## Prerequisites14- React 18+ project environment.15- `framer-motion` package installed (`npm install framer-motion`).1617## Step-by-step workflow181. **Convert to motion elements**: Replace standard HTML tags with motion equivalents (e.g., `motion.div`, `motion.button`).192. **Define animate states**: Use `initial`, `animate`, and `exit` props or named `variants` for clean state transitions.203. **Configure physics transitions**: Set `transition={{ type: 'spring', stiffness: 300, damping: 25 }}` for natural movement.214. **Handle exit animations**: Wrap conditional components with `<AnimatePresence>` providing a unique `key` to each child.225. **Respect reduced motion**: Check user system preferences with `useReducedMotion()` and suppress motion for sensitive users.2324## Commands / code examples25```tsx26import React from 'react';27import { motion, AnimatePresence, useReducedMotion } from 'framer-motion';2829interface ToastProps {30 id: string;31 message: string;32 onDismiss: () => void;33}3435export const Toast: React.FC<ToastProps> = ({ message, onDismiss }) => {36 const shouldReduceMotion = useReducedMotion();3738 const variants = {39 initial: { opacity: 0, y: shouldReduceMotion ? 0 : 20, scale: 0.95 },40 animate: { opacity: 1, y: 0, scale: 1 },41 exit: { opacity: 0, scale: 0.9, transition: { duration: 0.15 } },42 };4344 return (45 <motion.div46 variants={variants}47 initial="initial"48 animate="animate"49 exit="exit"50 transition={{ type: 'spring', stiffness: 400, damping: 30 }}51 className="bg-gray-900 text-white p-4 rounded-lg shadow-xl flex items-center justify-between"52 >53 <span>{message}</span>54 <button onClick={onDismiss} className="ml-4 text-sm text-gray-400 hover:text-white">55 Close56 </button>57 </motion.div>58 );59};60```6162## Common mistakes & pitfalls63- Forgetting to provide a stable, unique `key` to children of `<AnimatePresence>`, breaking exit animation detection.64- Animating non-performant properties like `width`, `height`, or `top` instead of GPU-accelerated `transform` and `opacity`.65- Neglecting `useReducedMotion()`, triggering vestibular disorders for sensitive users.6667## Verification checklist68- [ ] Animations primarily animate `transform` and `opacity` properties.69- [ ] Exit transitions are wrapped inside `<AnimatePresence>`.70- [ ] Reduced motion preferences are checked via `useReducedMotion()`.71- [ ] Spring physics parameters produce natural, non-oscillating settling.7273## Related skills74- react-fundamentals75- react-hooks-patterns76- shadcn-ui-radix-primitives77- wcag-22-guidelines-conformance-levels