useLongPress
Call a handler when an element is held down, cancelling if the pointer moves or lifts early.
Source@monoline/react 0.4.0Markdown for AI toolsstable
- 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
Props
UseLongPressOptions
| Prop | Type | Default | Description |
|---|---|---|---|
delay | number | 500 | Hold time in milliseconds. Default 500. |
tolerance | number | 10 | Movement 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.