Gesture-Driven Animations with use-gesture
Combine react-spring with @use-gesture/react to build drag, pinch, and scroll-driven animations.
Gesture-Driven Animations with use-gesture is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
@use-gesture/react Overview
@use-gesture/react is a gesture recognition library designed to work seamlessly with react-spring. It recognizes drag, pinch, scroll, hover, move, and wheel gestures, providing normalized gesture state that you pipe directly into spring setters for fluid, physics-based interactions.
useDrag Gesture State
The useDrag hook fires a callback on every drag event. The callback receives a gesture state object with key properties: movement (total displacement from drag start), velocity (current speed), direction (1 or -1 per axis), first (true on drag start), last (true on drag end), and event (the raw DOM event).
The bind() Function
All use-gesture hooks return a bind() function. Calling bind() returns an object of event handler props (onPointerDown, onTouchStart, etc.) that you spread onto the element you want to make interactive. This is the connection point between the gesture recognizer and your DOM element.
Combining useDrag with react-spring
The standard pattern: create a spring with the imperative API ([springs, api] = useSpring(...)), then use useDrag to call api.start({ x: movement[0], y: movement[1] }) on each drag event. The spring smoothly follows the drag movement, adding natural physics to what would otherwise be rigid cursor tracking.
Snap Back on Release
When a drag ends (last: true in gesture state), you typically snap the element back to its origin. Call api.start({ x: 0, y: 0 }) inside an if (last) check. react-spring animates back with spring physics — a gentle bounce rather than an instant reset — giving the interaction a satisfying physical feel.
Throw and Fling with Velocity
The velocity value in gesture state enables throw behavior. When the user releases with high velocity, you can fling the element in the drag direction instead of snapping back. Check if (last && velocity[0] > threshold) then animate the element to its final off-screen position. This is essential for swipe-to-dismiss interactions.
Drag Boundaries and Clamping
To restrict drag movement within a container, clamp the movement values before passing them to the spring: x: Math.max(-maxX, Math.min(maxX, movement[0])). Alternatively, use use-gesture's built-in bounds option: useDrag(handler, { bounds: { left: -100, right: 100 } }) to automatically clamp gesture state.
Swipe-to-Dismiss Pattern
A mobile swipe-to-dismiss implementation: use useDrag to track horizontal movement. If the user releases with velocity above a threshold OR has moved more than half the screen width, animate the card off-screen (api.start({ x: window.innerWidth })) and then remove it from the list. Otherwise, snap it back to center.
usePinch for Zoom Interactions
usePinch recognizes two-finger pinch gestures on touch screens and Ctrl+wheel on desktop. The gesture state includes scale (current zoom level relative to pinch start) and origin (center point of the pinch). Connect to a spring tracking scale to create smooth, bounded zoom interactions on images or maps.
useHover and useMove
useHover fires when the pointer enters or leaves an element, providing an active boolean. useMove tracks mouse position within an element, providing normalized coordinates. Both are useful for magnetic hover effects where elements subtly shift toward the cursor as it approaches.
Combining Multiple Gestures
use-gesture hooks compose together. You can use both useDrag and usePinch on the same element: const bindDrag = useDrag(...); const bindPinch = usePinch(...), then spread both bind functions: ...bindDrag() ...bindPinch(). use-gesture handles the disambiguation between gesture types automatically.
bind() Function Purpose
What does the bind() function returned by use-gesture hooks do?
Lesson Recap: use-gesture Integration
@use-gesture/react provides useDrag, usePinch, useScroll, useHover, and useMove. Each hook returns a bind() function whose spread result wires gesture events to DOM elements. Gesture state provides movement, velocity, direction, first, and last. Combine with react-spring's imperative api.start() for physics-based interactive animations. Use velocity for fling/dismiss and bounds for movement clamping.
Frequently asked questions
Is the “Gesture-Driven Animations with use-gesture” lesson free?
Yes — the full text of “Gesture-Driven Animations with use-gesture” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Gesture-Driven Animations with use-gesture”?
Combine react-spring with @use-gesture/react to build drag, pinch, and scroll-driven animations. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Gesture-Driven Animations with use-gesture” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- React Spring Core: useSpring Hook
- Animated Components and Style Interpolation
- useTrail and useChain for Sequences
- Gesture-Driven Animations with use-gesture