R
fn

useSwipe

v0.0.15testeddemo

Detect swipe gestures via touch events on a target element. Tracks start/end coordinates, the active state and the resolved direction.

Example

ts
const el = useTemplateRef<HTMLElement>('el');
const { isSwiping, direction, lengthX, lengthY } = useSwipe(el, {
  onSwipeEnd(e, dir) { console.log(dir); },
});

Demo

Loading demo…

Signature

ts
export function useSwipe(
  target: MaybeComputedElementRef,
  options: UseSwipeOptions ={ ... }

Parameters

ParameterTypeDescription
targetMaybeComputedElementRefElement ref, getter, or instance to listen on
options?UseSwipeOptionsThreshold, lifecycle callbacks and passive flag

Returns

UseSwipeReturnReactive swipe state and a `stop` teardown function