Skip to content

Commit b9cb205

Browse files
committed
initial commit
0 parents  commit b9cb205

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

.eslintrc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 6,
4+
"sourceType": "module",
5+
"ecmaFeatures": {
6+
"jsx": true,
7+
"experimentalObjectRestSpread": true
8+
}
9+
},
10+
"rules": {
11+
"semi": 2
12+
}
13+
}

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release/lib
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Goran Lepur
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# react-native-swipe-gestures

index.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'use strict';
2+
3+
import React, {Component} from 'react';
4+
import {View, PanResponder} from 'react-native';
5+
6+
export const swipeDirections = {
7+
SWIPE_UP: 'SWIPE_UP',
8+
SWIPE_DOWN: 'SWIPE_DOWN',
9+
SWIPE_LEFT: 'SWIPE_LEFT',
10+
SWIPE_RIGHT: 'SWIPE_RIGHT'
11+
};
12+
13+
const swipeConfig = {
14+
velocityThreshold: 0.5,
15+
directionalOffsetThreshold: 40
16+
};
17+
18+
function isValidSwipe(velocity, velocityThreshold, directionalOffset, directionalOffsetThreshold) {
19+
return Math.abs(velocity) > velocityThreshold && Math.abs(directionalOffset) < directionalOffsetThreshold;
20+
}
21+
22+
class GestureRecognizer extends Component {
23+
24+
constructor(props, context) {
25+
super(props, context);
26+
this.swipeConfig = Object.assign(swipeConfig, props.config);
27+
}
28+
29+
componentWillReceiveProps(props) {
30+
this.swipeConfig = Object.assign(swipeConfig, props.config);
31+
}
32+
33+
componentWillMount() {
34+
const responderEnd = this._handlePanResponderEnd.bind(this);
35+
this._panResponder = PanResponder.create({ //stop JS beautify collapse
36+
onStartShouldSetPanResponder: this._handleShouldSetPanResponder,
37+
onMoveShouldSetPanResponder: this._handleShouldSetPanResponder,
38+
onPanResponderRelease: responderEnd,
39+
onPanResponderTerminate: responderEnd
40+
});
41+
}
42+
43+
_handleShouldSetPanResponder(evt) {
44+
return evt.nativeEvent.touches.length === 1;
45+
}
46+
47+
_handlePanResponderEnd(evt, gestureState) {
48+
const swipeDirection = this._getSwipeDirection(gestureState);
49+
this._triggerSwipeHandlers(swipeDirection, gestureState);
50+
}
51+
52+
_triggerSwipeHandlers(swipeDirection, gestureState) {
53+
const {onSwipe, onSwipeUp, onSwipeDown, onSwipeLeft, onSwipeRight} = this.props;
54+
const {SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN} = swipeDirections;
55+
onSwipe && onSwipe(swipeDirection, gestureState);
56+
switch (swipeDirection) {
57+
case SWIPE_LEFT:
58+
onSwipeLeft && onSwipeLeft(gestureState);
59+
break;
60+
case SWIPE_RIGHT:
61+
onSwipeRight && onSwipeRight(gestureState);
62+
break;
63+
case SWIPE_UP:
64+
onSwipeUp && onSwipeUp(gestureState);
65+
break;
66+
case SWIPE_DOWN:
67+
onSwipeDown && onSwipeDown(gestureState);
68+
break;
69+
}
70+
}
71+
72+
_getSwipeDirection(gestureState) {
73+
const {SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN} = swipeDirections;
74+
const {dx, dy} = gestureState;
75+
if (this._isValidHorizontalSwipe(gestureState)) {
76+
return (dx > 0)
77+
? SWIPE_RIGHT
78+
: SWIPE_LEFT;
79+
} else if (this._isValidVerticalSwipe(gestureState)) {
80+
return (dy > 0)
81+
? SWIPE_DOWN
82+
: SWIPE_UP;
83+
}
84+
return null;
85+
}
86+
87+
_isValidHorizontalSwipe(gestureState) {
88+
const {vx, dy} = gestureState;
89+
const {velocityThreshold, directionalOffsetThreshold} = this.swipeConfig;
90+
return isValidSwipe(vx, velocityThreshold, dy, directionalOffsetThreshold);
91+
}
92+
93+
_isValidVerticalSwipe(gestureState) {
94+
const {vy, dx} = gestureState;
95+
const {velocityThreshold, directionalOffsetThreshold} = this.swipeConfig;
96+
return isValidSwipe(vy, velocityThreshold, dx, directionalOffsetThreshold);
97+
}
98+
99+
render() {
100+
return (<View {...this.props} {...this._panResponder.panHandlers}/>);
101+
}
102+
};
103+
104+
export default GestureRecognizer;

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "react-native-swipe-gestures",
3+
"version": "1.0.0",
4+
"description": "4-directional swipe gestures for react-native",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"react-native",
11+
"swipe",
12+
"gesture"
13+
],
14+
"author": "Goran Lepur <[email protected]>",
15+
"license": "MIT"
16+
}

0 commit comments

Comments
 (0)