Skip to content

useLongPress

A React hook for detecting long-press events on an element.


✨ Overview

useLongPress enables you to trigger a callback when a user presses and holds on an element for a specified duration. Useful for mobile gestures, context menus, or custom interactions.


📦 Import

tsx
import { useLongPress } from 'react-hookstack';

🚀 Usage Example

tsx
import { useLongPress } from 'react-hookstack';

function Example() {
  const longPressEvents = useLongPress(() => alert("Long pressed!"), 700);

  return (
    <button {...longPressEvents}>Hold me</button>
  );
}

🧩 API Reference

useLongPress(callback: () => void, ms?: number): { ...handlers }

Parameters

ParameterTypeDefaultDescription
callback() => voidFunction called on long press.
msnumber500Duration in ms to trigger long press.

Returns

PropertyTypeDescription
onMouseDownfunctionAttach to element.
onMouseUpfunctionAttach to element.
onMouseLeavefunctionAttach to element.
onTouchStartfunctionAttach to element.
onTouchEndfunctionAttach to element.

⚙️ Implementation

tsx
import { useRef, useCallback } from "react";

export function useLongPress(callback: () => void, ms: number = 500) {
    // Ref to store the timer ID
    const timer = useRef<NodeJS.Timeout | null>(null);

    // Start the long press timer
    const start = useCallback(() => {
        timer.current = setTimeout(callback, ms);
    }, [callback, ms]);

    // Stop the long press timer
    const stop = useCallback(() => {
        if (timer.current) {
            clearTimeout(timer.current);
            timer.current = null;
        }
    }, []);

    // Return handlers to attach to the element
    return {
        onMouseDown: start,
        onMouseUp: stop,
        onMouseLeave: stop,
        onTouchStart: start,
        onTouchEnd: stop,
    };
}

💡 Notes

  • Works for both mouse and touch events.
  • Prevents accidental triggers by requiring a hold.

🧾 Type Definition

tsx
type LongPressHandlers = {
  onMouseDown: () => void;
  onMouseUp: () => void;
  onMouseLeave: () => void;
  onTouchStart: () => void;
  onTouchEnd: () => void;
};

🧭 Summary

FeatureDescription
⏱️ CustomizableSet duration for long press
🧩 FlexibleMouse & touch support
⚡ LightweightNo dependencies