1 | # @use-gesture
|
---|
2 |
|
---|
3 | [![npm (tag)](https://img.shields.io/npm/v/@use-gesture/react?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/@use-gesture/react) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@use-gesture/react?style=flat&colorA=000000&colorB=000000) ![NPM](https://img.shields.io/npm/l/@use-gesture/react?style=flat&colorA=000000&colorB=000000) [![Discord Shield](https://img.shields.io/discord/740090768164651008?style=flat&colorA=000000&colorB=000000&label=discord&logo=discord&logoColor=ffffff)](https://discord.gg/poimandres)
|
---|
4 |
|
---|
5 | @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.
|
---|
6 |
|
---|
7 | You can use it stand-alone, but to make the most of it you should combine it with an animation library like [react-spring](https://github.com/pmndrs/react-spring), though you can most certainly use any other.
|
---|
8 |
|
---|
9 | <p align="middle">
|
---|
10 | <a href="https://codesandbox.io/s/github/pmndrs/use-gesture/tree/main/demo/src/sandboxes/draggable-list"><img src="https://i.imgur.com/qLKJod3.gif" width="400"/></a>
|
---|
11 | <a href="https://codesandbox.io/s/github/pmndrs/use-gesture/tree/main/demo/src/sandboxes/cards-stack"><img src="https://i.imgur.com/H6nXQEq.gif" width="400"/></a>
|
---|
12 | <a href="https://codesandbox.io/s/github/pmndrs/use-gesture/tree/main/demo/src/sandboxes/action-sheet"><img src="https://i.imgur.com/THKPrmR.gif" width="400"/></a>
|
---|
13 | <a href="https://codesandbox.io/s/github/pmndrs/use-gesture/tree/main/demo/src/sandboxes/infinite-slideshow"><img src="https://i.imgur.com/cuOfqST.gif" width="400"/></a>
|
---|
14 | <a href="https://codesandbox.io/s/github/pmndrs/use-gesture/tree/main/demo/src/sandboxes/viewpager"><img src="https://i.imgur.com/iwZOfT9.gif" width="400"/></a>
|
---|
15 | <a href="https://codesandbox.io/s/github/pmndrs/use-gesture/tree/main/demo/src/sandboxes/card-zoom"><img src="https://i.imgur.com/Walt1Ip.gif" width="400"/></a>
|
---|
16 | </p>
|
---|
17 |
|
---|
18 | <p align="middle"><i>The demos are real click them!</i></p>
|
---|
19 |
|
---|
20 | ## Installation
|
---|
21 |
|
---|
22 | ### React
|
---|
23 |
|
---|
24 | ```bash
|
---|
25 | #Yarn
|
---|
26 | yarn add @use-gesture/react
|
---|
27 |
|
---|
28 | #NPM
|
---|
29 | npm install @use-gesture/react
|
---|
30 | ```
|
---|
31 |
|
---|
32 | ### Vanilla javascript
|
---|
33 |
|
---|
34 | ```bash
|
---|
35 | #Yarn
|
---|
36 | yarn add @use-gesture/vanilla
|
---|
37 |
|
---|
38 | #NPM
|
---|
39 | npm install @use-gesture/vanilla
|
---|
40 | ```
|
---|
41 |
|
---|
42 | ### [Full documentation website](https://use-gesture.netlify.com)
|
---|
43 |
|
---|
44 | - [Available Gestures](https://use-gesture.netlify.com/docs/gestures)
|
---|
45 | - [Gesture State](https://use-gesture.netlify.com/docs/state)
|
---|
46 | - [Gesture Options](https://use-gesture.netlify.com/docs/options)
|
---|
47 | - [FAQ](https://use-gesture.netlify.com/docs/faq)
|
---|
48 |
|
---|
49 | ### Simple example
|
---|
50 |
|
---|
51 | <p align="middle">
|
---|
52 | <a href="https://codesandbox.io/s/github/pmndrs/use-gesture/tree/main/demo/src/sandboxes/gesture-simplest"><img src="https://i.imgur.com/AMzsEi3.gif" width="400"/></a>
|
---|
53 | </p>
|
---|
54 |
|
---|
55 | <details>
|
---|
56 | <summary>React</summary>
|
---|
57 |
|
---|
58 | ```jsx
|
---|
59 | import { useSpring, animated } from '@react-spring/web'
|
---|
60 | import { useDrag } from '@use-gesture/react'
|
---|
61 |
|
---|
62 | function Example() {
|
---|
63 | const [{ x, y }, api] = useSpring(() => ({ x: 0, y: 0 }))
|
---|
64 |
|
---|
65 | // Set the drag hook and define component movement based on gesture data.
|
---|
66 | const bind = useDrag(({ down, movement: [mx, my] }) => {
|
---|
67 | api.start({ x: down ? mx : 0, y: down ? my : 0 })
|
---|
68 | })
|
---|
69 |
|
---|
70 | // Bind it to a component.
|
---|
71 | return <animated.div {...bind()} style={{ x, y, touchAction: 'none' }} />
|
---|
72 | }
|
---|
73 | ```
|
---|
74 |
|
---|
75 | </details>
|
---|
76 |
|
---|
77 | <details>
|
---|
78 | <summary>Vanilla javascript</summary>
|
---|
79 |
|
---|
80 | ```html
|
---|
81 | <!-- index.html -->
|
---|
82 | <div id="drag" />
|
---|
83 | ```
|
---|
84 |
|
---|
85 | ```js
|
---|
86 | // script.js
|
---|
87 | const el = document.getElementById('drag')
|
---|
88 | const gesture = new DragGesture(el, ({ active, movement: [mx, my] }) => {
|
---|
89 | setActive(active)
|
---|
90 | anime({
|
---|
91 | targets: el,
|
---|
92 | translateX: active ? mx : 0,
|
---|
93 | translateY: active ? my : 0,
|
---|
94 | duration: active ? 0 : 1000
|
---|
95 | })
|
---|
96 | })
|
---|
97 |
|
---|
98 | // when you want to remove the listener
|
---|
99 | gesture.destroy()
|
---|
100 | ```
|
---|
101 |
|
---|
102 | </details>
|
---|
103 |
|
---|
104 | The example above makes a `div` draggable so that it follows your mouse on drag, and returns to its initial position on release.
|
---|
105 |
|
---|
106 | **Make sure you always set [`touchAction`](https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action) on a draggable element to prevent glitches with the browser native scrolling on touch devices**.
|
---|
107 |
|
---|
108 | ### Available hooks
|
---|
109 |
|
---|
110 | @use-gesture/react exports several hooks that can handle different gestures:
|
---|
111 |
|
---|
112 | | Hook | Description |
|
---|
113 | | ------------ | ------------------------------------------ |
|
---|
114 | | `useDrag` | Handles the drag gesture |
|
---|
115 | | `useMove` | Handles mouse move events |
|
---|
116 | | `useHover` | Handles mouse enter and mouse leave events |
|
---|
117 | | `useScroll` | Handles scroll events |
|
---|
118 | | `useWheel` | Handles wheel events |
|
---|
119 | | `usePinch` | Handles the pinch gesture |
|
---|
120 | | `useGesture` | Handles multiple gestures in one hook |
|
---|
121 |
|
---|
122 | #### [More on the full documentation website...](https://use-gesture.netlify.app/)
|
---|