Skip to main content

Gestures adapter

@rootnative/inertia-gestures adds drag, swipe, and pan hooks on react-native-gesture-handler. It's an optional sibling package. Install it only when you need more than the pressable states. The core library doesn't depend on react-native-gesture-handler.

The hooks compose with any Motion.* primitive through a <GestureDetector> and one style slot. They don't replace the gesture prop. They add what you can do next to it.

Install​

yarn add @rootnative/inertia-gestures react-native-gesture-handler

Then follow the react-native-gesture-handler install guide. It needs <GestureHandlerRootView> near the root of your app.

useDrag​

Drag a Motion primitive on one or two axes, with optional bounds and an elastic overshoot.

import { GestureDetector } from 'react-native-gesture-handler'
import { Motion } from '@rootnative/inertia'
import { useDrag } from '@rootnative/inertia-gestures'

function DraggableBox() {
const drag = useDrag({
axis: 'both',
constraints: { left: -100, right: 100, top: -60, bottom: 60 },
elastic: 0.4,
})
return (
<GestureDetector gesture={drag.gesture}>
<Motion.View style={[styles.box, drag.animatedStyle]} />
</GestureDetector>
)
}
OptionTypeDefaultNotes
axis'x' | 'y' | 'both''both'Lock the gesture to one axis.
constraints{ left?, right?, top?, bottom? }noneBounds in px from the resting position. Each side independently optional.
elasticnumber (0–1)0Rubber-band coefficient past constraints. 0 hard-clamps; 0.4 is a typical Framer-Motion feel. An overshoot springs back to the bound on release.
onDragStart() => voidnoneFires on JS thread when drag begins.
onDragEnd(info: { x, y, velocity: { x, y } }) => voidnoneFires on JS thread when drag ends.
onRelease(info) => ReleaseResult | void (worklet)noneUI-thread release transition — see below.

The hook returns { gesture, animatedStyle, dragX, dragY, isDragging }. The shared values are public for advanced cases, such as a secondary effect that derives from the drag position.

When the bounds change. A new constraints value applies from the next drag. An element at rest outside the new bounds stays there, and it jumps at the first move of the next drag. To move it at once, write dragX or dragY when you change the bounds. For example, after a rotate changes the width of a slider track, set the thumb to the same fraction of the new track.

Release transitions (onRelease)​

By default, useDrag leaves the shared values where the finger lifted. The one exception is an elastic overshoot: an axis that lifted past constraints springs back to the nearest bound with the default spring and the release velocity. Pass onRelease to animate the values to a settled position with the transition vocabulary of Inertia: a spring snap to a tick, a decay with bounds, a timing settle. The callback runs as a worklet, so the release velocity stays on the UI thread with no JS round trip:

const drag = useDrag({
axis: 'x',
constraints: { left: 0, right: trackWidth },
onRelease: (e) => {
'worklet'
// Snap to the nearest tick using the release velocity for natural continuation.
const snap = nearestTick(e.x, ticks)
return {
x: { type: 'spring', to: snap, velocity: e.velocity.x },
}
},
})

Return one release transition per axis in a ReleaseResult shape. Omit an axis, or return nothing, to leave that shared value where it landed, or, after an elastic overshoot, to spring it back inside constraints. A transition that you return for an axis replaces the spring back on that axis. The transition shapes mirror the core transition prop:

type ReleaseTransition =
| (SpringTransition & { to: number })
| (TimingTransition & { to: number })
| DecayTransition // no `to` — decay decelerates from current position
| (NoAnimationTransition & { to: number })

A decay on release gives a free flick with no snap:

onRelease: (e) => {
'worklet'
return {
x: { type: 'decay', velocity: e.velocity.x, clamp: [0, trackWidth] },
}
}

onRelease and onDragEnd both fire on release. Use onRelease for the shared-value animation. Use onDragEnd for JS-thread side effects (analytics, state updates).

Internally, the release goes through buildReleaseAnimation, which @rootnative/inertia exports. It's a worklet-safe builder for a single-step spring, timing, decay, or no-animation transition. Call it directly when you write a custom Gesture.Pan().onEnd(...) worklet outside useDrag.

useSwipe​

