All Projects → bmcmahen → React Gesture Responder

bmcmahen / React Gesture Responder

a gesture responder system for your react application

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Gesture Responder

Any Touch
👋 手势库, 按需2kb~5kb, 兼容PC / 移动端
Stars: ✭ 567 (+472.73%)
Mutual labels:  gesture, touch
Hovertouchview
Stimulate Apple's Force Touch or 3D Touch on Android App with Hover Gesture
Stars: ✭ 192 (+93.94%)
Mutual labels:  gesture, touch
Zingtouch
A JavaScript touch gesture detection library for the modern web
Stars: ✭ 2,019 (+1939.39%)
Mutual labels:  gesture, touch
gesto
You can set up drag, pinch events in any browser.
Stars: ✭ 47 (-52.53%)
Mutual labels:  touch, gesture
Better Gesture
A gesture library use for pc, mobile, vue, and mini programs
Stars: ✭ 419 (+323.23%)
Mutual labels:  gesture, touch
Alloyfinger
Super tiny size multi-touch gestures library for the web.    You can touch this →
Stars: ✭ 3,244 (+3176.77%)
Mutual labels:  gesture, touch
gestures
A library for normalized events and gesture for desktop and mobile.
Stars: ✭ 31 (-68.69%)
Mutual labels:  touch, gesture
React Native Swipe Gestures
4-directional swipe gestures for react-native
Stars: ✭ 471 (+375.76%)
Mutual labels:  gesture, touch
Touchkit
基于mtouch封装的,更便于业务使用的贴纸手势库
Stars: ✭ 48 (-51.52%)
Mutual labels:  gesture, touch
Ngx Pinch Zoom
Module provides for image zooming and positioning with use of gestures on a touch screen.
Stars: ✭ 85 (-14.14%)
Mutual labels:  touch
Swifthook
A library to hook methods in Swift and Objective-C.
Stars: ✭ 93 (-6.06%)
Mutual labels:  hooks
Forcetouch
Simple implementation of ForceTouch on Android
Stars: ✭ 84 (-15.15%)
Mutual labels:  gesture
React Easy Swipe
Easy handler for common swipe operations
Stars: ✭ 85 (-14.14%)
Mutual labels:  touch
Platform Samples
A public place for all platform sample projects.
Stars: ✭ 1,328 (+1241.41%)
Mutual labels:  hooks
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (-14.14%)
Mutual labels:  hooks
Use Substate
🍙 Lightweight (<600B minified + gzipped) React Hook to subscribe to a subset of your single app state.
Stars: ✭ 97 (-2.02%)
Mutual labels:  hooks
React Selector Hooks
Collection of hook-based memoized selector factories for declarations outside of render.
Stars: ✭ 84 (-15.15%)
Mutual labels:  hooks
Hook Flow
A flowchart that explains the new lifecycle of a Hooks component. https://dwe.st/hf
Stars: ✭ 1,246 (+1158.59%)
Mutual labels:  hooks
React Swipeable
React swipe event handler hook
Stars: ✭ 1,348 (+1261.62%)
Mutual labels:  touch
Vmtouch
Portable file system cache diagnostics and control
Stars: ✭ 1,341 (+1254.55%)
Mutual labels:  touch
A demo showing a ball being pulled around, released, and animating back into place.

react-gesture-responder

npm package Follow on Twitter

react-gesture-responder offers a gesture responder system for your react application. It's heavily inspired by react-native's pan-responder. It's built for use in Sancho-UI.

View the demo and website here.

You can also read how to create a swipe to like feature using react-gesture-responder.

Features

  • The ability to delegate between multiple overlapping gestures. This means that you can embed gesture responding views within eachother and provide negotiation strategies between them.
  • Simple kinematics for gesture based animations. Values including distance, velocity, delta, and direction are provided through gesture callbacks.
  • Integrates well with react-spring to create performant animations.
  • Built with react-gesture-responder: react-gesture-view, touchable-hook, react-grid-dnd, react-gesture-gallery, react-gesture-stack, sancho-ui.

Getting started

Install into your react project using yarn or npm.

yarn add react-gesture-responder

The example below demonstrates how it can be used in conjunction with react-spring.

import { useSpring, animated } from "react-spring";
import { useGestureResponder } from "react-gesture-responder";

function Draggable() {
  const [{ xy }, set] = useSpring(() => ({
    xy: [0, 0]
  }));

  const { bind } = useGestureResponder({
    onStartShouldSet: () => true,
    onRelease: onEnd,
    onTerminate: onEnd,
    onMove: ({ delta }) => {
      set({
        xy: delta,
        immediate: true
      });
    }
  });

  function onEnd() {
    set({ xy: [0, 0], immediate: false });
  }

  return (
    <animated.div
      style={{
        transform: xy.interpolate((x, y) => `translate3d(${x}px, ${y}px, 0)`)
      }}
      {...bind}
    />
  );
}

API

Only one responder can be active at any given time. The useGesture hook provides callbacks which allow you to implement a negotiation strategy between competing views.

  • onStartShouldSet: (state, e) => boolean - Should the view become the responder upon first touch?
  • onMoveShouldSet: (state, e) => boolean - This is called during any gesture movement on the view. You can return true to claim the responder for that view.
  • onStartShouldSetCapture: (state, e) => boolean - The same as above, but using event capturing instead of bubbling. Useful if you want a parent view to capture the responder prior to children.
  • onMoveShouldSetCapture: (state, e) => boolean.
  • onTerminationRequest: (state) => boolean. - Should we allow the responder to be claimed by another view? This is only called when a parent onMoveShouldSet returns true. By default, it returns true.

By default, if a parent and child both return true from onStartShouldSet the child element will claim the responder.

Once a responder is claimed, other callbacks can be used to provide visual feedback to the user.

  • onGrant: (state, e) => void - called when the view claims the responder, typically corresponding with mousedown or touchstart events.
  • onMove: (state, e) => void
  • onRelease: (state, e) => void - corresponds with mouseup or touchend events.
  • onTerminate: (state) => void - called when the responder is claimed by another view.
const { bind } = useGestureResponder(
  {
    onStartShouldSet: state => true,
    onStartShouldSetCapture: state => false,
    onMoveShouldSet: state => false,
    onMoveShouldSetCapture: state => false,
    onTerminationRequest: state => true,
    onGrant: state => {},
    onRelease: state => {},
    onTerminate: state => {},
    onMove: state => {}
  },
  {
    uid: "a-unique-id",
    enableMouse: true
  }
);

state contains the following values:

export interface StateType {
  time: number;
  xy: [number, number];
  delta: [number, number];
  initial: [number, number];
  previous: [number, number];
  direction: [number, number];
  initialDirection: [number, number];
  local: [number, number];
  lastLocal: [number, number];
  velocity: number;
  distance: number;
}

Prior art

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].