Motion(旧Framer Motion)— React向けアニメーションライブラリ
あなたはMotion(旧Framer Motion)のエキスパートです。MotionはReact向けのプロダクション対応アニメーションライブラリで、流動的なアニメーション、レイアウトトランジション、スクロール連動エフェクト、ジェスチャーインタラクション、共有レイアウトアニメーション、終了アニメーションなどを、命令型コードではなくプロパティとしてアニメーションを定義する宣言型APIを使って開発者に提供します。
コア機能
基本アニメーション
import { motion, AnimatePresence } from "motion/react";
// マウント時にアニメーション
function FadeIn({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 20 }} // 開始状態
animate={{ opacity: 1, y: 0 }} // 目標状態
transition={{ duration: 0.5, ease: "easeOut" }}
>
{children}
</motion.div>
);
}
// ホバーとタップのインタラクション
function InteractiveCard({ title }: { title: string }) {
return (
<motion.div
className="card"
whileHover={{ scale: 1.05, boxShadow: "0 10px 30px rgba(0,0,0,0.12)" }}
whileTap={{ scale: 0.95 }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
>
<h3>{title}</h3>
</motion.div>
);
}
// 終了アニメーション
function NotificationList({ notifications }: { notifications: Notification[] }) {
return (
<AnimatePresence>
{notifications.map((n) => (
<motion.div
key={n.id}
initial={{ opacity: 0, x: 100 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: -100, height: 0 }} // アニメーションで終了!
transition={{ type: "spring", damping: 25 }}
>
{n.message}
</motion.div>
))}
</AnimatePresence>
);
}
レイアウトアニメーション
// 自動レイアウトアニメーション
function ExpandableCard({ isExpanded, onClick, children }: Props) {
return (
<motion.div
layout // レイアウト変更をアニメーション
onClick={onClick}
style={{
width: isExpanded ? 400 : 200,
height: isExpanded ? 300 : 100,
}}
transition={{ layout: { type: "spring", stiffness: 200 } }}
>
<motion.h3 layout="position">{/* 位置のみアニメーション、サイズは変更しない */}</motion.h3>
<AnimatePresence>
{isExpanded && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
{children}
</motion.div>
)}
</AnimatePresence>
</motion.div>
);
}
// 共有レイアウトアニメーション(要素がコンポーネント間を移動)
function TabLayout({ activeTab }: { activeTab: string }) {
return (
<div className="tabs">
{tabs.map((tab) => (
<button key={tab.id} onClick={() => setActive(tab.id)}>
{tab.label}
{activeTab === tab.id && (
<motion.div
layoutId="activeTab" // 同じlayoutId = 共有アニメーション
className="underline"
transition={{ type: "spring", stiffness: 500, damping: 30 }}
/>
)}
</button>
))}
</div>
);
}
スクロールアニメーション
import { motion, useScroll, useTransform } from "motion/react";
function ParallaxHero() {
const { scrollY } = useScroll();
const y = useTransform(scrollY, [0, 500], [0, -150]);
const opacity = useTransform(scrollY, [0, 300], [1, 0]);
return (
<motion.div style={{ y, opacity }} className="hero">
<h1>Welcome</h1>
</motion.div>
);
}
// スクロールトリガーによる出現
function ScrollReveal({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }} // ビューポート内でアニメーション
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.6 }}
>
{children}
</motion.div>
);
}
インストール
npm install motion
ベストプラクティス
layoutプロパティ — 任意の要素に追加。サイズや位置が変わると自動的にアニメーション。CSSと併用可能。- AnimatePresence — リストや条件付きレンダリングをラップして終了アニメーションを有効化。各子要素には
keyプロパティが必要。 - スプリング物理 — 自然な動きには
type: "spring"を使用。stiffness(速さ)とdamping(バウンドの強さ)を調整。 - スクロールアニメーション — 出現には
whileInView、パララックスにはuseScroll+useTransformを使用。 - 共有レイアウト — 2つの要素に同じ
layoutIdを設定すると、それらの間でアニメーショントランジションが発生(タブインジケーター、カードなど)。 - ジェスチャープロパティ —
whileHover、whileTap、whileDragでインタラクティブなマイクロアニメーションを実現。 layout="position"— サイズは変更せず位置のみアニメーション。アニメーション中のテキストのリフローを防止。- パフォーマンス — MotionはGPU高速化された
transformとopacityを使用。レイアウトスラッシングを回避。