Skip to content

Commit 7e3ac11

Browse files
committed
add comments, update readme
1 parent 46da5ed commit 7e3ac11

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

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

src/index.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function getHandlers(set, props) {
5151
if (event.touches && event.touches.length > 1) return
5252

5353
set(state => {
54+
// setup mouse listeners on document to track swipe since swipe can leave container
5455
if (state.props.trackMouse) {
5556
document.addEventListener(mouseMove, onMove)
5657
document.addEventListener(mouseUp, onUp)
@@ -112,13 +113,9 @@ function getHandlers(set, props) {
112113
}
113114

114115
const stop = () => {
115-
set(state => {
116-
if (state.props.trackMouse) {
117-
document.removeEventListener(mouseMove, onMove)
118-
document.removeEventListener(mouseUp, onUp)
119-
}
120-
return state
121-
})
116+
// safe to just call removeEventListener
117+
document.removeEventListener(mouseMove, onMove)
118+
document.removeEventListener(mouseUp, onUp)
122119
}
123120

124121
const onUp = e => {
@@ -135,15 +132,21 @@ function getHandlers(set, props) {
135132
}
136133

137134
const onRef = el => {
135+
// "inline" ref functions are called twice on render, once with null then again with DOM element
136+
// ignore null here
138137
if (el === null) return
139138
set(state => {
139+
// if the same DOM el as previous just return state
140140
if (state.el === el) return state
141+
// if new DOM el clean up old DOM
141142
if (state.el && state.el !== el) cleanUp(state.el)
143+
// only attach if we want to track touch
142144
if (state.props.trackTouch) {
143145
if (el && el.addEventListener) {
144146
el.addEventListener(touchStart, onStart)
145147
el.addEventListener(touchMove, onMove)
146148
el.addEventListener(touchEnd, onUp)
149+
// store event attached DOM el for comparison
147150
return { ...state, el }
148151
}
149152
}
@@ -153,6 +156,8 @@ function getHandlers(set, props) {
153156

154157
// set ref callback to attach touch event listeners
155158
const output = { ref: onRef }
159+
160+
// if track mouse attach mouse down listener
156161
if (props.trackMouse) {
157162
output.onMouseDown = onStart
158163
}

0 commit comments

Comments
 (0)