Framer Motion 스킬은(는) 무엇을 하나요?
React 애플리케이션을 위한 프로덕션 준비 애니메이션.
빠른 시작
설치
npm install framer-motion
pnpm add framer-motion
기본 사용법
import { motion } from "framer-motion";
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.5 }}
>
콘텐츠
</motion.div>
핵심 개념
예제
템플릿
빠른 참조
기본 애니메이션
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
transition={{ duration: 0.3 }}
>
콘텐츠
</motion.div>
호버 및 탭
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: "spring", stiffness: 400, damping: 17 }}
>
클릭하세요
</motion.button>
Variants
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1 }
}
};
const item = {
hidden: { opacity: 0, y: 20 },
show: { opacity: 1, y: 0 }
};
<motion.ul variants={container} initial="hidden" animate="show">
{items.map(i => (
<motion.li key={i} variants={item}>{i}</motion.li>
))}
</motion.ul>
AnimatePresence (종료 애니메이션)
import { AnimatePresence, motion } from "framer-motion";
<AnimatePresence mode="wait">
{isVisible && (
<motion.div
key="modal"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
모달 콘텐츠
</motion.div>
)}
</AnimatePresence>
스크롤 트리거
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.5 }}
>
스크롤되어 보일 때 애니메이션 실행
</motion.div>
드래그
<motion.div
drag
dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }}
dragElastic={0.1}
>
드래그하세요
</motion.div>
레이아웃 애니메이션
<motion.div layout layoutId="shared-element">
레이아웃이 변경될 때 애니메이션되는 콘텐츠
</motion.div>
전환 유형
transition={{ duration: 0.3, ease: "easeOut" }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
transition={{ type: "spring", bounce: 0.25 }}
transition={{ type: "inertia", velocity: 50 }}
이징 함수
ease: "linear"
ease: "easeIn"
ease: "easeOut"
ease: "easeInOut"
ease: "circIn"
ease: "circOut"
ease: "circInOut"
ease: "backIn"
ease: "backOut"
ease: "backInOut"
ease: [0.17, 0.67, 0.83, 0.67]
축소된 움직임 (Reduced Motion)
사용자 설정을 항상 존중하세요:
import { motion, useReducedMotion } from "framer-motion";
function Component() {
const prefersReducedMotion = useReducedMotion();
return (
<motion.div
initial={{ opacity: 0, y: prefersReducedMotion ? 0 : 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: prefersReducedMotion ? 0 : 0.3 }}
>
모션 선호도 존중
</motion.div>
);
}
const variants = {
initial: { opacity: 0 },
animate: { opacity: 1 },
};
<motion.div
variants={variants}
initial="initial"
animate="animate"
className="motion-reduce:transition-none"
>
일반 패턴
페이드 인 업
const fadeInUp = {
initial: { opacity: 0, y: 20 },
animate: { opacity: 1, y: 0 },
transition: { duration: 0.4 }
};
<motion.div {...fadeInUp}>콘텐츠</motion.div>
스태거드 리스트
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1, delayChildren: 0.2 }
}
};
const item = {
hidden: { opacity: 0, x: -20 },
show: { opacity: 1, x: 0 }
};
모달
<AnimatePresence>
{isOpen && (
<>
{/* 배경 */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 bg-black/50"
onClick={onClose}
/>
{/* 모달 */}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
className="fixed inset-x-4 top-1/2 -translate-y-1/2 ..."
>
모달 콘텐츠
</motion.div>
</>
)}
</AnimatePresence>
아코디언
<motion.div
initial={false}
animate={{ height: isOpen ? "auto" : 0 }}
transition={{ duration: 0.3, ease: "easeInOut" }}
className="overflow-hidden"
>
<div className="p-4">아코디언 콘텐츠</div>
</motion.div>
모범 사례
- variants 사용: 코드가 깔끔해지고 오케스트레이션이 쉬워집니다.
- 축소된 움직임 존중: 항상
useReducedMotion을 확인하세요.
layout은 필요한 경우에만 사용: 성능 비용이 들 수 있으므로 꼭 필요할 때만 사용하세요.
- 종료 애니메이션:
AnimatePresence로 감싸세요.
- 인터랙션에는 Spring 사용: 호버/탭에 더 자연스러운 느낌을 줍니다.
- 페이지 전환에는 Tween 사용: 더 예측 가능한 타이밍을 제공합니다.
- GPU 가속 속성:
width, height보다 opacity, scale, x, y를 선호하세요.