useLongPress()
The
useLongPress() hook allows you to detect sustained clicks or touches on an element. It differentiates between a standard "click" and a "long press," making it ideal for secondary actions, context menus, or specialized game controls.Import
typescript
Usage
Action Status
LOCKED 🔒
Clicking does nothing.
Press and hold for 0.7s to unlock.
Component.tsx
API
Arguments
callback– (event) => void The function to execute when the long press is detected.options– object Optional configuration object (see below).
Options Object:
- delay: number (default: 500) - Time in ms before triggering.
- shouldPreventDefault: boolean (default: true) - Whether to prevent the default click event on long press.
Returns
Returns an object of event handlers to spread onto the target element:
onMouseDown, onTouchStart– Starts the timer.onMouseUp, onMouseLeave, onTouchEnd– Clears the timer.onClick– Handles preventing the default action if a long press occurred.