🌐 Web Development

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.

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

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>
  );
}

πŸ—£οΈ Examples of Practical Conversations

πŸ‘¨β€πŸŽ¨ UX Designer
"I'd like these list items to fade in sequentially while rising up slightly. And when deleting, I want them to slide off to the side and disappear."
πŸ‘¨β€πŸ’» Frontend Developer
"That's quick with Framer Motion. Set staggerChildren with variants and you get a sequential animation, and add an exit animation with AnimatePresence. If you also add the layout prop, the remaining items rearrange themselves naturally after a deletion."
πŸ‘©β€πŸ’» Tech Lead
"I'm worried about the bundle size of the animation library. Is Framer Motion okay?"
πŸ‘¨β€πŸ’» Frontend Developer
"The base bundle is about 40KB gzip, but tree-shaking works well, so it actually adds less. There's also a lightweight version, motion/react. For a UI with a lot of interactions like ours, it's well worth it. With CSS alone, exit animations and layout transitions get too complicated."

⚠️ Cautions

πŸ”— Related Terms

πŸ“š Learn More