Skip to Content
ohooks

useSwipe

Detects pointer swipe gestures and directions on a target element.

Loading preview...

The useSwipe hook detects directional swipe gestures ('left', 'right', 'up', 'down') on a target element using Pointer Events, enforcing both a minimum distance (threshold) and a maximum gesture duration (timeout).

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useSwipe.json

Usage

Examples

Use onSwipeLeft and onSwipeRight to advance or rewind slides on touch and pointer devices.

Vertical Swipe to Dismiss

Listen to onSwipeDown to dismiss a bottom sheet or notification card when the user swipes downward.

API Reference

UseSwipeOptions

Configuration options and directional callbacks for swipe detection.

Property
Specification
threshold?
numberdefault:50
Minimum distance in pixels required to recognize a swipe direction
enabled?
uniondefault:true
Whether swipe detection is active
preventDefault?
uniondefault:false
Call event.preventDefault() during pointer movement
onSwipeStart?
function
Callback fired when a pointer down initiates a swipe.
onSwipe?
function
Callback fired during pointer movement once threshold is exceeded.
onSwipeEnd?
function
Callback fired when the pointer is released or cancelled.
onSwipeLeft?
function
Callback fired when a swipe finishes in the 'left' direction.
onSwipeRight?
function
Callback fired when a swipe finishes in the 'right' direction.
onSwipeUp?
function
Callback fired when a swipe finishes in the 'up' direction.
onSwipeDown?
function
Callback fired when a swipe finishes in the 'down' direction.

SwipeState

Live swipe status, direction, and movement deltas returned by the hook.

Property
Specification
isSwiping
union
Whether a pointer swipe gesture is currently in progress.
direction
SwipeDirection
Detected swipe direction once threshold is exceeded.
lengthX
number
Horizontal distance swiped (startX - currentX).
lengthY
number
Vertical distance swiped (startY - currentY).