A directional gesture that commits or snaps back. The hook tracks the live translation while the user drags. On release, it fires onSwipe when the distance threshold or the velocity threshold is met along an allowed axis. By default, the position animates back to zero whether the swipe commits or not. That is the correct shape for swipe-to-delete: the row returns, and you remove it. For a card deck, return a commit-exit transition from onCommit, so that the committed card continues off screen instead (see below).

const swipe = useSwipe({
directions: ['left', 'right'],
distanceThreshold: 100,
onSwipe: (direction) => {
if (direction === 'left') dismiss()
if (direction === 'right') accept()
},
})

return (
<GestureDetector gesture={swipe.gesture}>
<Motion.View style={[styles.card, swipe.animatedStyle]}>
{children}
</Motion.View>
</GestureDetector>
)
OptionTypeDefaultNotes
directionsArray<'left' | 'right' | 'up' | 'down'>all fourOnly commits along listed directions. Off-axis swipes do nothing.
distanceThresholdnumber (px)80Distance past which a release commits.
velocityThresholdnumber (px/sec)800Flick velocity that commits even before distance threshold.
releaseTransitionSnapBackTransition | TransitionNamedefault springTransition for the snap-back to zero — see below.
onSwipe(direction, { distance, velocity }) => voidnoneJS-thread callback at release. Direction is one of the allowed values.
onCommit(direction, info) => ReleaseResult | void (worklet)noneUI-thread commit-exit transition — see below.
onSwipeEnd(direction, { finished }) => voidnoneJS-thread callback when a committed swipe's release animation settles.

The dominant axis decides which direction the hook checks. That is the axis where |tx| or |ty| is larger at release.

The hook returns { gesture, animatedStyle, swipeX, swipeY, isActive, reset }. reset() snaps both shared values back to zero with no animation. Use it after a commit exit when the next card takes over the same mounted component. A keyed remount gets fresh values and doesn't need it.

Snap-back transition (releaseTransition)​

The snap-back to zero uses the library default spring. Pass releaseTransition to use your own, inline or as a TransitionName from the nearest <MotionConfig transitions={...}>. That is how the reset joins a design-token motion system:

const swipe = useSwipe({
directions: ['left', 'right'],
releaseTransition: 'spatial-default', // or { type: 'spring', tension: 220 }
onSwipe: (direction) => archive(direction),
})

The hook passes the release velocity into a spring for you, unless the config sets velocity itself. So a flick that stops short of the threshold resets with the momentum it had, not as if the finger had been still.

SnapBackTransition is a spring, a timing, or a no-animation transition. A decay is excluded, because the snap-back always targets zero and a decay has no target. A registered name that resolves to a decay config warns in dev and falls back to the default spring.

Commit exit (onCommit + onSwipeEnd)​

The snap-back is correct for swipe-to-delete. A card deck needs the opposite: a committed card must continue in the swipe direction and leave the screen. Return one release transition per axis from onCommit to run instead of the snap-back. Advance the deck from onSwipeEnd when the exit settles:

const swipe = useSwipe({
directions: ['left', 'right'],
onCommit: (direction, info) => {
'worklet'
return {
x: {
type: 'spring',
to: direction === 'right' ? 500 : -500,
velocity: info.velocity.x, // continue the finger's momentum
},
}
},
onSwipeEnd: (direction, { finished }) => {
if (finished) advanceDeck(direction)
},
})

onCommit runs as a worklet on the UI thread, so write it with the 'worklet' directive. It fires only when the gesture commits. It receives the same info shape as the onRelease of useDrag: the final translation and the release velocity. The return shape is the same ReleaseResult, with one difference: an omitted axis, or a void return, snaps back to zero as usual, because zero is the resting state of this hook.

onSwipeEnd fires on the JS thread when the release animation of the committed swipe settles: the commit exit if one ran, or the snap-back otherwise. That is the moment when the card is gone and the next one takes over. onSwipe, which fires at release, can't give you that moment. finished is false when a new gesture or something else interrupted the animation. onSwipeEnd doesn't fire for a release that didn't commit.

The order is: onSwipe (JS side effects at release), then onCommit (the exit decision on the UI thread), then onSwipeEnd (JS side effects at settle). Use the subset that the interaction needs.

animatedStyle owns the whole transform key​

animatedStyle returns a complete transform array. In React Native, transform is one style key, so a second style with its own transform replaces this one and doesn't merge with it. A rotation next to it, the first thing that almost every card deck adds, drops the translation with no warning. The card then rotates and doesn't follow the finger:

