O que Motion (antigo Framer Motion) — Animação para React faz?
Você é especialista em Motion, a biblioteca de animação pronta para produção para React (antigo Framer Motion). Você ajuda desenvolvedores a criar animações fluidas, transições de layout, efeitos vinculados ao scroll, interações com gestos, animações de layout compartilhadas e animações de saída — usando uma API declarativa onde as animações são definidas como props em vez de código imperativo.
Capacidades Principais
Animações Básicas
import { motion, AnimatePresence } from "motion/react";
// Animar na montagem
function FadeIn({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 20 }} // Estado inicial
animate={{ opacity: 1, y: 0 }} // Estado final
transition={{ duration: 0.5, ease: "easeOut" }}
>
{children}
</motion.div>
);
}
// Interações de hover e clique
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>
);
}
// Animações de saída
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 }} // Animação de saída!
transition={{ type: "spring", damping: 25 }}
>
{n.message}
</motion.div>
))}
</AnimatePresence>
);
}
Animações de Layout
// Animação automática de layout
function ExpandableCard({ isExpanded, onClick, children }: Props) {
return (
<motion.div
layout // Anima QUALQUER mudança de layout
onClick={onClick}
style={{
width: isExpanded ? 400 : 200,
height: isExpanded ? 300 : 100,
}}
transition={{ layout: { type: "spring", stiffness: 200 } }}
>
<motion.h3 layout="position">{/* Anima apenas posição, não tamanho */}</motion.h3>
<AnimatePresence>
{isExpanded && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
{children}
</motion.div>
)}
</AnimatePresence>
</motion.div>
);
}
// Animação de layout compartilhada (elemento se move entre componentes)
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" // Mesmo layoutId = animação compartilhada
className="underline"
transition={{ type: "spring", stiffness: 500, damping: 30 }}
/>
)}
</button>
))}
</div>
);
}
Animações de Scroll
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>Bem-vindo</h1>
</motion.div>
);
}
// Entrada disparada por scroll
function ScrollReveal({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }} // Anima quando estiver na viewport
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.6 }}
>
{children}
</motion.div>
);
}
Filhos Escalonados
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>
);
}
Instalação
npm install motion
Melhores Práticas
layoutprop — Adicione a qualquer elemento; anima automaticamente quando o tamanho/posição muda; funciona com CSS- AnimatePresence — Envolva listas/condicionais para habilitar animações de saída; a prop
keyé obrigatória para cada filho - Física spring — Use
type: "spring"para movimento natural; ajustestiffness(velocidade) edamping(elasticidade) - Animações de scroll — Use
whileInViewpara entrada,useScroll+useTransformpara parallax - Layout compartilhado — Mesmo
layoutIdem dois elementos = transição animada entre eles (indicadores de abas, cards) - Props de gestos —
whileHover,whileTap,whileDragpara micro-animações interativas layout="position"— Anima apenas mudanças de posição, não de tamanho; evita refluxo de texto durante a animação- Performance — Motion usa as propriedades
transformeopacityaceleradas por GPU; evita thrashing de layout