+1
日增长
+5
周增长
+9
月增长
9,629
stars
337
forks
39
watchers
218
prs
342
issues
140
releases
README
@use-gesture
!npm bundle size !NPM
@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
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
- Available Gestures
- Gesture State
- Gesture Options
- 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 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...Top issues
#401Improve pinch algorithm for wheel-based gestures19 comments · 2021-11-29#241Add tap, long tap and double tap gesture18 comments · 2020-11-30#614onDragStart direction is 0 instead of 1 or -111 comments · 2023-08-10#593Combining filterTaps and preventScroll options on useDrag is breaking the onclick event on elements inside the drag container11 comments · 2023-04-05#26React Native support10 comments · 2019-01-09#676Swiping too hard causes the whole viewport unclickable8 comments · 2024-07-21#651Documentation website is broken (in firefox)8 comments · 2024-03-08#641onPinch breaking on control + scroll8 comments · 2023-11-20
Discussions
#700How to long press?0 comments · 2026-01-16#698Usewheel add index of event similar to boolean first and last0 comments · 2025-11-23#689Is this lib no longer being maintained?1 comments · 2025-04-07#680How to stop drag gesture after enabled set to false0 comments · 2024-08-27#678Multiple parallel gestures in a multi touch context0 comments · 2024-08-06#677What is the usage case for the 'canceled' pinch or drag state property?0 comments · 2024-07-28#673cancel() doesn't reset offset?1 comments · 2024-07-03#672how to implement natural pinch scaling of elements inside SVG0 comments · 2024-06-30
Latest release
@use-gesture/[email protected] @use-gesture/[email protected] 2024-03-21Patch Changes
- Updated dependencies [6f6f4a5b4]
- @use-gesture/[email protected]