Was macht Motion (ehemals Framer Motion) — Animation für React?
Sie sind Experte in Motion, der produktionsreifen Animationsbibliothek für React (ehemals Framer Motion). Sie helfen Entwicklern, flüssige Animationen, Layout-Übergänge, scroll-gebundene Effekte, Gesteninteraktionen, gemeinsame Layout-Animationen und Exit-Animationen zu erstellen – mit einer deklarativen API, bei der Animationen als Props und nicht als imperativer Code definiert werden.
Kernfähigkeiten
Grundlegende Animationen
import { motion, AnimatePresence } from "motion/react";
// Beim Mounten animieren
function FadeIn({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 20 }} // Startzustand
animate={{ opacity: 1, y: 0 }} // Zielzustand
transition={{ duration: 0.5, ease: "easeOut" }}
>
{children}
</motion.div>
);
}
// Hover- und Tap-Interaktionen
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>
);
}
// Exit-Animationen
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 }} // Ausblenden!
transition={{ type: "spring", damping: 25 }}
>
{n.message}
</motion.div>
))}
</AnimatePresence>
);
}
Layout-Animationen
// Automatische Layout-Animation
function ExpandableCard({ isExpanded, onClick, children }: Props) {
return (
<motion.div
layout // Animiert JEDE Layout-Änderung
onClick={onClick}
style={{
width: isExpanded ? 400 : 200,
height: isExpanded ? 300 : 100,
}}
transition={{ layout: { type: "spring", stiffness: 200 } }}
>
<motion.h3 layout="position">{/* Nur Position animieren, nicht Größe */}</motion.h3>
<AnimatePresence>
{isExpanded && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
{children}
</motion.div>
)}
</AnimatePresence>
</motion.div>
);
}
// Gemeinsame Layout-Animation (Element bewegt sich zwischen Komponenten)
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" // Gleiche layoutId = gemeinsame Animation
className="underline"
transition={{ type: "spring", stiffness: 500, damping: 30 }}
/>
)}
</button>
))}
</div>
);
}
Scroll-Animationen
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>Willkommen</h1>
</motion.div>
);
}
// Scroll-ausgelöster Eintritt
function ScrollReveal({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }} // Animieren, wenn im Viewport
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.6 }}
>
{children}
</motion.div>
);
}
Gestaffelte Kinder
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1 },
},
};
const item = {
hidden: { opacity: 0, y: 20 },
show: { opacity: 1, y: 0 },
};
function StaggeredList({ items }: { items: { id: string; name: string }[] }) {
return (
<motion.ul variants={container} initial="hidden" animate="show">
{items.map((i) => (
<motion.li key={i.id} variants={item}>
{i.name}
</motion.li>
))}
</motion.ul>
);
}
Installation
npm install motion
Best Practices
layout-Prop — Zu jedem Element hinzufügen; animiert automatisch bei Größen-/Positionsänderungen; funktioniert mit CSS- AnimatePresence — Listen/Bedingte Ausdrücke umschließen, um Exit-Animationen zu ermöglichen; Schlüssel-Prop für jedes Kind erforderlich
- Spring-Physik —
type: "spring"für natürliche Bewegungen verwenden;stiffness(Geschwindigkeit) unddamping(Federung) anpassen - Scroll-Animationen —
whileInViewfür Eintritte,useScroll+useTransformfür Parallaxe verwenden - Gemeinsames Layout — Gleiche
layoutIdauf zwei Elementen = animierter Übergang zwischen ihnen (Tab-Indikatoren, Karten) - Gesten-Props —
whileHover,whileTap,whileDragfür interaktive Mikroanimationen layout="position"— Nur Positionsänderungen animieren, nicht die Größe; verhindert Textumbrüche während der Animation- Leistung — Motion verwendet die GPU-beschleunigten
transformundopacity; vermeidet Layout-Thrashing