@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!
#Yarn
yarn add @use-gesture/react
#NPM
npm install @use-gesture/react
#Yarn
yarn add @use-gesture/vanilla
#NPM
npm install @use-gesture/vanilla
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.
@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 |