Skip to main content

useLongPress

The useLongPress hook detects a press-and-hold gesture on both mouse and touch devices. Spread the returned handlers onto any element; the callback fires once the press has lasted long enough, and releasing or leaving the element early cancels it.

Usage​

Installation​

import { useLongPress } from "hookstorm";

Parameters​

  • callback — Called once the press has lasted long enough. The latest version is always used.
  • options — (optional) An object with:
    • delay — How long the press must last, in milliseconds. Default: 400.

Return Values​

An object of event handlers to spread onto the target element: onMouseDown, onMouseUp, onMouseLeave, onTouchStart, onTouchEnd and onTouchCancel.

Example Usage​

import { ReactElement, useState } from "react";
import { useLongPress } from "hookstorm";

export default function DeleteButton(): ReactElement {
const [deleted, setDeleted] = useState(false);
const longPress = useLongPress(() => setDeleted(true), { delay: 800 });

return (
<button {...longPress}>
{deleted ? "Deleted!" : "Hold to delete"}
</button>
);
}

Notes​

  • The timer is cleared automatically when the component unmounts.
  • A normal short click is not affected: onClick still fires as usual.
  • The hook only reports the long press; it does not prevent text selection or the context menu on touch devices. Add user-select: none to the element if you need that.