Skip to content

Commit 0614970

Browse files
author
Brian Hartz
committed
add nodeName prop to configure container
1 parent 0da88e0 commit 0614970

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

.babelrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"plugins": ["transform-object-rest-spread"],
23
"presets": ["es2015-loose", "react"]
34
}

README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ $ npm install --save react-swipeable
1010

1111
### Use
1212

13+
react-swipeable generates a React component (defaults to `<div>`) and binds touch events to it.
14+
1315
```js
1416
var Swipeable = require('react-swipeable')
1517

@@ -27,16 +29,14 @@ var SampleComponent = React.createClass({
2729
onSwipedDown={this.swipedDown}
2830
onSwipedLeft={this.swipedLeft}
2931
onSwiped={this.handleSwipeAction}>
30-
<div>
31-
This element can be swiped
32-
</div>
32+
You can be swipe here!
3333
</Swipeable>
3434
)
3535
}
3636
})
3737
```
3838

39-
## Props
39+
## Event Props
4040

4141
**`onSwiping`**, **`onSwipingUp`**, **`onSwipingRight`**, **`onSwipingDown`**, **`onSwipingLeft`**, are called with the event
4242
as well as the absolute delta of where the swipe started and where it's currently at. These constantly fire throughout touch events.
@@ -50,12 +50,14 @@ as well as the x distance, + or -, from where the swipe started to where it ende
5050

5151
#####Configuration Props
5252

53-
**`flickThreshold`** is a number (float) which determines the max velocity of a swipe before it's considered a flick.
53+
**`flickThreshold`** is a number (float) which determines the max velocity of a swipe before it's considered a flick. The default value is `0.6`.
5454

55-
**`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.
55+
**`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`.
5656

5757
**`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`.
5858

59+
**`nodeName`** is a string which determines the html element/node that this react component binds its touch events to then returns. The default value is `'div'`.
60+
5961
**None of the props are required.**
6062
### PropTypes
6163

@@ -73,6 +75,7 @@ as well as the x distance, + or -, from where the swipe started to where it ende
7375
flickThreshold: React.PropTypes.number,
7476
delta: React.PropTypes.number,
7577
preventDefaultTouchmoveEvent: React.PropTypes.bool
78+
nodeName: React.PropTypes.string
7679
```
7780

7881
## Development

examples/app/Main.js

