UNPKG

5.53 kBMarkdownView Raw
1React Swipeable
2=========================
3
4React swipe and touch event handler component & hook
5
6[![build status](https://img.shields.io/travis/dogfessional/react-swipeable/master.svg?style=flat-square)](https://travis-ci.org/dogfessional/react-swipeable) [![npm version](https://img.shields.io/npm/v/react-swipeable.svg?style=flat-square)](https://www.npmjs.com/package/react-swipeable) [![npm downloads](https://img.shields.io/npm/dm/react-swipeable.svg?style=flat-square)](https://www.npmjs.com/package/react-swipeable) [![gzip size](https://flat.badgen.net/bundlephobia/minzip/react-swipeable)](https://bundlephobia.com/result?p=react-swipeable)
7
8[![Edit react-swipeable image carousel with hook](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/lrk6955l79?module=%2Fsrc%2FCarousel.js)
9
10[Github Pages Demo](https://dogfessional.github.io/react-swipeable/)
11
12### Version 5 released!
13[React hooks](https://reactjs.org/docs/hooks-reference.html) have been released with [16.8.0](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html) 🎉
14
15v5 of `react-swipeable` includes a hook, `useSwipeable`, that provides the same great functionality as `<Swipeable>`. See the `useSwipeable` hook in action with this [codesandbox](https://codesandbox.io/s/lrk6955l79?module=%2Fsrc%2FCarousel.js).
16
17The component is still included and migration to v5 is straightforward. Please see the [migration doc]((./migration.md)) for more details including more info on the simplified api.
18
19### Installation
20```
21$ npm install react-swipeable
22```
23
24### Api
25Use React-hooks or a Component and set your swipe(d) handlers.
26```js
27import { useSwipeable, Swipeable } from 'react-swipeable'
28```
29
30#### Hook
31```jsx
32const handlers = useSwipeable({ onSwiped: (eventData) => eventHandler, ...config })
33return (<div {...handlers}> You can swipe here </div>)
34```
35Hook use requires **react >= 16.8.0**.
36
37#### Component
38```jsx
39<Swipeable onSwiped={(eventData) => eventHandler} {...config} >
40 You can swipe here!
41</Swipeable>
42```
43
44The Component `<Swipeable>` uses a `<div>` by default under the hood to attach event handlers to.
45
46## Props / Config Options
47
48### Event Handler Props
49
50```
51{
52 onSwiped, // Fired after any swipe
53 onSwipedLeft, // Fired after LEFT swipe
54 onSwipedRight, // Fired after RIGHT swipe
55 onSwipedUp, // Fired after UP swipe
56 onSwipedDown, // Fired after DOWN swipe
57 onSwiping, // Fired during any swipe
58}
59```
60
61### Event data
62All Event Handlers are called with the below event data.
63```
64{
65 event, // source event
66 deltaX, // x offset (current.x - initial.x)
67 deltaY, // y offset (current.y - initial.y)
68 absX, // absolute deltaX
69 absY, // absolute deltaY
70 velocity, // √(absX^2 + absY^2) / time
71 dir, // direction of swipe (Left|Right|Up|Down)
72}
73```
74
75### Configuration Props
76
77```
78{
79 delta: 10, // min distance(px) before a swipe starts
80 preventDefaultTouchmoveEvent: false, // preventDefault on touchmove, *See Details*
81 trackTouch: true, // track touch input
82 trackMouse: false, // track mouse input
83 rotationAngle: 0, // set a rotation angle
84
85 touchHandlerOption: { // overwrite touch handler 3rd argument
86 passive: true|false // defaults opposite of preventDefaultTouchmoveEvent *See Details*
87 },
88}
89```
90
91### Component Specific Props
92
93```
94{
95 nodeName: 'div', // internal component dom node
96 innerRef // access internal component dom node
97}
98```
99
100**None of the props/config options are required.**
101
102### preventDefaultTouchmoveEvent Details
103
104**`preventDefaultTouchmoveEvent`** prevents the browser's [touchmove](https://developer.mozilla.org/en-US/docs/Web/Events/touchmove) event. Use this to stop the browser from scrolling while a user swipes.
105* `e.preventDefault()` is only called when:
106 * `preventDefaultTouchmoveEvent: true`
107 * `trackTouch: true`
108 * the users current swipe has an associated `onSwiping` or `onSwiped` handler/prop
109* if `preventDefaultTouchmoveEvent: true` then `touchHandlerOption` defaults to `{ passive: false }`
110* if `preventDefaultTouchmoveEvent: false` then `touchHandlerOption` defaults to `{ passive: true }`
111
112Example:
113 * If a user is swiping right with `<Swipable onSwipedRight={this.userSwipedRight} preventDefaultTouchmoveEvent={true} >` then `e.preventDefault()` will be called, but if the user was swiping left then `e.preventDefault()` would **not** be called.
114
115Please experiment with the [example](http://dogfessional.github.io/react-swipeable/) to test `preventDefaultTouchmoveEvent`.
116
117#### Older browser support
118If you need to support older browsers that do not have support for `{passive: false}` `addEventListener` 3rd argument, we recommend using [detect-passive-events](https://www.npmjs.com/package/detect-passive-events) package to determine the `touchHandlerOption` prop value.
119
120
121## Development
122
123Initial set up, with `node 10+`, run `npm install`.
124
125Make changes/updates to the `src/index.js` file.
126
127***Please add tests if PR adds/changes functionality.***
128
129#### Verify updates with the examples
130
131Build, run, and test examples locally:
132`npm run start:examples`
133
134After the server starts you can then view the examples page with your changes at `http://localhost:3000`.
135
136You can now make updates/changes to `src/index.js` and webpack will rebuild, then reload the page so you can test your changes!
137
138## License
139
140MIT