fn

useSwipe

v0.0.14testeddemo

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
PropertyTypeDescription
isSwipingShallowRef<boolean>Whether a swipe is currently in progress (threshold exceeded).
directionComputedRef<UseSwipeDirection>The resolved swipe direction.
coordsStartDeepReadonly<UseSwipePosition>Coordinates of the initial touch.
coordsEndDeepReadonly<UseSwipePosition>Coordinates of the latest touch position.
lengthXComputedRef<number>Signed horizontal distance travelled (coordsEnd.x - coordsStart.x).
lengthYComputedRef<number>Signed vertical distance travelled (coordsEnd.y - coordsStart.y).
stop() => voidTear down all listeners.