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

NameTypeDescriptionDefault ValueOptional
callbackFunctionThis is the function to be executed when a long press event is detected.-No
optionsobjectThis is an optional configuration object provided when calling useLongPress.{}Yes
options.thresholdnumberThis is the time (in milliseconds) the user must press and hold to trigger a long press event. Default value is 400.400Yes
options.onStartFunctionThis function is called when the user starts pressing.-Yes
options.onFinishFunctionThis function is called when a long press event finishes successfully (the user releases after the threshold).-Yes
options.onCancelFunctionThis function is called when a press event is cancelled (the user releases before the threshold).-Yes

Return Values

NameTypeDescription
onMouseDownFunctionThis is the mouse down event handler.
onMouseUpFunctionThis is the mouse up event handler.
onMouseLeaveFunctionThis is the mouse leave event handler.
onTouchStartFunctionThis is the touch start event handler.
onTouchEndFunctionThis is the touch end event handler.