vue-use-gesture

Vue hook for receiving gestures https://vue-use-gesture.netlify.app
36,138 150
Install
npm install vue-use-gesture
Latest Version:1.0.1
Vue:2 · 3
License:MIT
Last Updated:Aug 11, 2021
Links: GitHub  ·  npm
Maintainer: koca

Vue UseGesture

Vue UseGesture is a hook that lets you bind richer mouse and touch events to any component or view. With the data you receive, it becomes trivial to set up gestures, and often takes no more than a few lines of code.

You can use it stand-alone, but to make the most of it you should combine it with an animation library like vue-use-spring, though you can most certainly use any other.

Installation

#Yarn
yarn add vue-use-gesture

#NPM
npm install vue-use-gesture

Full documentation website

Simple example

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

<script>
  import { useDrag } from 'vue-use-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' }))

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

The example above makes a div draggable so that it follows your mouse on drag, and returns to its initial position on release.

Make sure you always set touchAction on a draggable element to prevent glitches with the browser native scrolling on touch devices.

Available hooks

vue-use-gesture exports several hooks that can handle different gestures:

Hook Description
useDrag Handles the drag gesture
useMove Handles mouse move events
useHover Handles mouse enter and mouse leave events
useScroll Handles scroll events
useWheel Handles wheel events
usePinch Handles the pinch gesture
useGesture Handles multiple gestures in one hook
More on the full documentation website...

Thanks

This library is a port of React UseGesture. Thanks Polmandres 🙏

License

MIT

Related Packages

vue-prism-editor

A dead simple code editor with syntax highlighting and line numbers.

3,560,154 755
@highlighted/vue-js-editor

Simple code editor with syntax highlighting and line numbers.

1,932
@codemirror/lang-vue

Vue template support for the CodeMirror code editor

71,371,417 29
ace-editor-vue3

Like vue2-ace-editor but more functional and supports Vue 3

19,103 2

Version History

Version Released License
1.0.1 MIT