MCPcopy Index your code
hub / github.com/pmndrs/use-gesture

github.com/pmndrs/use-gesture @10.3.1 sqlite

repository ↗ · DeepWiki ↗ · release 10.3.1 ↗
335 symbols 818 edges 148 files 23 documented · 7%
README

@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

React

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' }} />
}

Vanilla javascript





// 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...

Extension points exported contracts — how you extend this code

PinchState (Interface)
(no doc) [2 implementers]
packages/core/src/types/state.ts
MoveGestureConstructor (Interface)
(no doc)
packages/vanilla/src/MoveGesture.ts
Props (Interface)
(no doc)
test/components/Api.tsx
WheelEngine (Interface)
(no doc) [1 implementers]
packages/core/src/engines/WheelEngine.ts
MoveGesture (Interface)
(no doc)
packages/vanilla/src/MoveGesture.ts
Props (Interface)
(no doc)
test/components/types.ts
DOMHandlers (Interface)
(no doc)
packages/core/src/types/utils.ts
DragGestureConstructor (Interface)
(no doc)
packages/vanilla/src/DragGesture.ts

Core symbols most depended-on inside this repo

pointerDown
called by 50
packages/core/src/engines/DragEngine.ts
pointerUp
called by 45
packages/core/src/engines/DragEngine.ts
pointerMove
called by 43
packages/core/src/engines/DragEngine.ts
useDrag
called by 41
packages/react/src/useDrag.ts
parse
called by 34
packages/core/src/config/resolver.ts
add
called by 19
packages/core/src/EventStore.ts
useGesture
called by 16
packages/react/src/useGesture.ts
registerAction
called by 12
packages/core/src/actions.ts

Shape

Function 213
Method 79
Class 22
Interface 21

Languages

TypeScript100%

Modules by API surface

packages/core/src/engines/PinchEngine.ts25 symbols
packages/core/src/engines/Engine.ts21 symbols
packages/core/src/engines/DragEngine.ts21 symbols
packages/core/src/utils/events.ts18 symbols
packages/core/src/config/dragConfigResolver.ts13 symbols
packages/core/src/config/commonConfigResolver.ts12 symbols
packages/core/src/Controller.ts12 symbols
packages/core/src/utils/maths.ts9 symbols
documentation/pages/docs/code/examples.js9 symbols
packages/core/src/engines/CoordinatesEngine.ts8 symbols
packages/core/src/engines/WheelEngine.ts7 symbols
packages/core/src/engines/MoveEngine.ts7 symbols

Dependencies from manifests, versioned

@babel/core7.22.19 · 1×
@babel/plugin-syntax-flow7.22.5 · 1×
@babel/plugin-transform-class-properties7.22.5 · 1×
@babel/plugin-transform-react-jsx7.22.15 · 1×
@babel/preset-env7.22.15 · 1×
@babel/preset-react7.22.15 · 1×
@babel/preset-typescript7.22.15 · 1×
@changesets/cli2.26.2 · 1×
@leva-ui/plugin-spring* · 1×
@preconstruct/cli2.8.1 · 1×
@reach/tabs0.16.4 · 1×
@react-spring/three9.4.5 · 1×

For agents

$ claude mcp add use-gesture \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact