react-use-gesture

hoc for receiving gestures

This package is no longer maintained. Please use @use-gesture/react instead
Last updated 6 years ago by drcmda .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install react-use-gesture 
SYNC missed versions from official npm registry.

These demos are real, click them!

npm install react-use-gesture

Ever thought about doing that sidebar pull-out, a view pager, some slider, any gesture on the web basically, and dropped the idea because it's too hard? In that case, this is your lib.

React-use-gesture is a small utility 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.

Api

import useGesture from 'react-use-gesture'

The api is straight forward. You can use React-hooks, render-props or higher-order-components. You bind handlers to your view (done for you if you use render-props or hoc's), and you will receive events when you click/drag/pull/release it. Hooks however are preferred, since they allow gestures to be re-used for more than one view (you can use the same bind() function multiple times!).

// Full config with event handler
const bind = useGesture({ onAction: event => eventHandler, ...config })
return <div {...bind(optionalArgs)} />

// Short cut with event handler (becomes onAction + default config)
const bind = useGesture(event => eventHandler)
return <div {...bind(optionalArgs)} />

// Without onAction it will re-render the component on event changes with fresh props
const [bind, props] = useGesture({ ...config })
return <div {...bind(optionalArgs)} />

Config

{ 
  touch: true,                  // accept touch input
  mouse: true,                  // accept mouse input
  passive: { passive: true },   // event handler 3rd argument input, passive by default
  onAction: undefined           // event => eventHandler, respond to events outside React's render cycle
  onMove: undefined             // event => eventHandler, respond to mousemove/touchmove events within React's render cycle
  onUp: undefined               // event => eventHandler, respond to mouseup/touchend events within React's render cycle
  onDown: undefined             // event => eventHandler, respond to touchstart/mousedown events within React's render cycle
}

Event data

{
  event,                        // source event
  target,                       // dom node
  time,                         // time tag
  initial,                      // click coordinates (vec2)
  xy,                           // page coordinates (vec2)
  previous,                     // previous page coordinates (vec2)
  delta,                        // delta offset (xy - initial) (vec2)
  direction,                    // direction normal (vec2)
  local,                        // delta with book-keeping (vec2)
  velocity,                     // drag momentuum / speed
  distance,                     // delta distance
  down,                         // mouse / touch down
  first,                        // marks first event (mouse / touch down)
  args,                         // arguments optionally passed to bind(a,b,c,d,..)
  temp,                         // arguments optionally returned by onActions eventHandler
  shiftKey,                     // shift pressed (true/false)
}

Examples

React hooks (basic drag/n/drop)

Demo: https://codesandbox.io/embed/l2wy87l28l

In this example we use useGesture's default syntax, where each change ends up re-rendering the component so that we get fresh props that we simply stick into the view. In this case we fetch local off the gesture event, which keeps track of delta positions after release. Deltas are especially important in this lib, because they make it possible to use transitions for positioning, instead of doing complex getBoundingClientRect() calculations to figure out where a node went on the screen.

const [bind, { local: [x, y] }] = useGesture()
return <div {...bind()} style={{ transform: `translate3d(${x}px,${y}px,0)` }} />

React hooks with onAction (and react-spring) (basic pull & release)

Demo: https://codesandbox.io/embed/r24mzvo3q

Re-rendering on every event can be taxing, but it can be avoided. If you are using an animation lib that can update the view outside of React (for instance react-spring or animated), then you can use the onAction syntax, which gives you a callback in which you receive events.

const [{ xy }, set] = useSpring(() => ({ xy: [0, 0] }))
const bind = useGesture(({ down, delta }) => set({ xy: down ? delta : [0, 0] }))
return (
  <animated.div
    {...bind()}
    style={{ transform: xy.interpolate((x, y) => `translate3d(${x}px,${y}px,0)`) }}
  />
)

React hooks with onAction (and react-spring) (decay)

Demo: https://codesandbox.io/embed/zq19y1xr9m

This demo reads out further data like velocity and direction to calculate decay. temp in this case is a simple storage that picks up whatever value you (optionally) return inside the event handler. It's valid as long as the gesture is active. Without this you would need to store the initial xy value somewhere else and conditionally update it when the gesture begins.

const [{ xy }, set] = useSpring(() => ({ xy: [0, 0] }))
const bind = useGesture(({ down, delta, velocity, direction, temp = xy.getValue() }) => {
  set({ 
    xy: add(delta, temp),
    immediate: down,
    config: { velocity: scale(direction, velocity), decay: true }
  })
  return temp
})
return (
  <animated.div
    {...bind()}
    style={{ transform: xy.interpolate((x, y) => `translate3d(${x}px,${y}px,0)`) }}
  />
)

Current Tags

  • 7.0.0-alpha.37                                ...           alpha (5 years ago)
  • 7.0.0-beta.11                                ...           beta (5 years ago)
  • 9.1.3                                ...           latest (4 years ago)
  • 9.1.0-alpha.1                                ...           next (4 years ago)

173 Versions

  • 9.1.0 [deprecated]           ...           4 years ago
  • 9.1.0-alpha.1 [deprecated]           ...           4 years ago
  • 9.0.4 [deprecated]           ...           4 years ago
  • 9.0.3 [deprecated]           ...           4 years ago
  • 9.0.2 [deprecated]           ...           4 years ago
  • 9.0.1 [deprecated]           ...           4 years ago
  • 9.0.0 [deprecated]           ...           4 years ago
  • 9.0.0-beta.14 [deprecated]           ...           4 years ago
  • 9.0.0-beta.13 [deprecated]           ...           4 years ago
  • 9.1.3 [deprecated]           ...           4 years ago
  • 9.1.2 [deprecated]           ...           4 years ago
  • 9.1.1 [deprecated]           ...           4 years ago
  • 9.0.0-beta.12 [deprecated]           ...           4 years ago
  • 9.0.0-beta.11 [deprecated]           ...           4 years ago
  • 9.0.0-beta.10 [deprecated]           ...           4 years ago
  • 8.1.0-beta.9 [deprecated]           ...           4 years ago
  • 8.1.0-beta.8 [deprecated]           ...           4 years ago
  • 8.1.0-beta.7 [deprecated]           ...           4 years ago
  • 8.1.0-beta.6 [deprecated]           ...           4 years ago
  • 8.1.0-beta.5 [deprecated]           ...           4 years ago
  • 8.1.0-beta.4 [deprecated]           ...           4 years ago
  • 8.1.0-beta.3 [deprecated]           ...           4 years ago
  • 8.1.0-beta.2 [deprecated]           ...           4 years ago
  • 8.1.0-beta.1 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.12 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.11 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.10 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.9 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.8 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.7 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.6 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.5 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.4 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.3 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.2 [deprecated]           ...           4 years ago
  • 8.1.0-alpha.1 [deprecated]           ...           4 years ago
  • 8.0.1 [deprecated]           ...           4 years ago
  • 8.0.0 [deprecated]           ...           4 years ago
  • 8.0.0-beta.3 [deprecated]           ...           4 years ago
  • 8.0.0-beta.2 [deprecated]           ...           4 years ago
  • 8.0.0-beta.1 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.10 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.9 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.8 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.7 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.6 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.5 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.4 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.3 [deprecated]           ...           4 years ago
  • 7.0.16 [deprecated]           ...           4 years ago
  • 8.0.0-alpha.2 [deprecated]           ...           5 years ago
  • 8.0.0-alpha.1 [deprecated]           ...           5 years ago
  • 7.0.15 [deprecated]           ...           5 years ago
  • 7.0.14 [deprecated]           ...           5 years ago
  • 7.0.13 [deprecated]           ...           5 years ago
  • 7.0.12 [deprecated]           ...           5 years ago
  • 7.0.11 [deprecated]           ...           5 years ago
  • 7.0.10 [deprecated]           ...           5 years ago
  • 7.0.9 [deprecated]           ...           5 years ago
  • 7.0.8 [deprecated]           ...           5 years ago
  • 7.0.7 [deprecated]           ...           5 years ago
  • 7.0.6 [deprecated]           ...           5 years ago
  • 7.0.5 [deprecated]           ...           5 years ago
  • 7.0.4 [deprecated]           ...           5 years ago
  • 7.0.3 [deprecated]           ...           5 years ago
  • 7.0.2 [deprecated]           ...           5 years ago
  • 7.0.1 [deprecated]           ...           5 years ago
  • 7.0.0 [deprecated]           ...           5 years ago
  • 7.0.0-beta.11 [deprecated]           ...           5 years ago
  • 7.0.0-beta.10 [deprecated]           ...           5 years ago
  • 7.0.0-beta.9 [deprecated]           ...           5 years ago
  • 7.0.0-beta.8 [deprecated]           ...           5 years ago
  • 7.0.0-beta.7 [deprecated]           ...           5 years ago
  • 7.0.0-beta.6 [deprecated]           ...           5 years ago
  • 7.0.0-beta.5 [deprecated]           ...           5 years ago
  • 7.0.0-beta.4 [deprecated]           ...           5 years ago
  • 7.0.0-beta.3 [deprecated]           ...           5 years ago
  • 7.0.0-beta.2 [deprecated]           ...           5 years ago
  • 7.0.0-beta.1 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.37 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.36 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.35 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.34 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.33 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.32 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.31 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.30 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.29 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.28 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.27 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.26 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.25 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.24 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.23 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.22 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.21 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.20 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.19 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.18 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.17 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.16 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.15 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.14 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.13 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.12 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.11 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.10 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.9 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.8 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.7 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.6 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.5 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.4 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.3 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.2 [deprecated]           ...           5 years ago
  • 7.0.0-alpha.1 [deprecated]           ...           5 years ago
  • 6.0.14 [deprecated]           ...           5 years ago
  • 6.0.13 [deprecated]           ...           5 years ago
  • 6.0.12 [deprecated]           ...           5 years ago
  • 6.0.11 [deprecated]           ...           5 years ago
  • 6.0.10 [deprecated]           ...           5 years ago
  • 6.0.9 [deprecated]           ...           5 years ago
  • 6.0.8 [deprecated]           ...           5 years ago
  • 6.0.7 [deprecated]           ...           5 years ago
  • 6.0.6 [deprecated]           ...           5 years ago
  • 6.0.5 [deprecated]           ...           5 years ago
  • 6.0.4 [deprecated]           ...           5 years ago
  • 6.0.3 [deprecated]           ...           5 years ago
  • 6.0.2 [deprecated]           ...           5 years ago
  • 6.0.2-beta.2 [deprecated]           ...           5 years ago
  • 6.0.2-beta.1 [deprecated]           ...           5 years ago
  • 6.0.1 [deprecated]           ...           5 years ago
  • 6.0.0 [deprecated]           ...           5 years ago
  • 6.0.0-alpha.6 [deprecated]           ...           5 years ago
  • 6.0.0-alpha.5 [deprecated]           ...           5 years ago
  • 6.0.0-beta.4 [deprecated]           ...           5 years ago
  • 6.0.0-beta.3 [deprecated]           ...           5 years ago
  • 6.0.0-beta.2 [deprecated]           ...           5 years ago
  • 6.0.0-beta.1 [deprecated]           ...           5 years ago
  • 5.2.4 [deprecated]           ...           5 years ago
  • 5.2.3 [deprecated]           ...           5 years ago
  • 5.2.2 [deprecated]           ...           5 years ago
  • 5.2.1 [deprecated]           ...           5 years ago
  • 5.2.0 [deprecated]           ...           5 years ago
  • 5.1.3 [deprecated]           ...           5 years ago
  • 5.1.3-beta.2 [deprecated]           ...           5 years ago
  • 5.1.3-beta.1 [deprecated]           ...           5 years ago
  • 5.2.0-beta.1 [deprecated]           ...           5 years ago
  • 5.1.2 [deprecated]           ...           6 years ago
  • 5.1.1 [deprecated]           ...           6 years ago
  • 5.1.0-beta.9 [deprecated]           ...           6 years ago
  • 5.1.0-beta.8 [deprecated]           ...           6 years ago
  • 5.1.0-beta.7 [deprecated]           ...           6 years ago
  • 5.1.0-beta.6 [deprecated]           ...           6 years ago
  • 5.0.2 [deprecated]           ...           6 years ago
  • 5.1.0-beta.5 [deprecated]           ...           6 years ago
  • 5.1.0-beta.4 [deprecated]           ...           6 years ago
  • 5.1.0-beta.3 [deprecated]           ...           6 years ago
  • 5.1.0-beta.2 [deprecated]           ...           6 years ago
  • 5.1.0-beta.1 [deprecated]           ...           6 years ago
  • 5.1.0 [deprecated]           ...           6 years ago
  • 5.0.1 [deprecated]           ...           6 years ago
  • 5.0.0 [deprecated]           ...           6 years ago
  • 5.0.0-beta.8 [deprecated]           ...           6 years ago
  • 5.0.0-beta.7 [deprecated]           ...           6 years ago
  • 5.0.0-beta.6 [deprecated]           ...           6 years ago
  • 5.0.0-beta.5 [deprecated]           ...           6 years ago
  • 5.0.0-beta.4 [deprecated]           ...           6 years ago
  • 5.0.0-beta.3 [deprecated]           ...           6 years ago
  • 5.0.0-beta.2 [deprecated]           ...           6 years ago
  • 5.0.0-beta.1 [deprecated]           ...           6 years ago
  • 5.0.0-beta.0 [deprecated]           ...           6 years ago
  • 1.0.0 [deprecated]           ...           6 years ago
Maintainers (1)
Downloads
Total 169
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE