patternjavascriptTip
Framer Motion Basic Animation with motion Components
Viewed 0 times
framer motionmotion divinitial animatedeclarative animationvariantsreact animation
Problem
You need declarative enter animations in a React component without managing CSS classes or JS state for animation triggers.
Solution
Replace HTML elements with their
For reusable variants, extract to a
motion.* counterparts and use initial, animate, and transition props to drive animations declaratively.import { motion } from 'framer-motion';
function Card() {
return (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.4, ease: 'easeOut' }}
>
Hello
</motion.div>
);
}For reusable variants, extract to a
variants object:const cardVariants = {
hidden: { opacity: 0, y: 20 },
visible: { opacity: 1, y: 0 },
};
<motion.div variants={cardVariants} initial="hidden" animate="visible" />Why
Framer Motion tracks animation state internally, so you never imperatively start or cancel animations. The declarative model makes animation intent clear at the component level.
Gotchas
- motion.* components add a small runtime cost — do not wrap every DOM element if only a few animate.
initial={false}skips the enter animation on first render, useful for page load when content should appear instantly.- Framer Motion infers units:
x: 100means 100px, but mixing units with CSS calc strings requiresuseTransform.
Revisions (0)
No revisions yet.