Skip to Content
ohooks

usePointerDrag

Tracks pointer drag gestures on a target element with axis locking and movement thresholds.

Loading preview...

The usePointerDrag hook tracks pointer drag gestures on a target element using setPointerCapture so dragging continues smoothly even when the pointer leaves the element bounds, supporting axis locking ('x', 'y', or 'both') and activation distance thresholds.

Installation

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

Usage

Examples

Draggable Card with Transform Offset

Translate an element by deltaX and deltaY while dragging and accumulate the final offset in onDragEnd.

API Reference

UsePointerDragOptions

Configuration options and gesture callbacks for pointer dragging.

Property
Specification
enabled?
uniondefault:true
Whether pointer drag tracking is enabled
axis?
uniondefault:'both'
Constrain drag deltas to a specific axis
threshold?
numberdefault:0
Minimum distance in pixels before dragging starts
preventUserSelect?
uniondefault:true
Disable text selection on document.body while dragging
preventDefault?
uniondefault:false
Call event.preventDefault() on pointer events
onDragStart?
function
Callback fired when dragging begins.
onDragMove?
function
Callback fired on each pointer move while dragging.
onDragEnd?
function
Callback fired when the drag operation ends or is cancelled.

PointerDragState

Live coordinates and movement deltas returned by the hook.

Property
Specification
isDragging
union
Whether a drag gesture is currently active.
x
number
Current pointer X coordinate (clientX).
y
number
Current pointer Y coordinate (clientY).
startX
number
Initial pointer X coordinate where the drag started.
startY
number
Initial pointer Y coordinate where the drag started.
deltaX
number
Total horizontal displacement from startX.
deltaY
number
Total vertical displacement from startY.
movementX
number
Horizontal distance moved since the previous pointer move event.
movementY
number
Vertical distance moved since the previous pointer move event.