Habilidad de Framer Motion
Animaciones listas para producción en aplicaciones React.
Inicio Rápido
Instalación
npm install framer-motion
pnpm add framer-motion
Uso Básico
import { motion } from "framer-motion";
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.5 }}
>
Contenido
</motion.div>
Conceptos Principales
Ejemplos
Plantillas
Referencia Rápida
Animación Básica
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
transition={{ duration: 0.3 }}
>
Contenido
</motion.div>
Hover y Tap
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: "spring", stiffness: 400, damping: 17 }}
>
Haz clic en mí
</motion.button>
Variantes
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1 }
}
};
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>
AnimatePresence (Animaciones de Salida)
import { AnimatePresence, motion } from "framer-motion";
<AnimatePresence mode="wait">
{isVisible && (
<motion.div
key="modal"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
Contenido del modal
</motion.div>
)}
</AnimatePresence>
Disparador por Scroll
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.5 }}
>
Se anima al hacer scroll hasta la vista
</motion.div>
Arrastre
<motion.div
drag
dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }}
dragElastic={0.1}
>
Arrástrame
</motion.div>
Animación de Layout
<motion.div layout layoutId="shared-element">
Contenido que se anima cuando el layout cambia
</motion.div>
Tipos de Transición
transition={{ duration: 0.3, ease: "easeOut" }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
transition={{ type: "spring", bounce: 0.25 }}
transition={{ type: "inertia", velocity: 50 }}
Funciones de Easing
ease: "linear"
ease: "easeIn"
ease: "easeOut"
ease: "easeInOut"
ease: "circIn"
ease: "circOut"
ease: "circInOut"
ease: "backIn"
ease: "backOut"
ease: "backInOut"
ease: [0.17, 0.67, 0.83, 0.67]
Movimiento Reducido
Respeta siempre las preferencias del usuario:
import { motion, useReducedMotion } from "framer-motion";
function Component() {
const prefersReducedMotion = useReducedMotion();
return (
<motion.div
initial={{ opacity: 0, y: prefersReducedMotion ? 0 : 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: prefersReducedMotion ? 0 : 0.3 }}
>
Respeta las preferencias de movimiento
</motion.div>
);
}
const variants = {
initial: { opacity: 0 },
animate: { opacity: 1 },
};
<motion.div
variants={variants}
initial="initial"
animate="animate"
className="motion-reduce:transition-none"
>
Patrones Comunes
Aparición desde Abajo
const fadeInUp = {
initial: { opacity: 0, y: 20 },
animate: { opacity: 1, y: 0 },
transition: { duration: 0.4 }
};
<motion.div {...fadeInUp}>Contenido</motion.div>
Lista Escalonada
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1, delayChildren: 0.2 }
}
};
const item = {
hidden: { opacity: 0, x: -20 },
show: { opacity: 1, x: 0 }
};
Modal
<AnimatePresence>
{isOpen && (
<>
{/* Fondo */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 bg-black/50"
onClick={onClose}
/>
{/* Modal */}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
className="fixed inset-x-4 top-1/2 -translate-y-1/2 ..."
>
Contenido del modal
</motion.div>
</>
)}
</AnimatePresence>
Acordeón
<motion.div
initial={false}
animate={{ height: isOpen ? "auto" : 0 }}
transition={{ duration: 0.3, ease: "easeInOut" }}
className="overflow-hidden"
>
<div className="p-4">Contenido del acordeón</div>
</motion.div>
Buenas Prácticas
- Usa variantes: Código más limpio y orquestación más sencilla
- Respeta el movimiento reducido: Siempre verifica
useReducedMotion
- Usa
layout con moderación: Puede ser costoso, úsalo solo cuando sea necesario
- Animaciones de salida: Envuelve con
AnimatePresence
- Spring para interacciones: Sensación más natural para hover/tap
- Tween para transiciones de página: Tiempo más predecible
- Propiedades aceleradas por GPU: Prefiere
opacity, scale, x, y sobre width, height