Skip to Content
ohooks

useLongPress

Detects long-press pointer gestures with movement threshold cancellation and click suppression.

Loading preview...

The useLongPress hook returns pointer and click event handlers that invoke a callback when the user presses and holds an element for threshold milliseconds, cancelling automatically if the pointer moves beyond moveThreshold and suppressing the subsequent synthetic click event.

Installation

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

Usage

Examples

Long Press vs. Normal Click

Spread the returned handlers onto a button that also has a normal onClick—if a long press completes, the click event is suppressed automatically.

Lifecycle Callbacks (onStart, onFinish, onCancel)

Track press progress and distinguish completed long presses from cancelled ones using onStart, onFinish, and onCancel.

API Reference

UseLongPressOptions

Configuration options and lifecycle callbacks for the long-press gesture.

Property
Specification
threshold?
numberdefault:400
Hold duration in milliseconds required to trigger the long press callback
moveThreshold?
numberdefault:10
Maximum pointer movement in pixels allowed before canceling the long press
preventClick?
uniondefault:true
Suppress the subsequent click event after a long press triggers
cancelOnLeave?
uniondefault:true
Cancel the pending long press if the pointer leaves the element
filterEvents?
function
Predicate to filter which pointer events can initiate a long press
onStart?
function
Callback fired when the pointer press starts
onFinish?
function
Callback fired when the pointer is released after a completed long press
onCancel?
function
Callback fired if the press is released, moved, or canceled before threshold elapses

UseLongPressHandlers

DOM event handlers returned by the hook to spread onto the target element.

Property
Specification
onPointerDownrequired
function
Pointer down handler to spread onto the target element
onPointerMoverequired
function
Pointer move handler to cancel the press if movement exceeds moveThreshold
onPointerUprequired
function
Pointer up handler to finish or cancel the press
onPointerLeaverequired
function
Pointer leave handler to cancel the press when leaving the element
onPointerCancelrequired
function
Pointer cancel handler
onClickCapturerequired
function
Click capture handler to suppress synthetic clicks after a long press