useOnLongPress

About

A hook to detect long press on an element.

Examples

Basic example

import { useOnLongPress } from "rooks";
export default function App() {
  const handleLongPress = () => {
    console.log("Long press detected");
  };

  const longPressRef = useOnLongPress(handleLongPress);

  return (
    <>
      <p>
        Press and hold the button for 500ms to trigger a console.log statement
      </p>
      <button ref={longPressRef}>Long press me</button>
    </>
  );
}

Custom duration and touch end behavior

import { useOnLongPress } from "rooks";
export default function App() {
  const handleLongPress = () => {
    console.log("Long press detected");
  };

  const longPressRef = useOnLongPress(handleLongPress, {
    duration: 1000,
    onClick: handleTouchEnd,
  });

  return (
    <>
      <p>
        Press and hold the button for 1000ms to trigger a console.log statement
      </p>
      <button ref={longPressRef}>Long press me</button>
    </>
  );
}

Arguments

Argument valueTypeDescriptionDefualt
callbackFunctionCallback function to be called when long press is detectedundefined
optionsObjectSee table belowundefined
Options valueTypeDescriptionDefault
durationNumberThe duration (in ms) after which long press is detected300
onClickFunctionFires on clickundefined

Returns

Return valueTypeDescriptionDefualt
refCallback RefA ref that can be used on the element you want to detect long press onundefined

Join the community!

Join our discord server! You can click the floating discord icon at the bottom right of the screen and chat with us!

Powered by vercel