npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@usefy/use-long-press

v0.25.1

Published

A React hook for long-press gestures with a time threshold and movement cancellation — mouse and touch, with onStart/onFinish/onCancel callbacks

Readme


Overview

useLongPress is part of the @usefy ecosystem — a collection of production-ready, TypeScript-first, SSR-safe React hooks. It recognises a long-press gesture — a pointer held down on an element for at least a time threshold — and returns a bind object of DOM handler props you spread onto the target: <button {...bind}>.

Features

  • bind object — spread the memoized handlers onto any element; they're referentially stable so spreading never churns the element's listeners.
  • Time threshold — the callback fires only after the press is held for threshold ms (default 400).
  • Movement cancellation — dragging farther than moveThreshold px (default 10) from the down point cancels the press; pass moveThreshold: false to disable.
  • Mouse + touch — one gesture for both inputs; the synthetic mouse events browsers emit after a touch are detected and ignored, so a touch long-press never double-fires.
  • Lifecycle callbacks — optional onStart, onFinish, and onCancel (with a { reason } of "released" | "moved"), all kept in latest-refs so inline functions never destabilise the handlers.
  • Safe by default — the pending timer is cleared on release, cancel, and unmount; SSR-safe (no window/document access) and StrictMode/concurrent-safe.
  • TypeScript-first — full type inference and exported types.
  • Tiny & tree-shakeable — published as its own package.

Installation

# npm
npm install @usefy/use-long-press

# yarn
yarn add @usefy/use-long-press

# pnpm
pnpm add @usefy/use-long-press

Requires React 18 or 19 (peerDependencies: "react": "^18.0.0 || ^19.0.0").

Quick Start

import { useLongPress } from "@usefy/use-long-press";

function DeleteButton() {
  const bind = useLongPress(() => deleteItem(), { threshold: 600 });

  return (
    <button {...bind} style={{ touchAction: "none", userSelect: "none" }}>
      Hold to delete
    </button>
  );
}

API

const bind = useLongPress(callback, options?);
// <button {...bind}>Press and hold</button>

Parameters

| Parameter | Type | Description | | ---------- | --------------------------- | --------------------------------------------------------------------------- | | callback | (event) => void | Fires once when the press reaches threshold. Receives the originating pointer-down event. | | options | UseLongPressOptions | Optional configuration (see below). |

Options

| Option | Type | Default | Description | | --------------- | ------------------------------------------ | ------- | -------------------------------------------------------------------------------------------------- | | threshold | number | 400 | Milliseconds the press must be held before callback fires. | | moveThreshold | number \| false | 10 | Pixels the pointer may drift from the down point before cancelling (reason "moved"). false disables movement cancellation. | | disabled | boolean | false | When true, the handlers are no-ops (read at press-start). | | onStart | (event) => void | — | Fires when a valid press begins, before the timer starts. | | onFinish | (event) => void | — | Fires when a completed long press is then released. | | onCancel | (event, { reason }) => void | — | Fires when a press ends early — reason is "released" or "moved". |

Returns

bind — a stable object of DOM handler props: onMouseDown, onMouseUp, onMouseLeave, onMouseMove, onTouchStart, onTouchEnd, onTouchMove. Spread it onto the target element to wire up the whole gesture.

preventDefault caveat

React attaches onTouchStart/onTouchMove as passive listeners, so calling event.preventDefault() inside them will not stop scrolling or the long-press context menu (the browser ignores it). Prevent those with CSS on the target instead:

.long-press-target {
  touch-action: none;      /* stop scroll/pan while pressing */
  user-select: none;       /* stop text selection */
  -webkit-touch-callout: none; /* stop the iOS callout menu */
}

This hook deliberately never calls preventDefault.

Touch / mouse de-duplication

After a touch sequence, browsers emit synthetic mousedown/mouseup/click events. useLongPress timestamps the last touch event and ignores any mousedown that arrives within a short guard window, so a single touch long-press fires exactly once. Pure-mouse and pure-touch devices are unaffected.

Testing

📊 View Detailed Coverage Report (GitHub Pages) — 29 tests, 100% statement coverage.

License

MIT © mirunamu

This package is part of the usefy monorepo.