Docs
useLongPress
Enable precise control of long-press interactions for both touch and mouse events with useLongPress.
Loading...
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-long-press
Usage
"use client"
import { useLongPress, type LongPressCallback } from "@/hooks/use-long-press"
export function Component() {
const handleLongPress: LongPressCallback = (event) => {
console.log("Long press detected!", event)
}
const handleStart: LongPressCallback = (event) => {
console.log("Press started", event)
}
const handleFinish: LongPressCallback = (event) => {
console.log("Press finished", event)
}
const handleCancel: LongPressCallback = (event) => {
console.log("Press cancelled", event)
}
const buttonHandlers = useLongPress(handleLongPress, {
threshold: 500,
onStart: handleStart,
onFinish: handleFinish,
onCancel: handleCancel,
})
return <button {...buttonHandlers}>Press and hold me</button>
}
API Reference
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
callback | Function | This is the function to be executed when a long press event is detected. | - | No |
options | object | This is an optional configuration object provided when calling useLongPress . | {} | Yes |
options.threshold | number | This is the time (in milliseconds) the user must press and hold to trigger a long press event. Default value is 400 . | 400 | Yes |
options.onStart | Function | This function is called when the user starts pressing. | - | Yes |
options.onFinish | Function | This function is called when a long press event finishes successfully (the user releases after the threshold). | - | Yes |
options.onCancel | Function | This function is called when a press event is cancelled (the user releases before the threshold). | - | Yes |
Return Values
Name | Type | Description |
---|---|---|
onMouseDown | Function | This is the mouse down event handler. |
onMouseUp | Function | This is the mouse up event handler. |
onMouseLeave | Function | This is the mouse leave event handler. |
onTouchStart | Function | This is the touch start event handler. |
onTouchEnd | Function | This is the touch end event handler. |