Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/react-spring/react-use-gesture
๐Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
https://github.com/react-spring/react-use-gesture
drag gestures hooks pinch react scroll wheel zoom
Last synced: 3 months ago
JSON representation
๐Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
- Host: GitHub
- URL: https://github.com/react-spring/react-use-gesture
- Owner: pmndrs
- License: mit
- Created: 2018-03-23T15:37:58.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-04-02T20:46:17.000Z (7 months ago)
- Last Synced: 2024-05-01T11:29:10.775Z (6 months ago)
- Topics: drag, gestures, hooks, pinch, react, scroll, wheel, zoom
- Language: TypeScript
- Homepage: https://use-gesture.netlify.app
- Size: 178 MB
- Stars: 8,676
- Watchers: 41
- Forks: 296
- Open Issues: 27
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @use-gesture
[![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)
@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](https://github.com/pmndrs/react-spring), though you can most certainly use any other.
The demos are real click them!
## Installation
### React
```bash
#Yarn
yarn add @use-gesture/react#NPM
npm install @use-gesture/react
```### Vanilla javascript
```bash
#Yarn
yarn add @use-gesture/vanilla#NPM
npm install @use-gesture/vanilla
```### [Full documentation website](https://use-gesture.netlify.com)
- [Available Gestures](https://use-gesture.netlify.com/docs/gestures)
- [Gesture State](https://use-gesture.netlify.com/docs/state)
- [Gesture Options](https://use-gesture.netlify.com/docs/options)
- [FAQ](https://use-gesture.netlify.com/docs/faq)### Simple example
React
```jsx
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
}
```Vanilla javascript
```html
``````js
// 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`](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**.
### 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...](https://use-gesture.netlify.app/)