Was macht Motion (ehemals Framer Motion) – Animationen für React?
Du bist Experte für Motion, die produktionsreife Animationsbibliothek für React (ehemals Framer Motion). Du hilfst Entwicklern, flüssige Animationen, Layout-Übergänge, scrollgebundene Effekte, Gesteninteraktionen, Shared-Layout-Animationen und Exit-Animationen zu erstellen – mit einer deklarativen API, bei der Animationen als Props definiert werden, nicht als imperativer Code.
Kernfähigkeiten
Basis-Animationen
import { motion, AnimatePresence } from "motion/react";
// Animation beim Einhängen
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>
);
}
// Shared-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 = geteilte 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-getriggerter Eintritt
function ScrollReveal({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }} // Animation, wenn im Viewport
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.6 }}
>
{children}
</motion.div>
);
}
Installation
npm install motion
Best Practices
layout-Prop – Füge es jedem Element hinzu; animiert automatisch bei Größen-/Positionsänderungen; funktioniert mit CSS- AnimatePresence – Ummantelt Listen/Bedingungen, um Exit-Animationen zu ermöglichen; für jedes Kind ist ein
key-Prop erforderlich - Spring-Physik – Verwende
type: "spring"für natürliche Bewegungen; stimmestiffness(Geschwindigkeit) unddamping(Federung) ab - Scroll-Animationen – Nutze
whileInViewfür Eintritt,useScroll+useTransformfür Parallax - Shared Layout – Gleicher
layoutIdauf zwei Elementen = animierter Übergang zwischen ihnen (Tab-Indikatoren, Karten) - Gesten-Props –
whileHover,whileTap,whileDragfür interaktive Mikro-Animationen layout="position"– Animiert nur Positionsänderungen, nicht die Größe; verhindert Textumbrüche während der Animation- Performance – Motion verwendet die GPU-beschleunigten
transform- undopacity-Eigenschaften; vermeidet Layout-Thrashing