Skip to Content
react-utilsuseGesture

useGesture

See source on Github

Attaches synthetic gesture events on the provided ref whose triggers can be constrained via axis, pointerType, deadZone, and lifespan options.

When a valid gesture is detected, onGestureStart, onGestureMove, and onGestureEnd callbacks are invoked, allowing for custom behavior to be implemented.

If a gesture is deemed invalid, the gesture can be aborted by calling the abort method on the returned event object, in which case the onGestureEnd callback is called immediately.

  • @param ref - A RefObject of the element to observe.
  • @param options - Configurable options
  • @returns void

Example

const ref = useRef<HTMLDivElement>(null) useGesture(ref, { onGestureStart: ({ x, y }) => console.log('Gesture started at:', x, y), onGestureMove: ({ deltaX, deltaY }) => console.log('Gesture moved by:', deltaX, deltaY), onGestureEnd: ({ deltaX, deltaY }) => console.log('Gesture ended at:', deltaX, deltaY) }) return <div ref={ref} />

GestureStartEvent

See source on Github

A synthetic gesture event that is triggered when the pointer pressed down.

GestureMoveEvent

See source on Github

A synthetic gesture event that is triggered when the pointer device is pressed down and moving.

GestureEndEvent

See source on Github

A synthetic gesture event that is triggered when the pointer device is released.

GesturePosition

See source on Github

The current position of pointer device at a given time during the gesture.

GestureMovement

See source on Github

The pointer device rate of travel in both axes. Calculated between move events.

GestureDelta

See source on Github

The delta between current pointer position in relation to initial gesture position.

GestureDirection

See source on Github

The general direction pointer device is traveling over the course of the gesture.

GesturePointerType

See source on Github

The type of pointer device used for the current gesture.