Motion(Framer Motion) — React UIアニメーションライブラリ
React向けの宣言的アニメーション。スプリング、ジェスチャー、レイアウトアニメーション、ページトランジション。GPUアクセラレーション対応、プロダクション品質。
概要
- リポジトリ: github.com/motiondivision/motion(26K以上のスター)
- 言語: TypeScript / React
- ライセンス: MIT
- インストール:
npm install motion - ドキュメント: motion.dev
- 注意: Framer MotionはMotionに名称変更されました。
motion/reactからインポートします。
コアAPI
基本アニメーション
import { motion } from "motion/react";
// マウント時にアニメーション
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
Hello World
</motion.div>
// アンマウント時にアニメーション(AnimatePresenceが必要)
import { AnimatePresence } from "motion/react";
<AnimatePresence>
{isVisible && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
コンテンツ
</motion.div>
)}
</AnimatePresence>
スプリング物理
<motion.div
animate={{ x: 100 }}
transition={{
type: "spring",
stiffness: 300,
damping: 20,
mass: 1,
}}
/>
// プリセット
transition={{ type: "spring", bounce: 0.5 }} // 0 = バウンドなし、1 = 非常にバウンドあり
バリアント(オーケストレーションアニメーション)
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: {
staggerChildren: 0.1, // 子要素間の遅延
delayChildren: 0.3, // 初期遅延
},
},
};
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>
ジェスチャー
// ホバー
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
>
クリックしてね
</motion.button>
// ドラッグ
<motion.div
drag // 両軸のドラッグを有効化
dragConstraints={{ left: -100, right: 100, top: -50, bottom: 50 }}
dragElastic={0.2}
/>
レイアウトアニメーション
// レイアウト変更を自動的にアニメーション
<motion.div layout>
{isExpanded ? <ExpandedContent /> : <CollapsedContent />}
</motion.div>
// 共有レイアウトアニメーション(要素間でモーフィング)
<motion.div layoutId="card">
{/* この要素はlayoutIdが一致すると位置がモーフィングします */}
</motion.div>
スクロールアニメーション
import { useScroll, useTransform, motion } from "motion/react";
function ParallaxSection() {
const { scrollYProgress } = useScroll();
const y = useTransform(scrollYProgress, [0, 1], [0, -200]);
const opacity = useTransform(scrollYProgress, [0, 0.5, 1], [1, 1, 0]);
return (
<motion.div style={{ y, opacity }}>
パララックスコンテンツ
</motion.div>
);
}
useAnimate(命令的制御)
import { useAnimate } from "motion/react";
function MyComponent() {
const [scope, animate] = useAnimate();
async function handleClick() {
await animate(".box", { scale: 1.5 }, { duration: 0.3 });
await animate(".box", { rotate: 360 }, { duration: 0.5 });
await animate(".box", { scale: 1, rotate: 0 });
}
return (
<div ref={scope}>
<div className="box" onClick={handleClick}>クリックしてね</div>
</div>
);
}
キーフレーム
<motion.div
animate={{
x: [0, 100, 100, 0, 0],
y: [0, 0, 100, 100, 0],
rotate: [0, 90, 180, 270, 360],
}}
transition={{ duration: 2, repeat: Infinity }}
/>
トランジションタイプ
// スプリング(物理的な値のデフォルト)
transition={{ type: "spring", stiffness: 100, damping: 10 }}
// トゥイーン(持続時間ベース)
transition={{ type: "tween", duration: 0.5, ease: "easeInOut" }}
// イージングオプション
ease: "linear" | "easeIn" | "easeOut" | "easeInOut"
ease: [0.42, 0, 0.58, 1] // カスタム3次ベジェ
// 繰り返し
transition={{ repeat: Infinity, repeatType: "reverse", duration: 1 }}
// 遅延
transition={{ delay: 0.5 }}
共通パターン
ページトランジション
// レイアウトコンポーネント内
<AnimatePresence mode="wait">
<motion.div
key={pathname}
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: -20 }}
transition={{ duration: 0.3 }}
>
{children}
</motion.div>
</AnimatePresence>
段階的リスト
const container = {
hidden: {},
show: { transition: { staggerChildren: 0.08 } },
};
const item = {
hidden: { opacity: 0, y: 15 },
show: { opacity: 1, y: 0, transition: { type: "spring", bounce: 0.3 } },
};
ホバー時のカード拡大
<motion.div
whileHover={{ scale: 1.03, boxShadow: "0 10px 30px rgba(0,0,0,0.15)" }}
transition={{ type: "spring", stiffness: 400, damping: 25 }}
/>
ベストプラクティス
- バリアントを使用して親子アニメーションを連携させる
- スプリング物理で自然な動きを実現(線形イージングは避ける)
- layoutプロパティでレイアウト変更を自動アニメーション
- AnimatePresenceはexitアニメーションに必須
- layoutIdでルート間の共有要素トランジションを実現
- useScrollでスクロール連動アニメーション(パララックス、プログレスバー)
- whileHover/whileTapでインタラクティブなフィードバックを提供
- バリアントの
transition.staggerChildrenで子要素を段階的に表示 - アニメーションは控えめに — UIインタラクションでは0.2〜0.5秒の持続時間
- GPUアクセラレーション対応 — Motionはデフォルトでtransformを使用
MotionとGSAPの使い分け
| Motion | GSAP | |
|---|---|---|
| フレームワーク | Reactファースト | フレームワーク非依存 |
| スタイル | 宣言的(JSX props) | 命令的(関数呼び出し) |
| レイアウトアニメーション | 組み込み(マジック) | 手動 |
| スクロール | useScrollフック | ScrollTriggerプラグイン |
| ジェスチャー | 組み込み(ドラッグ、ホバー、タップ) | Draggableプラグイン |
| SVGモーフィング | 制限あり | MorphSVGPlugin |
| バンドルサイズ | 約30KB | 約25KB(コア) |
ソース
- リポジトリ: github.com/motiondivision/motion
- ドキュメント: motion.dev
- サンプル: motion.dev/docs/react-quick-start