# @technarts/react-use-hold

> React hook for handling clicking and holding it.

Latest version **1.0.2** (published 2023-08-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install @technarts/react-use-hold
pnpm add @technarts/react-use-hold
yarn add @technarts/react-use-hold
bun add @technarts/react-use-hold
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-08-21 |
| First published | 2023-08-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 23 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | technarts |
| Maintainers | fenestra, guney, scriptmonster |
| Keywords | react, typescript, hold, click |

## Links

- npm: https://www.npmjs.com/package/@technarts/react-use-hold
- Repository: https://github.com/technarts/react-use-hold
- Homepage: https://github.com/technarts/react-use-hold#readme
- Issues: https://github.com/technarts/react-use-hold/issues
- npm.io page: https://npm.io/package/@technarts/react-use-hold

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-08-21
- 1.0.1 — 2023-08-15
- 1.0.0 — 2023-08-15

## README

# React Hook for Handling Long Button Presses

Altough the title states "Button Presses", `useHold` can be used for any HTML element supporting `onclick` event. With `useHold` hook, you can make a function call on N'th millisecond of a mouse click, suppressing the normal click event.

## Installation

```bash
$ npm i @technarts/react-use-hold
```

## Usage

Below code exemplifies a button that whose regular clicks are handled as well as long clicks up to `500ms`. It also demonstrates how to handle the release, which is fired at the end of the long click.

```typescript
import React from "react";
import { useHold } from "@technarts/react-use-hold";

const App = () => {
  const [clicked, setClicked] = React.useState<boolean>(false)
  const [clicking, setClicking] = React.useState<boolean>(false)
  
  const events = useHold({
    ms: 500,
    onClick: () => {
      setClicked(true);
    },
    onHold: () => {
      setClicking(true);
    },
    onRelease: () => {
      setClicked(false);
      setClicking(false);
    },
  })

  return (
    <>
      <button {...events} >Click and keep clicking</button>
      <p>Clicked: {clicked ? "Yes" : "No"}</p>
      <p>Clicking: {clicking ? "Yes" : "No"}</p>
    </>
  )
}
```

## Types

### `useHold(params: Params) => void`

```typescript
type Params = {
  onClick?: React.MouseEventHandler, // This one will be called for clicks shorter than ms.
  onHold?: (event: React.MouseEvent<Element>, target: EventTarget) => void, // ...for clicks longer than ms.
  onRelease?: () => void, // Fired just after onHold.
  ms: number, // The measurement of longness for clicking and holding.
}
```

## Inspiration

Inspired by (and highly recommended reading): https://spacejelly.dev/posts/how-to-detect-long-press-gestures-in-javascript-events-in-react (Author: Colby Fayock, thanks.)

---
_Source: https://npm.io/package/@technarts/react-use-hold · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
