Skip to content

useSwipe

Category
Export Size
996 B
Last Changed
33 minutes ago

Reactive swipe detection based on TouchEvents.

Demo

Swipe right

Direction: none
lengthX: 0 | lengthY: 0

Usage

vue
<script setup>
const el = ref(null)
const { isSwiping, direction } = useSwipe(el)
</script>

<template>
  <div ref="el">
    Swipe here
  </div>
</template>

Type Declarations

Show Type Declarations
typescript
export type UseSwipeDirection = "up" | "down" | "left" | "right" | "none"
export interface UseSwipeOptions extends ConfigurableWindow {
  /**
   * Register events as passive
   *
   * @default true
   */
  passive?: boolean
  /**
   * @default 50
   */
  threshold?: number
  /**
   * Callback on swipe start
   */
  onSwipeStart?: (e: TouchEvent) => void
  /**
   * Callback on swipe moves
   */
  onSwipe?: (e: TouchEvent) => void
  /**
   * Callback on swipe ends
   */
  onSwipeEnd?: (e: TouchEvent, direction: UseSwipeDirection) => void
}
export interface UseSwipeReturn {
  isPassiveEventSupported: boolean
  isSwiping: Ref<boolean>
  direction: ComputedRef<UseSwipeDirection>
  coordsStart: Readonly<Position>
  coordsEnd: Readonly<Position>
  lengthX: ComputedRef<number>
  lengthY: ComputedRef<number>
  stop: () => void
}
/**
 * Reactive swipe detection.
 *
 * @see https://vueuse.org/useSwipe
 * @param target
 * @param options
 */
export declare function useSwipe(
  target: MaybeRefOrGetter<EventTarget | null | undefined>,
  options?: UseSwipeOptions,
): UseSwipeReturn

Source

SourceDemoDocs

Contributors

Anthony Fu
丶远方
Amirreza Zarkesh
lstoeferle
IlyaL
Anthony Fu
Alexey Istomin
Jelf
云游君
Shinigami
Alex Kozack

Changelog

v11.3.0 on 11/21/2024
1bb54 - fix: allow vertical scrolling during swipe (#4323)
v10.7.0 on 12/5/2023
fccf2 - feat: upgrade deps (#3614)
v10.1.0 on 4/22/2023
af3ca - fix: ignore multiple touches (#2996)
995aa - fix: ignore multiple touches (#2994)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
v10.0.0-beta.0 on 3/14/2023
f285c - fix!: use literal for swipe direction instead of enum
08c21 - feat(useSwipe, usePointerSwipe, useTransition): improve tree-shaking (#2863)

Released under the MIT License.