Gestures: Step-by-step tutorial

Matt Perry
In this tutorial, we're going to build the Gestures example step-by-step.
This tutorial is rated 1/5 difficulty, which means we'll spend some time explaining the Motion APIs that we've chosen to use (and why), and also any browser APIs we encounter that might be unfamiliar to beginners.
Here's a live demo of the example we're going to be creating:
Introduction
The Gestures example shows how to animate an element in response to user interactions like hovering and tapping. It uses the whileHover
and whileTap
props from Motion for React to create intuitive interactive animations.
Motion's whileHover
and whileTap
functions provide a couple of key advantages over traditional event listeners, beyond the power of Motion's spring animations and independent transform animations:
Pointer filtering
Normal pointer events fire for all pointers, whereas whileHover
will filter out touch events and whileTap
will filter out secondary pointers (right-click, secondary touch, etc.).
Keyboard accessibility
When whileTap
is attached to an element, it will automatically become keyboard accessible. This means it can receive focus, and be pressed via the enter key. This ensures that keyboard users get the same great experience as pointer users.
Get started
Let's start by creating a simple colored box without any animations:
This creates a green square on the page, but it doesn't respond to any user interactions yet.
Let's animate!
Import from Motion
To add gesture animations to our box, we first need to import the motion
component from Motion for React:
Basic functionality
Now we can replace the standard div
with a motion.div
:
To create gesture animations, we use the whileHover
and whileTap
props. These define values to animate to while the gestures are active.
With these props:
whileHover
: When the user hovers over the box, it will scale up to 1.2 times its original sizewhileTap
: When the user clicks or taps the box, it will scale down to 0.8 times its original size
These animations happen automatically when the respective gestures are detected, and they smoothly reverse when the gesture ends.
Customizing gesture animations
You can customize gesture animations further by adding more properties or providing a transition
prop:
This version adds rotation and color changes to the gesture animations, and uses a default spring transition for more natural movement. whileHover
receives a specific transition
that will trigger when the gesture starts.
Motion has several other convenient props for gesture animations, like whileDrag
, whileInView
, and whileFocus
.
Conclusion
In this tutorial, we learned how to:
Create hover animations with the
whileHover
propCreate tap/click animations with the
whileTap
propDefine animation states using JavaScript objects
Combine multiple animation properties for richer interactions
Motion for React makes it easy to add engaging, interactive animations to your UI elements, creating a more dynamic and responsive user experience with minimal code.