Skip to content

Commit 3932868

Browse files
authored
Merge pull request #131 from dogfessional/preventdefault-touch-ios-fix2
Preventdefault touch ios fix
2 parents e543107 + 7f4193a commit 3932868

11 files changed

+223
-361
lines changed

.eslintrc

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
"node": true
2121
},
2222
"rules": {
23+
"prettier/prettier": [
24+
"error",
25+
{
26+
"singleQuote": true,
27+
"semi": false,
28+
"printWidth": 100
29+
}
30+
],
2331
"valid-jsdoc": 2,
2432
"react/jsx-uses-react": 1,
2533
"react/jsx-no-undef": 2,

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"printWidth": 100,
23
"semi": false,
34
"singleQuote": true,
45
"tabWidth": 2

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# 5.1.0
2+
* Fix for `preventDefaultTouchmoveEvent` in safari [issue #127](https://github.com/dogfessional/react-swipeable/issues/127) and [PR #131](https://github.com/dogfessional/react-swipeable/pull/131)
3+
* Thank you [@JiiB](https://github.com/JiiB) and [@bhj](https://github.com/bhj)!
4+
* use `ref` callback for both `<Swipeable>` and `useSwipeable` to attach all touch event handlers
5+
* `useSwipeable`'s returned `handlers` now contains a ref callback
6+
* Please see disscusion and comments in both [#127](https://github.com/dogfessional/react-swipeable/issues/127) and [#131](https://github.com/dogfessional/react-swipeable/issues/127) for more details and info.
7+
* fix avoids the `passive: true` issue from chrome document event listeners
8+
* fix avoids bug on safari where the `touchmove` event listener needs to be attached before a `touchstart` in order to be able to call `e.preventDefault`
9+
* removed `touchHandlerOption` prop
10+
* fix above deprecates this prop
11+
112
# 5.0.0
213
* Introduce react hook, `useSwipeable`
314
* Core rewrite to simplify api and trim down bundled size

README.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { useSwipeable, Swipeable } from 'react-swipeable'
3232
const handlers = useSwipeable({ onSwiped: (eventData) => eventHandler, ...config })
3333
return (<div {...handlers}> You can swipe here </div>)
3434
```
35-
Hook use requires **react >= 16.8.0**.
35+
Spread `handlers` onto the element you wish to track swipes inside of. [Details below](#hook-details).
3636

3737
#### Component
3838
```jsx
@@ -81,42 +81,39 @@ All Event Handlers are called with the below event data.
8181
trackTouch: true, // track touch input
8282
trackMouse: false, // track mouse input
8383
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-
},
8884
}
8985
```
9086

9187
### Component Specific Props
9288

9389
```
9490
{
95-
nodeName: 'div', // internal component dom node
96-
innerRef // access internal component dom node
91+
nodeName: 'div', // internally rendered component dom node
92+
innerRef // callback ref for internal component dom node
9793
}
9894
```
9995

10096
**None of the props/config options are required.**
10197

98+
### Hook details
99+
- Hook use requires **react >= 16.8.0**
100+
- The props contained in `handlers` are currently `ref` and `onMouseDown`
101+
- Please spread `handlers` as the props contained in it could change as react improves event listening capabilities
102+
- See [#127](https://github.com/dogfessional/react-swipeable/issues/127) for some more context
103+
102104
### preventDefaultTouchmoveEvent Details
103105

104106
**`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.
105107
* `e.preventDefault()` is only called when:
106108
* `preventDefaultTouchmoveEvent: true`
107109
* `trackTouch: true`
108110
* 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 }`
111111

112112
Example:
113113
* 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.
114114

115115
Please experiment with the [example](http://dogfessional.github.io/react-swipeable/) to test `preventDefaultTouchmoveEvent`.
116116

117-
#### Older browser support
118-
If 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-
120117

121118
## Development
122119

examples/app/FeatureTestConsole.js

+48-16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const initialStateSwipeable = {
2727
rotationAngle: 0,
2828
};
2929
const initialStateApplied = {
30+
showHook: true,
31+
showComponent: true,
32+
showOnSwipeds: false,
3033
onSwipingApplied: true,
3134
onSwipedApplied: true,
3235
onSwipedLeftApplied: true,
@@ -100,6 +103,9 @@ export default class Main extends Component {
100103
swipingDirection,
101104
swipedDirection,
102105
delta,
106+
showHook,
107+
showComponent,
108+
showOnSwipeds,
103109
onSwipingApplied,
104110
onSwipedApplied,
105111
persistEvent,
@@ -129,38 +135,38 @@ export default class Main extends Component {
129135
<div className="row" id="FeatureTestConsole">
130136
<div className="small-12 column">
131137
<h5><strong>Test react-swipeable features.</strong></h5>
132-
<Swipeable
138+
{showComponent && <Swipeable
133139
{...boundSwipes}
134140
{...swipeableDirProps}
135141
delta={deltaNum}
136142
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent}
137143
trackTouch={trackTouch}
138144
trackMouse={trackMouse}
139145
rotationAngle={rotationAngleNum}
140-
className="callout"
146+
className="callout classComponent"
141147
style={swipeableStyle}>
142148
<div onTouchStart={()=>this.resetState()}>
143149
<h5>Component - Swipe inside here to test</h5>
144150
<p>See output below and check the console for 'onSwiping' and 'onSwiped' callback output(open dev tools)</p>
145151
<span>You can also 'toggle' the swip(ed/ing) props being applied to this container below.</span>
146152
</div>
147-
</Swipeable>
148-
<SwipeableHook
153+
</Swipeable>}
154+
{showHook && <SwipeableHook
149155
{...boundSwipes}
150156
{...swipeableDirProps}
151157
delta={deltaNum}
152158
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent}
153159
trackTouch={trackTouch}
154160
trackMouse={trackMouse}
155161
rotationAngle={rotationAngleNum}
156-
className="callout"
162+
className="callout hookComponent"
157163
style={swipeableStyle}>
158164
<div onTouchStart={()=>this.resetState()}>
159165
<h5>Hook - Swipe inside here to test</h5>
160166
<p>See output below and check the console for 'onSwiping' and 'onSwiped' callback output(open dev tools)</p>
161167
<span>You can also 'toggle' the swip(ed/ing) props being applied to this container below.</span>
162168
</div>
163-
</SwipeableHook>
169+
</SwipeableHook>}
164170
<table>
165171
<thead>
166172
<tr><th>Applied?</th><th>Action</th><th>Output</th></tr>
@@ -185,9 +191,26 @@ export default class Main extends Component {
185191
<td>onSwiping Direction</td><td>{swipingDirection}</td>
186192
</tr>
187193
<tr>
188-
<td className="text-center"><a href="#appliedDirs">↓&nbsp;Below&nbsp;↓</a></td>
194+
<td className="text-center">
195+
<a href="#" onClick={(e)=>(e.preventDefault(),this.updateValue('showOnSwipeds', !showOnSwipeds))}>
196+
{showOnSwipeds ? '↑ Hide ↑' : '↓ Show ↓'}
197+
</a>
198+
</td>
189199
<td>onSwiped Direction</td><td>{swipedDirection}</td>
190200
</tr>
201+
{showOnSwipeds && <tr>
202+
<td className="text-center" colSpan="3">
203+
<table id="appliedDirs">
204+
<thead>
205+
<tr><th colSpan="2" className="text-center">onSwiped</th></tr>
206+
<tr><th>Applied?</th><th>Direction</th></tr>
207+
</thead>
208+
<tbody>
209+
{DIRECTIONS.map(this._renderAppliedDirRow.bind(this))}
210+
</tbody>
211+
</table>
212+
</td>
213+
</tr>}
191214
<tr>
192215
<td colSpan="2" className="text-center">delta:</td>
193216
<td>
@@ -228,15 +251,24 @@ export default class Main extends Component {
228251
onChange={(e)=>this.updateValue('persistEvent', e.target.checked)}/>
229252
</td>
230253
</tr>
231-
</tbody>
232-
</table>
233-
<table id="appliedDirs">
234-
<thead>
235-
<tr><th colSpan="2" className="text-center">onSwiped</th></tr>
236-
<tr><th>Applied?</th><th>Direction</th></tr>
237-
</thead>
238-
<tbody>
239-
{DIRECTIONS.map(this._renderAppliedDirRow.bind(this))}
254+
<tr>
255+
<td colSpan="2" className="text-center">Show Hook Example:</td>
256+
<td style={{textAlign: "center"}}>
257+
<input style={{margin: "0px"}}
258+
type="checkbox"
259+
checked={showHook}
260+
onChange={(e)=>this.updateValue('showHook', e.target.checked)}/>
261+
</td>
262+
</tr>
263+
<tr>
264+
<td colSpan="2" className="text-center">Show Component Example:</td>
265+
<td style={{textAlign: "center"}}>
266+
<input style={{margin: "0px"}}
267+
type="checkbox"
268+
checked={showComponent}
269+
onChange={(e)=>this.updateValue('showComponent', e.target.checked)}/>
270+
</td>
271+
</tr>
240272
</tbody>
241273
</table>
242274
<table style={{width: "100%"}}>

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-swipeable",
3-
"version": "5.0.1",
3+
"version": "5.1.0-alpha.1",
44
"description": "Swipe and touch handlers for react",
55
"main": "./lib/index.js",
66
"module": "es/index.js",

rollup.config.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import pkg from './package.json'
33

44
export default {
55
input: './src/index.js',
6-
output: [
7-
{ file: `${pkg.main}`, format: 'cjs' },
8-
{ file: `${pkg.module}`, format: 'es' }
9-
],
10-
external: [
11-
...Object.keys(pkg.dependencies || {}),
12-
...Object.keys(pkg.peerDependencies || {})
13-
],
6+
output: [{ file: `${pkg.main}`, format: 'cjs' }, { file: `${pkg.module}`, format: 'es' }],
7+
external: [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})],
148
plugins: [babel()]
159
}

src/__tests__/__snapshots__/index.spec.js.snap

-114
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Swipeable allows touchHandlerOption overwrite: verify touchHandlerOption overwrite 1`] = `
4-
Array [
5-
Array [
6-
"touchmove",
7-
[Function],
8-
Object {
9-
"capture": true,
10-
},
11-
],
12-
Array [
13-
"touchend",
14-
[Function],
15-
Object {
16-
"capture": true,
17-
},
18-
],
19-
]
20-
`;
21-
22-
exports[`Swipeable calls preventDefault when swiping in direction that has a callback: verify touchHandlerOption passive is false 1`] = `
23-
Array [
24-
Array [
25-
"touchmove",
26-
[Function],
27-
Object {
28-
"passive": false,
29-
},
30-
],
31-
Array [
32-
"touchend",
33-
[Function],
34-
Object {
35-
"passive": false,
36-
},
37-
],
38-
]
39-
`;
40-
41-
exports[`Swipeable does not call preventDefault when false: verify touchHandlerOption passive is true 1`] = `
42-
Array [
43-
Array [
44-
"touchmove",
45-
[Function],
46-
Object {
47-
"passive": true,
48-
},
49-
],
50-
Array [
51-
"touchend",
52-
[Function],
53-
Object {
54-
"passive": true,
55-
},
56-
],
57-
]
58-
`;
59-
603
exports[`Swipeable handles mouse events with trackMouse prop and fires correct props: Swipeable onSwiped trackMouse 1`] = `
614
Array [
625
Array [
@@ -251,63 +194,6 @@ Array [
251194
]
252195
`;
253196

