@use-gesture/react
React target for @use-gesture
Last updated 2 years ago by dbismut .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @use-gesture/react 
SYNC missed versions from official npm registry.

@use-gesture

npm (tag) npm bundle size NPM Discord Shield

@use-gesture is a library 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 react-spring, though you can most certainly use any other.

The demos are real click them!

Installation

React

#Yarn
yarn add @use-gesture/react

#NPM
npm install @use-gesture/react

Vanilla javascript

#Yarn
yarn add @use-gesture/vanilla

#NPM
npm install @use-gesture/vanilla

Full documentation website

Simple example

<summary>React</summary>
import { useSpring, animated } from '@react-spring/web'
import { useDrag } from '@use-gesture/react'

function Example() {
  const [{ x, y }, api] = useSpring(() => ({ x: 0, y: 0 }))

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

  // Bind it to a component.
  return <animated.div {...bind()} style={{ x, y, touchAction: 'none' }} />
}
<summary>Vanilla javascript</summary>
<!-- index.html -->
<div id="drag" />
// script.js
const el = document.getElementById('drag')
const gesture = new DragGesture(el, ({ active, movement: [mx, my] }) => {
  setActive(active)
  anime({
    targets: el,
    translateX: active ? mx : 0,
    translateY: active ? my : 0,
    duration: active ? 0 : 1000
  })
})

// when you want to remove the listener
gesture.destroy()

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

@use-gesture/react 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...

Current Tags

  • 10.3.1                                ...           latest (9 months ago)

67 Versions

  • 10.3.1                                ...           9 months ago
  • 10.3.0                                ...           a year ago
  • 10.2.27                                ...           2 years ago
  • 10.2.26                                ...           2 years ago
  • 10.2.25                                ...           2 years ago
  • 10.2.24                                ...           2 years ago
  • 10.2.23                                ...           2 years ago
  • 10.2.22                                ...           2 years ago
  • 10.2.21                                ...           2 years ago
  • 10.2.20                                ...           2 years ago
  • 10.2.19                                ...           2 years ago
  • 10.2.18                                ...           2 years ago
  • 10.2.17                                ...           2 years ago
  • 10.2.16                                ...           3 years ago
  • 10.2.15                                ...           3 years ago
  • 10.2.14                                ...           3 years ago
  • 10.2.13                                ...           3 years ago
  • 10.2.12                                ...           3 years ago
  • 10.2.11                                ...           3 years ago
  • 10.2.10                                ...           3 years ago
  • 10.2.9                                ...           3 years ago
  • 10.2.8                                ...           3 years ago
  • 10.2.7                                ...           3 years ago
  • 10.2.6                                ...           3 years ago
  • 10.2.5                                ...           3 years ago
  • 10.2.4                                ...           3 years ago
  • 10.2.3                                ...           3 years ago
  • 10.2.2                                ...           3 years ago
  • 10.2.1                                ...           3 years ago
  • 10.2.0                                ...           3 years ago
  • 10.1.6                                ...           3 years ago
  • 10.1.5                                ...           3 years ago
  • 10.1.4                                ...           3 years ago
  • 10.1.3                                ...           3 years ago
  • 10.1.2                                ...           3 years ago
  • 10.1.1                                ...           3 years ago
  • 10.1.0                                ...           3 years ago
  • 10.0.3                                ...           3 years ago
  • 10.0.2                                ...           3 years ago
  • 10.0.1                                ...           3 years ago
  • 10.0.0                                ...           3 years ago
  • 10.0.0-beta.26                                ...           3 years ago
  • 10.0.0-beta.25                                ...           3 years ago
  • 10.0.0-beta.24                                ...           3 years ago
  • 10.0.0-beta.23                                ...           3 years ago
  • 10.0.0-beta.22                                ...           3 years ago
  • 10.0.0-beta.21                                ...           3 years ago
  • 10.0.0-beta.20                                ...           3 years ago
  • 10.0.0-beta.19                                ...           3 years ago
  • 10.0.0-beta.18                                ...           3 years ago
  • 10.0.0-beta.17                                ...           3 years ago
  • 10.0.0-beta.16                                ...           3 years ago
  • 10.0.0-beta.15                                ...           3 years ago
  • 10.0.0-beta.14                                ...           3 years ago
  • 10.0.0-beta.13                                ...           4 years ago
  • 10.0.0-beta.12                                ...           4 years ago
  • 10.0.0-beta.11                                ...           4 years ago
  • 10.0.0-beta.10                                ...           4 years ago
  • 10.0.0-beta.9                                ...           4 years ago
  • 10.0.0-beta.8                                ...           4 years ago
  • 10.0.0-beta.7                                ...           4 years ago
  • 10.0.0-beta.6                                ...           4 years ago
  • 10.0.0-beta.5                                ...           4 years ago
  • 10.0.0-beta.4                                ...           4 years ago
  • 10.0.0-beta.3                                ...           4 years ago
  • 10.0.0-beta.2                                ...           4 years ago
  • 10.0.0-beta.1                                ...           4 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE