Framer Motion
Framer Motion
Framer Motion is an animation library for React that makes complex UI animations simple with declarative syntax and powerful gesture support. It features physics-based spring animations and layout transitions.
Framer Motion
Framer Motion is an animation library for React that makes complex UI animations simple with declarative syntax and powerful gesture support. It features physics-based spring animations and layout transitions.
Framer Motion is a React animation library derived from the Framer design tool, and since its initial release in 2019 it has become the most popular motion library in the React ecosystem. Through the motion component, animations can be added declaratively to HTML/SVG elements, and it provides a far more intuitive API than CSS transition or keyframes.
Its key feature is physics-based spring animation. Instead of duration, it uses the stiffness, damping, and mass parameters to implement natural, elastic movement. Because this is based on real physics simulation, it feels more natural and responsive than duration-based easing.
The variants system is a powerful pattern for managing complex component animations. State changes in a parent element are automatically propagated to child elements, making it easy to orchestrate hierarchical animations. Sequential animations can also be implemented simply with the staggerChildren and delayChildren options.
The AnimatePresence component solves exit animations, the most difficult problem in React. Exit animations such as fade-out and slide-out can also be applied to components that are removed from the DOM through conditional rendering. Using the layout prop, changes in an element's position/size are also transitioned smoothly and automatically.
import { motion, AnimatePresence } from 'framer-motion';
// κΈ°λ³Έ μ λλ©μ΄μ
function FadeInCard() {
return (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
className="card"
>
μΉ΄λ λ΄μ©
</motion.div>
);
}
// μ€νλ§ μ λλ©μ΄μ
with hover/tap
function SpringButton() {
return (
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: 'spring', stiffness: 400, damping: 17 }}
>
ν΄λ¦νμΈμ
</motion.button>
);
}
// Variantsλ₯Ό μ¬μ©ν 리μ€νΈ μ λλ©μ΄μ
const containerVariants = {
hidden: { opacity: 0 },
visible: {
opacity: 1,
transition: {
staggerChildren: 0.1,
delayChildren: 0.2,
},
},
};
const itemVariants = {
hidden: { opacity: 0, x: -20 },
visible: { opacity: 1, x: 0 },
};
function AnimatedList({ items }) {
return (
<motion.ul
variants={containerVariants}
initial="hidden"
animate="visible"
>
{items.map((item) => (
<motion.li key={item.id} variants={itemVariants}>
{item.name}
</motion.li>
))}
</motion.ul>
);
}
// AnimatePresenceλ‘ exit μ λλ©μ΄μ
function Modal({ isOpen, onClose }) {
return (
<AnimatePresence>
{isOpen && (
<motion.div
className="modal-overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={onClose}
>
<motion.div
className="modal-content"
initial={{ scale: 0.8, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.8, opacity: 0 }}
onClick={(e) => e.stopPropagation()}
>
λͺ¨λ¬ λ΄μ©
</motion.div>
</motion.div>
)}
</AnimatePresence>
);
}
// Layout μ λλ©μ΄μ
function ExpandableCard({ isExpanded }) {
return (
<motion.div
layout
className={isExpanded ? 'card-expanded' : 'card-collapsed'}
transition={{ layout: { duration: 0.3 } }}
>
<motion.h2 layout="position">μ λͺ©</motion.h2>
{isExpanded && <p>νμ₯λ λ΄μ©...</p>}
</motion.div>
);
}