254-
exports[`useSwipeable allows touchHandlerOption overwrite: verify touchHandlerOption overwrite 1`] = `
255-
Array [
256-
Array [
257-
"touchmove",
258-
[Function],
259-
Object {
260-
"capture": true,
261-
},
262-
],
263-
Array [
264-
"touchend",
265-
[Function],
266-
Object {
267-
"capture": true,
268-
},
269-
],
270-
]
271-
`;
272-
273-
exports[`useSwipeable calls preventDefault when swiping in direction that has a callback: verify touchHandlerOption passive is false 1`] = `
274-
Array [
275-
Array [
276-
"touchmove",
277-
[Function],
278-
Object {
279-
"passive": false,
280-
},
281-
],
282-
Array [
283-
"touchend",
284-
[Function],
285-
Object {
286-
"passive": false,
287-
},
288-
],
289-
]
290-
`;
291-
292-
exports[`useSwipeable does not call preventDefault when false: verify touchHandlerOption passive is true 1`] = `
293-
Array [
294-
Array [
295-
"touchmove",
296-
[Function],
297-
Object {
298-
"passive": true,
299-
},
300-
],
301-
Array [
302-
"touchend",
303-
[Function],
304-
Object {
305-
"passive": true,
306-
},
307-
],
308-
]
309-
`;
310-
311197
exports[`useSwipeable handles mouse events with trackMouse prop and fires correct props: useSwipeable onSwiped trackMouse 1`] = `
312198
Array [
313199
Array [

0 commit comments

Comments
 (0)