R
fn

onLongPress

v0.0.15testeddemo

Directive-like helper that invokes a handler after a sustained long press on a target element. Movement beyond `distanceThreshold` cancels the press, and an optional `onMouseUp` callback reports the press duration, distance, and long-press status. Listeners are passive by default and registered via `useEventListener` for automatic cleanup.

Examples

ts
onLongPress(el, () => console.log('held!'));
ts
// Custom delay, cancel-on-move disabled, release reporting
const stop = onLongPress(el, onHeld, {
  delay: 800,
  distanceThreshold: false,
  modifiers: { prevent: true },
  onMouseUp: (duration, distance, isLongPress) => {
    if (!isLongPress) onTap();
  },
});

Demo

Loading demo…

Signature

ts
export function onLongPress(
  target: MaybeComputedElementRef,
  handler: (event: PointerEvent) => void,
  options: OnLongPressOptions ={ ... }

Parameters

ParameterTypeDescription
targetMaybeComputedElementRefElement to watch for long presses
handler(event: PointerEvent) => voidCallback fired once the press passes `delay`
options?OnLongPressOptionsLong-press configuration

Returns

VoidFunctionStop handle that removes all listeners