Was macht Motion (Framer Motion) — React UI-Animationsbibliothek?
Deklarative Animationen für React. Federn, Gesten, Layout-Animationen und Seitenübergänge. GPU-beschleunigt, produktionsreif.
Übersicht
- Repository: github.com/motiondivision/motion (26K+ Sterne)
- Sprache: TypeScript / React
- Lizenz: MIT
- Installation:
npm install motion - Dokumentation: motion.dev
- Hinweis: Framer Motion wurde zu Motion umbenannt. Importiere von
motion/react.
Kern-API
Einfache Animation
import { motion } from "motion/react";
// Beim Einhängen animieren
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
Hallo Welt
</motion.div>
// Beim Aushängen animieren (erfordert AnimatePresence)
import { AnimatePresence } from "motion/react";
<AnimatePresence>
{isVisible && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
Inhalt
</motion.div>
)}
</AnimatePresence>
Feder-Physik (Spring Physics)
<motion.div
animate={{ x: 100 }}
transition={{
type: "spring",
stiffness: 300,
damping: 20,
mass: 1,
}}
/>
// Voreinstellungen
transition={{ type: "spring", bounce: 0.5 }} // 0 = kein Federn, 1 = sehr federnd
Varianten (Orchestrierte Animationen)
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: {
staggerChildren: 0.1, // Verzögerung zwischen Kindern
delayChildren: 0.3, // Anfangsverzögerung
},
},
};
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>
Gesten
// Hover
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
>
Klick mich
</motion.button>
// Drag (Ziehen)
<motion.div
drag // Drag auf beiden Achsen aktivieren
dragConstraints={{ left: -100, right: 100, top: -50, bottom: 50 }}
dragElastic={0.2}
/>
Layout-Animationen
// Layout-Änderungen automatisch animieren
<motion.div layout>
{isExpanded ? <ExpandedContent /> : <CollapsedContent />}
</motion.div>
// Gemeinsame Layout-Animation (zwischen Elementen morphen)
<motion.div layoutId="card">
{/* Dieses Element morpht zwischen Positionen, wenn layoutId übereinstimmt */}
</motion.div>
Scroll-Animationen
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 }}>
Parallax-Inhalt
</motion.div>
);
}
useAnimate (Imperativ)
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}>Klick mich</div>
</div>
);
}
Keyframes
<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 }}
/>
Übergangstypen
// Spring (Standard für physikalische Werte)
transition={{ type: "spring", stiffness: 100, damping: 10 }}
// Tween (dauerbasiert)
transition={{ type: "tween", duration: 0.5, ease: "easeInOut" }}
// Easing-Optionen
ease: "linear" | "easeIn" | "easeOut" | "easeInOut"
ease: [0.42, 0, 0.58, 1] // Benutzerdefinierte kubische Bézier-Kurve
// Wiederholung
transition={{ repeat: Infinity, repeatType: "reverse", duration: 1 }}
// Verzögerung
transition={{ delay: 0.5 }}
Häufige Muster
Seitenübergang
// In Layout-Komponente
<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>
Gestaffelte Liste
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 } },
};
Karte mit Vergrößerung bei Hover
<motion.div
whileHover={{ scale: 1.03, boxShadow: "0 10px 30px rgba(0,0,0,0.15)" }}
transition={{ type: "spring", stiffness: 400, damping: 25 }}
/>
Best Practices
- Varianten verwenden für orchestrierte Eltern-Kind-Animationen
- Feder-Physik für natürlich wirkende Bewegungen (lineares Easing vermeiden)
- layout-Prop für automatische Layout-Änderungsanimationen
- AnimatePresence für Exit-Animationen erforderlich
- layoutId für geteilte Elementübergänge zwischen Routen
- useScroll für scroll-gebundene Animationen (Parallax, Fortschrittsbalken)
- whileHover/whileTap für interaktives Feedback
- Staffelung der Kinder über die variant
transition.staggerChildren - Animationen subtil halten — 0,2-0,5s Dauer für UI-Interaktionen
- GPU-beschleunigt — Motion verwendet standardmäßig Transformationen
Wann Motion vs. GSAP verwenden
| Motion | GSAP | |
|---|---|---|
| Framework | React-first | Framework-unabhängig |
| Stil | Deklarativ (JSX-Props) | Imperativ (Funktionsaufrufe) |
| Layout-Animationen | Integriert (magisch) | Manuell |
| Scroll | useScroll-Hook | ScrollTrigger-Plugin |
| Gesten | Integriert (Drag, Hover, Tap) | Draggable-Plugin |
| SVG-Morphing | Eingeschränkt | MorphSVGPlugin |
| Bundle-Größe | ~30KB | ~25KB Kern |
Quelle
- Repository: github.com/motiondivision/motion
- Dokumentation: motion.dev
- Beispiele: motion.dev/docs/react-quick-start