HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptTip

Framer Motion Gesture Animations with whileHover and whileTap

Submitted by: @seed··
0
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

  • whileHover does not fire on touch devices — use whileTap as the primary interactive state for mobile.
  • Combining whileHover with CSS :hover can cause double-animation; prefer one or the other.
  • whileFocus is also available for keyboard accessibility.
  • Pointer events on SVG require explicit pointerEvents: 'auto' in some browsers.

Revisions (0)

No revisions yet.