Playground

useLongPress

Call a handler when an element is held down, cancelling if the pointer moves or lifts early.

  • Press and hold is hard for many people: always offer a normal click or keyboard action for the same result.

Import

import '@monoline/css';
import {useLongPress} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add use-long-press

Usage

import {useState} from 'react';
import {Button, useLongPress, type Demo} from '@monoline/react';

function Demo() {
  const [n, setN] = useState(0);
  const press = useLongPress(() => setN((v) => v + 1), {delay: 600});
  return <div style={{display: 'flex', gap: 'var(--s3)', alignItems: 'center'}}><Button {...press} onClick={() => setN((v) => v + 1)}>Hold or click</Button><output aria-live="polite">{n} presses</output></div>;
}

<Demo />

Examples

Hold for 0.6 seconds

A normal click does the same thing, so the hold is optional.

Props

UseLongPressOptions

PropTypeDefaultDescription
delaynumber500Hold time in milliseconds. Default 500.
tolerancenumber10Movement in pixels that cancels the press. Default 10.

Behaviour

  • Press and hold is hard for many people: always offer a normal click or keyboard action for the same result.