VueUse Gesture

1.0.1 · active · verified Sun Apr 19

@vueuse/gesture is a comprehensive collection of Vue Composables designed to provide robust and interactive gesture support for Vue applications. It offers a suite of hooks for various pointer and touch gestures, including `useDrag`, `useMove`, `useHover`, `useScroll`, `useWheel`, `usePinch`, and a generic `useGesture` for handling multiple interactions simultaneously. The library is currently stable at version `2.0.0` (last published approximately two years ago) and is an active part of the broader VueUse ecosystem. Its key differentiators include seamless compatibility with both Vue 2 and Vue 3 environments via `vue-demi`, offering both composable function-based and directive-based (e.g., `v-drag`) APIs. It is a direct port of the well-known `react-use-gesture` library, bringing similar declarative and powerful animation possibilities to the Vue ecosystem, and is explicitly designed to integrate smoothly with animation libraries like `@vueuse/motion`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic draggable element using the `useDrag` composable, integrating with `vue-use-spring` for smooth animation and including the essential `touch-action` CSS property.

<template>
  <!-- Bind it to a component -->
  <div v-bind="bind()" :style="style" class="box"></div>
</template>

<script lang="ts">
  import { useDrag } from '@vueuse/gesture'
  import { useSpring } from 'vue-use-spring'
  import { defineComponent, computed } from 'vue'

  export default defineComponent({
    setup() {
      const [{ x, y }, set] = useSpring(() => ({ x: 0, y: 0 }))

      // Set the drag hook and define component movement based on gesture data
      const bind = useDrag(({ down, movement: [mx, my] }) => {
        set({ x: down ? mx : 0, y: down ? my : 0 })
      })

      const style = computed(() => ({
        transform: `translate3d(${x.value}px,${y.value}px,0)`,
        touchAction: 'none' // Crucial for preventing browser native scrolling glitches
      }))

      return { bind, style }
    },
  })
</script>

<style scoped>
.box {
  width: 100px;
  height: 100px;
  background: #42b983;
  border-radius: 8px;
  cursor: grab;
  user-select: none;
}
.box:active {
  cursor: grabbing;
}
</style>

view raw JSON →