+31-14
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ const initialState = {
77
swiping: false,
88
swiped: false,
99
swipingDirection: '',
10-
swipedDirection: ''
10+
swipedDirection: '',
1111
};
1212
const initialStateSwipeable = {
1313
flickThreshold: '0.6',
1414
delta: '10',
15-
preventDefaultTouchmoveEvent: true
15+
preventDefaultTouchmoveEvent: true,
16+
nodeName: 'div',
1617
};
1718
const initialStateApplied = {
1819
onSwipingApplied: true,
@@ -103,6 +104,7 @@ export default class Main extends Component {
103104
onSwipingApplied,
104105
onSwipedApplied,
105106
preventDefaultTouchmoveEvent,
107+
nodeName,
106108
} = this.state;
107109

108110
const isFlickThresholdNumber = !(isNaN(flickThreshold) || flickThreshold === '');
@@ -127,14 +129,15 @@ export default class Main extends Component {
127129
{...swipeableDirProps}
128130
flickThreshold={flickThresholdNum}
129131
delta={deltaNum}
130-
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent}>
131-
<div className="callout"
132-
onTouchStart={()=>this.resetState()}
133-
style={{fontSize: "0.75rem"}}>
134-
<h5>Swipe inside here to test...</h5>
135-
<p>See output below and check the console for 'onSwiping' and 'onSwiped' callback output</p>
136-
<span>You can also 'toggle' the swip(ed/ing) props being applied to this container below.</span>
137-
</div>
132+
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent}
133+
nodeName={nodeName}
134+
className="callout"
135+
style={{fontSize: "0.75rem"}}>
136+
<div onTouchStart={()=>this.resetState()}>
137+
<h5>Swipe inside here to test...</h5>
138+
<p>See output below and check the console for 'onSwiping' and 'onSwiped' callback output</p>
139+
<span>You can also 'toggle' the swip(ed/ing) props being applied to this container below.</span>
140+
</div>
138141
</Swipeable>
139142
<table>
140143
<thead>
@@ -156,11 +159,11 @@ export default class Main extends Component {
156159
<td>onSwiped</td><td>{swiped ? 'True' : 'False'}</td>
157160
</tr>
158161
<tr>
159-
<td className="text-center"><a href="#appliedDirs">Below</a></td>
162+
<td className="text-center"><a href="#appliedDirs">↓&nbsp;Below&nbsp;↓</a></td>
160163
<td>onSwiping[Direction]</td><td>{swipingDirection}</td>
161164
</tr>
162165
<tr>
163-
<td className="text-center"><a href="#appliedDirs">Below</a></td>
166+
<td className="text-center"><a href="#appliedDirs">↓&nbsp;Below&nbsp;↓</a></td>
164167
<td>onSwiped[Direction]</td><td>{swipedDirection}</td>
165168
</tr>
166169
<tr>
@@ -188,9 +191,22 @@ export default class Main extends Component {
188191
onChange={(e)=>this.updateValue('preventDefaultTouchmoveEvent', e.target.checked)}/>
189192
</td>
190193
</tr>
194+
<tr>
195+
<td className="text-center">nodeName:</td>
196+
<td colSpan="2" className="text-center">
197+
<button type="button" className={`button${nodeName!=='div'?' secondary':''}`}
198+
style={{margin: '.5rem'}}
199+
onClick={()=>this.updateValue('nodeName', 'div')}>{`'div'`}</button>
200+
<button type="button" className={`button${nodeName!=='span'?' secondary':''}`}
201+
style={{margin: '.5rem'}}
202+
onClick={()=>this.updateValue('nodeName', 'span')}>{`'span'`}</button>
203+
<button type="button" className={`button${nodeName!=='li'?' secondary':''}`}
204+
style={{margin: '.5rem'}}
205+
onClick={()=>this.updateValue('nodeName', 'li')}>{`'li'`}</button>
206+
</td>
207+
</tr>
191208
</tbody>
192209
</table>
193-
<button type="button" className="tiny button expanded" onClick={()=>this.resetState(true)}>Reset All Options</button>
194210
<table id="appliedDirs">
195211
<thead>
196212
<tr><th colSpan="2" className="text-center" style={{borderRight: "1px solid #cccccc"}}>onSwiping</th><th colSpan="2" className="text-center">onSwiped</th></tr>
@@ -200,7 +216,8 @@ export default class Main extends Component {
200216
{DIRECTIONS.map(this._renderAppliedDirRow.bind(this))}
201217
</tbody>
202218
</table>
203-
<p style={{"margin-bottom": "5rem"}}>
219+
<button type="button" className="tiny button expanded" onClick={()=>this.resetState(true)}>Reset All Options</button>
220+
<p style={{"marginBottom": "5rem"}}>
204221
Thanks for checking out the example app! Let us know if you find any bugs, and&nbsp;
205222
<a href="https://github.com/dogfessional/react-swipeable/pulls">submit a PR!</a>
206223
</p>

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"license": "MIT",
3232
"devDependencies": {
3333
"babel-cli": "^6.3.17",
34+
"babel-plugin-transform-object-rest-spread": "^6.3.13",
3435
"babel-preset-es2015-loose": "^6.1.4",
3536
"babel-preset-react": "^6.3.13",
3637
"react": ">=0.12.x"

src/Swipeable.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const Swipeable = React.createClass({
1414
onSwipedLeft: React.PropTypes.func,
1515
flickThreshold: React.PropTypes.number,
1616
delta: React.PropTypes.number,
17-
preventDefaultTouchmoveEvent: React.PropTypes.bool
17+
preventDefaultTouchmoveEvent: React.PropTypes.bool,
18+
nodeName: React.PropTypes.string
1819
},
1920

2021
getInitialState: function () {
@@ -30,7 +31,8 @@ const Swipeable = React.createClass({
3031
return {
3132
flickThreshold: 0.6,
3233
delta: 10,
33-
preventDefaultTouchmoveEvent: true
34+
preventDefaultTouchmoveEvent: true,
35+
nodeName: 'div'
3436
}
3537
},
3638

@@ -149,14 +151,16 @@ const Swipeable = React.createClass({
149151
},
150152

151153
render: function () {
152-
return (
153-
<div {...this.props}
154-
onTouchStart={this.touchStart}
155-
onTouchMove={this.touchMove}
156-
onTouchEnd={this.touchEnd} >
157-
{this.props.children}
158-
</div>
159-
)
154+
return React.createElement(
155+
this.props.nodeName,
156+
{
157+
...this.props,
158+
onTouchStart: this.touchStart,
159+
onTouchMove: this.touchMove,
160+
onTouchEnd: this.touchEnd,
161+
},
162+
this.props.children
163+
);
160164
}
161165
})
162166

0 commit comments

Comments
 (0)