useOnLongPress
Detect long press gestures on an element. Fires a handler after a configurable delay, with support for distance thresholds, event modifiers, and a release callback.
Detect long press gestures on an element. Fires a handler callback after a configurable hold duration (default 500ms), with optional distance threshold cancellation, event modifiers, and a release callback.
Press and hold the box for 500ms to trigger a long press.
import { const useRef$: { <T = any>(initialValue: null): Ref$<T | null>; <T = any>(initialValue: T): Ref$<T>; <T = any>(): Ref$<T | null>;}
useRef$ } from "@usels/core";import { const useOnLongPress: (target: MaybeEventTarget, handler: (evt: PointerEvent) => void, options?: DeepMaybeObservable<UseOnLongPressOptions>) => Fn
useOnLongPress } from "@usels/web";
function function Component(): JSX.Element
Component() { const const el$: Ref$<HTMLDivElement | null>
el$ = useRef$<HTMLDivElement>(): Ref$<HTMLDivElement | null> (+2 overloads)
Core (framework-agnostic) version of useRef$.
Creates an observable element ref with opaque wrapping.
- non-null value →
Ref$<T>: current, get(), peek() return T
- null / no arg →
Ref$<T | null>: current, get(), peek() return T | null
Nullability is expressed via the type parameter, mirroring T | null at the call site.
useRef$<interface HTMLDivElement
HTMLDivElement>();
function useOnLongPress(target: MaybeEventTarget, handler: (evt: PointerEvent) => void, options?: DeepMaybeObservable<UseOnLongPressOptions>): Fn
Framework-agnostic long press gesture detector.
Listens for pointerdown/pointermove/pointerup/pointerleave events
and fires the handler callback after the configured delay when held.
useOnLongPress(const el$: Ref$<HTMLDivElement | null>
el$, (e: PointerEvent
e) => { any
console.any
log("Long pressed!", e: PointerEvent
e); });
return <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div RefAttributes<HTMLDivElement>.ref?: Ref<HTMLDivElement> | undefined
Allows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
ref={const el$: Ref$<HTMLDivElement | null>
el$}>Press and hold me</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>;}import { createRef$ } from "@usels/core";import { createOnLongPress } from "@usels/web";
function Component() { "use scope" const el$ = createRef$<HTMLDivElement>();
createOnLongPress(el$, (e) => { console.log("Long pressed!", e); });
return <div ref={el$}>Press and hold me</div>;}Custom delay
Section titled “Custom delay”import { useRef$ } from "@usels/core";import { useOnLongPress } from "@usels/web";
function Component() { const el$ = useRef$<HTMLDivElement>(); useOnLongPress(el$, () => {}, { delay: 1000 }); // 1 second return <div ref={el$} />;}import { createRef$ } from "@usels/core";import { createOnLongPress } from "@usels/web";
function Component() { "use scope" const el$ = createRef$<HTMLDivElement>(); createOnLongPress(el$, () => {}, { delay: 1000 }); // 1 second return <div ref={el$} />;}Distance threshold
Section titled “Distance threshold”Cancel the long press when the pointer moves too far from the initial position. Default is 10px. Set to false to disable.
import { useRef$ } from "@usels/core";import { useOnLongPress } from "@usels/web";
function Component() { const el$ = useRef$<HTMLDivElement>();
// Custom threshold useOnLongPress(el$, () => {}, { distanceThreshold: 20 });
// Disable distance checking useOnLongPress(el$, () => {}, { distanceThreshold: false });
return <div ref={el$} />;}import { createRef$ } from "@usels/core";import { createOnLongPress } from "@usels/web";
function Component() { "use scope" const el$ = createRef$<HTMLDivElement>();
// Custom threshold createOnLongPress(el$, () => {}, { distanceThreshold: 20 });
// Disable distance checking createOnLongPress(el$, () => {}, { distanceThreshold: false });
return <div ref={el$} />;}Release callback (onMouseUp)
Section titled “Release callback (onMouseUp)”onMouseUp is called when the pointer is released, providing duration, distance, and whether a long press was detected.
import { useRef$ } from "@usels/core";import { useOnLongPress } from "@usels/web";
function Component() { const el$ = useRef$<HTMLDivElement>();
useOnLongPress(el$, () => {}, { onMouseUp: (duration, distance, isLongPress, event) => { if (isLongPress) { console.log(`Long press released after ${duration}ms`); } else { console.log(`Short press: ${duration}ms`); } }, });
return <div ref={el$} />;}import { createRef$ } from "@usels/core";import { createOnLongPress } from "@usels/web";
function Component() { "use scope" const el$ = createRef$<HTMLDivElement>();
createOnLongPress(el$, () => {}, { onMouseUp: (duration, distance, isLongPress, event) => { if (isLongPress) { console.log(`Long press released after ${duration}ms`); } else { console.log(`Short press: ${duration}ms`); } }, });
return <div ref={el$} />;}Event modifiers
Section titled “Event modifiers”import { useRef$ } from "@usels/core";import { useOnLongPress } from "@usels/web";
function Component() { const el$ = useRef$<HTMLDivElement>();
useOnLongPress(el$, () => {}, { modifiers: { prevent: true, // preventDefault on pointer events stop: true, // stopPropagation self: true, // only trigger on the element itself (not children) capture: true, // use capturing phase once: true, // fire only once }, });
return <div ref={el$} />;}import { createRef$ } from "@usels/core";import { createOnLongPress } from "@usels/web";
function Component() { "use scope" const el$ = createRef$<HTMLDivElement>();
createOnLongPress(el$, () => {}, { modifiers: { prevent: true, // preventDefault on pointer events stop: true, // stopPropagation self: true, // only trigger on the element itself (not children) capture: true, // use capturing phase once: true, // fire only once }, });
return <div ref={el$} />;}Stop function
Section titled “Stop function”useOnLongPress returns a stop function to manually remove all listeners and clear timers.
import { useRef$ } from "@usels/core";import { useOnLongPress } from "@usels/web";
function Component() { const el$ = useRef$<HTMLDivElement>(); const stop = useOnLongPress(el$, () => {});
// Later: remove all listeners stop();
return <div ref={el$} />;}import { createRef$ } from "@usels/core";import { createOnLongPress } from "@usels/web";
function Component() { "use scope" const el$ = createRef$<HTMLDivElement>(); const stop = createOnLongPress(el$, () => {});
// Later: remove all listeners stop();
return <div ref={el$} />;}Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
target | MaybeEventTarget | - The element to detect long presses on |
handler | (evt: PointerEvent) => void | - Callback fired when a long press is detected |
options | UseOnLongPressOptions (optional) | - Configuration options |
UseOnLongPressOptions
Section titled “UseOnLongPressOptions”Returns
Section titled “Returns”Fn
A stop function that removes all event listeners and clears timers