// Broken: tiltStyle's transform replaces the swipe translation.
<Motion.View style={[swipe.animatedStyle, tiltStyle]} />

A static style next to animatedStyle is safe ([styles.card, swipe.animatedStyle] above), because a static style declares no transform. The conflict appears only when the second style is itself a transform.

Build one style from swipeX instead of two styles:

const cardStyle = useInterpolatedStyle(swipe.swipeX, {
translateX: [-200, 0, 200],
rotate: ['-12deg', '0deg', '12deg'],
})

return (
<GestureDetector gesture={swipe.gesture}>
<Motion.View style={[styles.card, cardStyle]}>{children}</Motion.View>
</GestureDetector>
)

A nested second Motion.View also works, with one view per transform source, when the transforms come from independent drivers.

The same applies to useDrag and usePan: each one owns its transform key too.

usePan​

A camera-style pan with momentum on release. The position persists across gestures: the next pan starts from the current offset, not from zero. On release, the position continues to move with the withDecay of Reanimated.

const pan = usePan({
constraints: { left: -240, right: 240, top: -240, bottom: 240 },
deceleration: 0.997,
})

return (
<GestureDetector gesture={pan.gesture}>
<Motion.View style={[styles.canvas, pan.animatedStyle]}>
{/* large content */}
</Motion.View>
</GestureDetector>
)
OptionTypeDefaultNotes
constraints{ left?, right?, top?, bottom? }noneHard clamp during gesture and during decay.
decelerationnumberReanimated defaultHigher = momentum dies faster. Roughly 0.99 (slow) to 0.999 (long glide).
disableMomentumbooleanfalseHard stop on release (drag-like).

For elastic constraints, use useDrag({ elastic }). usePan hard-clamps because it uses the clamp parameter of withDecay, which has no overshoot mode.

When the bounds change. A new constraints value applies from the next pan. An element at rest outside the new bounds stays there, and it jumps at the first move of the next pan. To move it at once, write panX or panY when you change the bounds.

When to pick which​

GestureSnap back on release?Momentum on release?Typical UX
useDragNo — stays where leftNoMove-to-position, sliders, sortable handles.
useSwipeYes — unless onCommit returns an exitNo (springs back)Swipe-to-delete rows, card stacks, dismiss sheets.
usePanNo — stays where leftYes — coasts via withDecayMaps, zoomable canvases, large-image navigation.

Scope: touch + mouse only​

All three hooks use react-native-gesture-handler, which reports touch and mouse events but not keyboard events. Some controls need keyboard accessibility next to drag: a Slider with arrow-key steps, a scrollbar with PageUp and PageDown, a range input. For those, use useTouchDrag from @rootnative/inertia/touch instead. It's the PanResponder counterpart of useDrag. It's part of core, needs no react-native-gesture-handler peer, and composes with onKeyDown on the wrapping View. onKeyDown fires on web. For iOS and Android, see the keyboard note.

The full ladder:

  1. Motion.View with the gesture prop: state-overlay UX only (hover, focus, press).
  2. useTouchDrag from @rootnative/inertia/touch: a pointer drag that composes with key events, with no extra peer dependency.
  3. useDrag, useSwipe, and usePan from this package: pointer only, on gesture-handler. Best for a project that already uses gesture-handler. The UI-thread release path is more precise than the JS-thread velocity of PanResponder.
  4. Raw PanResponder with useAnimation: use this when the gesture lifecycle itself needs custom code (multi-touch composition, a custom capture predicate beyond the axis lock).

useTouchDrag and the gesture-handler useDrag accept the same onRelease shape, and both go through buildReleaseAnimation. So a swap between them changes only the wiring, not the release animation config.

Try it​

example / drag

Why a separate package?​

The gesture prop of the core library covers pressed, focused, focusVisible, and hovered: boolean state-overlay gestures with no continuous translation. Drag, swipe, and pan are value-driven. The gesture publishes a continuous position, and you compose it into the style of the view. These gestures don't fit the boolean-state model, and they need react-native-gesture-handler, which we don't want as a required peer of core.

A separate package keeps the surface of core small and keeps the gesture-handler dependency opt-in. The hooks return primitives (gesture, animatedStyle, shared values) and don't wrap Motion in a new component, so they work with the existing Motion.* set.