Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make preventing the default touch event optional. #32

Merged
merged 4 commits into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var SampleComponent = React.createClass({
onSwipedRight={this.swipedRight}
onSwipedDown={this.swipedDown}
onSwipedLeft={this.swipedLeft}
onSwiped={this.handleSwipeAction}>
onSwiped={this.handleSwipeAction}
preventDefaultTouchmoveEvent={false}>
<div>
This element can be swiped
</div>
Expand Down Expand Up @@ -52,6 +53,8 @@ as well as the x distance, + or -, from where the swipe started to where it ende

`delta` is the amount of px before we start firing events. Also effects how far `onSwipedUp`, `onSwipedRight`, `onSwipedDown`, and `onSwipedLeft` need to be before they fire events. The default value is 10.

`preventDefaultTouchmoveEvent` is whether to prevent the browser's `[touchmove](https://developer.mozilla.org/en-US/docs/Web/Events/touchmove)` event. Sometimes you would like the target to scroll natively. The default value is `true`.

### PropTypes

```
Expand All @@ -66,7 +69,8 @@ as well as the x distance, + or -, from where the swipe started to where it ende
onSwipedDown: React.PropTypes.func,
onSwipedLeft: React.PropTypes.func,
flickThreshold: React.PropTypes.number,
delta: React.PropTypes.number
delta: React.PropTypes.number,
preventDefaultTouchmoveEvent: React.PropTypes.bool
```

## Development
Expand Down
20 changes: 17 additions & 3 deletions examples/app/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const initialState = {
swiping: false,
swiped: false,
swipingDirection: '',
swipedDirection: '',
swipedDirection: ''
};
const initialStateSwipeable = {
flickThreshold: '0.6',
delta: '10',
preventDefaultTouchmoveEvent: true
};

export default class Main extends Component {
Expand Down Expand Up @@ -68,6 +69,7 @@ export default class Main extends Component {
swipedDirection,
flickThreshold,
delta,
preventDefaultTouchmoveEvent
} = this.state;
const boundSwipes = getBoundSwipes(this);
const isFlickThresholdNumber = !(isNaN(flickThreshold) || flickThreshold === '');
Expand All @@ -91,11 +93,14 @@ export default class Main extends Component {
onSwiping={(...args)=>this.onSwiping(...args)}
onSwiped={(...args)=>this.onSwiped(...args)}
flickThreshold={flickThresholdNum}
delta={deltaNum}>
delta={deltaNum}
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent}>
<div className="callout"
onTouchStart={()=>this.resetState()}
style={{height: '150px'}}>
style={{height: '150px', overflowY: 'scroll'}}>
<h5>Swipe inside here...</h5>
<p>Here is some filler to make it scrollable...</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</Swipeable>
</div>
Expand Down Expand Up @@ -127,6 +132,15 @@ export default class Main extends Component {
onChange={(e)=>this.updateValue('flickThreshold', getVal(e))} value={flickThreshold}/>
</td>
</tr>
<tr>
<td>preventDefaultTouchmoveEvent:</td>
<td>
<input
type="checkbox"
checked={preventDefaultTouchmoveEvent}
onChange={(e)=>this.updateValue('preventDefaultTouchmoveEvent', e.target.checked)}/>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
8 changes: 5 additions & 3 deletions js/Swipeable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var Swipeable = React.createClass({displayName: "Swipeable",
onSwipedDown: React.PropTypes.func,
onSwipedLeft: React.PropTypes.func,
flickThreshold: React.PropTypes.number,
delta: React.PropTypes.number
delta: React.PropTypes.number,
preventDefaultTouchmoveEvent: React.PropTypes.bool
},

getInitialState: function () {
Expand All @@ -28,7 +29,8 @@ var Swipeable = React.createClass({displayName: "Swipeable",
getDefaultProps: function () {
return {
flickThreshold: 0.6,
delta: 10
delta: 10,
preventDefaultTouchmoveEvent: true
}
},

Expand Down Expand Up @@ -106,7 +108,7 @@ var Swipeable = React.createClass({displayName: "Swipeable",

this.setState({ swiping: true })

if (cancelPageSwipe) {
if (cancelPageSwipe && this.props.preventDefaultTouchmoveEvent) {
e.preventDefault()
}
},
Expand Down
8 changes: 5 additions & 3 deletions jsx/Swipeable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var Swipeable = React.createClass({
onSwipedDown: React.PropTypes.func,
onSwipedLeft: React.PropTypes.func,
flickThreshold: React.PropTypes.number,
delta: React.PropTypes.number
delta: React.PropTypes.number,
preventDefaultTouchmoveEvent: React.PropTypes.bool
},

getInitialState: function () {
Expand All @@ -28,7 +29,8 @@ var Swipeable = React.createClass({
getDefaultProps: function () {
return {
flickThreshold: 0.6,
delta: 10
delta: 10,
preventDefaultTouchmoveEvent: true
}
},

Expand Down Expand Up @@ -106,7 +108,7 @@ var Swipeable = React.createClass({

this.setState({ swiping: true })

if (cancelPageSwipe) {
if (cancelPageSwipe && this.props.preventDefaultTouchmoveEvent) {
e.preventDefault()
}
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-swipeable",
"version": "3.1.0",
"version": "3.2.0",
"description": "Swipe bindings for react",
"main": "js/Swipeable.js",
"scripts": {
Expand Down