Using gestures in Motion
Gestures are an important aspect of user interfaces. They are what make elements feel alive. The user performs an action and the interface responds appropriately. This is very well known from the physical world.
While using computers we hover over buttons, tap links, drag cards and more. Motion (opens in a new tab) extends React’s basic event system (opens in a new tab) with a simple set of gestures that make them easy to animate.
Gesture types
To use gestures from Motion, we have to use the motion component. It supports hover, tap, pan, drag, focus and inView gestures.
Each of these gestures can trigger animations through a while- prop or be handled manually with event listeners. pan is an exception, as it doesn’t have a while- prop.
Hover
Hover is the most obvious gesture and also one of the most used ones. whileHover detects when a pointer enters or leaves the element.
Hover over the rectangle
The great part about Motion is that you can easily use spring animations for any of these gestures, which is quite challenging in CSS.
<motion.div
whileHover={{ rotate: 180 }}
transition={{ type: "spring", duration: 0.8, bounce: 0 }}
/>For simple effects like these, both CSS and Motion work well. I prefer Motion because I can use springs, which creates a more natural-looking motion.
Hover over the buttons to see the difference between CSS and Motion
Performance is also worth keeping in mind, but for effects that involve transform, clipPath or filter, CSS and Motion behave almost identically.
Even though Motion uses JavaScript, these animations still run on the compositor thread, just like CSS.
In most cases, there’s no meaningful performance difference. Sometimes Motion’s performance is even better, because it handles independent transforms more efficiently than CSS variables.
Tap
The whileTap prop fires when a pointer or a keyboard key is pressed and released on the same element.
Tap or click the rectangle
Tap automatically cancels if the pointer moves too far, so it won’t conflict with dragging.
<motion.div
whileTap={{ scale: 0.8 }}
transition={{ type: "spring", duration: 0.5, bounce: 0 }}
/>It’s also keyboard accessible. Pressing the return key will trigger onTapStart and whileTap. Releasing it fires onTap.
Click on the buttons to see the difference between CSS and Motion
Drag
The drag gesture applies pointer movement directly to the component. Motion provides a lot of controls out of the box. To make a component draggable, you can simply add the drag prop to the motion element.
Drag the rectangle around
You can then add whileDrag and the property that you want to animate. In this example, I also added dragConstraints, so the element can’t be dragged outside of its container.
<motion.div
whileDrag={{ scale: 0.8 }}
drag
dragConstraints={constraintsRef}
transition={{ type: "spring", duration: 0.5, bounce: 0 }}
/>The drag gesture is very flexible and can be configured in many different ways. The Motion docs (opens in a new tab) cover this more in depth.
Drag items to the left or right to see the interaction.
If you want to understand how I built this component, I recently wrote an article about drag gestures on the web, where I talk about it in depth.
Pan
The pan gesture recognizes when a pointer moves more than 3 pixels while pressed and ends when it’s released.
Click and hold on the rectangle and then drag your cursor away while holding
Pan doesn’t have an associated while- prop, so you’ll need to use event handlers like onPan, onPanStart and onPanEnd.
<motion.div
onPanStart={() => setIsPanning(true)}
onPanEnd={() => setIsPanning(false)}
animate={{ scale: isPanning ? 0.9 : 1 }}
/>It’s one of the less common gestures, but it’s still worth mentioning. It’s great for creating interactive components like sliders or scrollable areas.
Focus
The whileFocus prop fires when an element gains or loses focus. It follows the same logic as :focus-visible in CSS.
Click on the container and press the Tab key to focus the rectangle
It adds subtle visual feedback for keyboard navigation or accessibility flows.
<motion.button
whileFocus={{ scale: 1.1 }}
transition={{ type: "spring", duration: 0.3, bounce: 0 }}
/>It’s great for buttons, input fields and other elements that you want to give a clear visual response to when they are focused.
Click on the container and press the Tab key to focus and cycle over the buttons
Since you can animate almost any value, you can get very creative with implementing even more complex effects.
Click on the container and press the Tab key to focus the input
I’d recommend keeping focus animations subtle though. Complex motion can feel distracting or even disorienting when navigating by keyboard.
In view
whileInView triggers when an element enters or exits the viewport.
Scroll down to see the rectangle enter the viewport
It is great for scroll-based reveals or entrances. You can pair it with the initial prop to make a component animate from and to a specific state.
<motion.div
initial={{ filter: "blur(8px)" }}
whileInView={{ scale: 1.1, rotate: 45, filter: "blur(0px)" }}
viewport={{ root: scrollRef, amount: 0.5 }}
transition={{ type: "spring", duration: 0.6, bounce: 0 }}
/>In this example, the unrevealed blur value is 8px. Once the component enters the viewport, it animates to 0px.
Use the buttons to navigate between cards and see the shapes animate
Conclusion
A lot of the things covered in this article and more can be found in the Motion Documentation (opens in a new tab).
This article is by no means supposed to recite things that are written in the documentation, but instead point out that this is something you can do with Motion and show how I use these gestures in my work.
Thanks to Matt Perry (opens in a new tab) for providing feedback on this article.
More
If you enjoy articles like this and want to learn more, take a look at Interfaces, my design engineering magazine.
It’s where I share everything I know about building great interfaces. From animation and typography to performance and accessibility.