Skip to content

useLongPress ​

Hook that defines the logic when long pressing an element

sensors
test coverage
Last changed: last month

Installation ​

Library
CLI
Manual
typescript
import { useLongPress } from '@siberiacancode/reactuse';

Usage ​

typescript
const pressed = useLongPress(ref, () => console.log('callback'));
// or
const { ref, pressed } = useLongPress(() => console.log('callback'));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
targetHookTarget-The target element to be long pressed
callback(event: LongPressEvents) => void-The callback function to be invoked on long press
options?UseLongPressOptions-The options for the long press

Returns

boolean

Parameters

NameTypeDefaultNote
callback(event: LongPressEvents) => void-The callback function to be invoked on long press
options?UseLongPressOptions-The options for the long press

Returns

boolean

Type declaration ​

typescript
import type { HookTarget } from '@/utils/helpers';

import type { StateRef } from '../useRefState/useRefState';

export type LongPressEvents = MouseEvent | TouchEvent;

export interface UseLongPressOptions {
  // * The threshold time in milliseconds
  threshold?: number;
  // * The callback function to be invoked on long press cancel
  onCancel?: (event: LongPressEvents) => void;
  // * The callback function to be invoked on long press end
  onFinish?: (event: LongPressEvents) => void;
  // * The callback function to be invoked on long press start
  onStart?: (event: LongPressEvents) => void;
}

export interface UseLongPress {
  (
    target: HookTarget,
    callback: (event: LongPressEvents) => void,
    options?: UseLongPressOptions
  ): boolean;

  <Target extends Element>(
    callback: (event: LongPressEvents) => void,
    options?: UseLongPressOptions,
    target?: never
  ): {
    ref: StateRef<Target>;
    pressed: boolean;
  };
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
H
hywax
hywax

Released under the MIT License.