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>
バリアント
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>
ベストプラクティス
- バリアントを使用する: コードが整理され、オーケストレーションが容易になります
- 動きの軽減設定を尊重する: 常に
useReducedMotion をチェックすること
layout は控えめに使用する: コストがかかる可能性があるため、必要なときだけ使う
- 終了アニメーション:
AnimatePresence でラップする
- インタラクションには Spring: ホバーやタップに自然な感触を
- ページトランジションには Tween: タイミングがより予測可能
- GPU アクセラレーションが効くプロパティ:
width, height よりも opacity, scale, x, y を優先する