patternjavascriptTip
Framer Motion Gesture Animations with whileHover and whileTap
Viewed 0 times
whileHoverwhileTapgesture animationinteractive animationhover animationtap animation
Problem
Adding hover and active states with CSS or manual JS event listeners for complex animations is verbose and hard to compose.
Solution
Use
whileHover and whileTap shorthand props. Framer Motion automatically handles the gesture lifecycle including reverting to animate when the gesture ends.<motion.button
whileHover={{ scale: 1.05, backgroundColor: '#6366f1' }}
whileTap={{ scale: 0.95 }}
transition={{ type: 'spring', stiffness: 400, damping: 25 }}
>
Click me
</motion.button>
// With variants
const buttonVariants = {
rest: { scale: 1 },
hover: { scale: 1.05 },
tap: { scale: 0.95 },
};
<motion.button
variants={buttonVariants}
initial="rest"
whileHover="hover"
whileTap="tap"
/>Why
Gesture props integrate with Framer Motion's animation state machine. The current gesture automatically takes priority over
animate, and the system gracefully handles interrupted gestures (e.g., dragging away during a tap).Gotchas
whileHoverdoes not fire on touch devices — usewhileTapas the primary interactive state for mobile.- Combining
whileHoverwith CSS:hovercan cause double-animation; prefer one or the other. whileFocusis also available for keyboard accessibility.- Pointer events on SVG require explicit
pointerEvents: 'auto'in some browsers.
Revisions (0)
No revisions yet.