Skip to content

Commit 8a12330

Browse files
feat(Carousel): Add touch swipe support (#1731)
* feat(Carousel): Add touch swipe support * fix(Carousel): prefer scrolling to slide change if movement in Y is greater than X * feat(Carousel): allow disabling touch gestures * Update Carousel.js Do not store touch points in state since they are not rendered and do not need to trigger a re-render. * Update Carousel.js Co-authored-by: Evan Sharp <dumbdrum@gmail.com>
1 parent 310b061 commit 8a12330

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎docs/lib/Components/CarouselPage.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export default class CarouselPage extends React.Component {
5858
// controls whether the slide animation on the Carousel works or not
5959
slide: PropTypes.bool,
6060
cssModule: PropTypes.object,
61+
// controls whether the touch gestures on the Carousel works or not (default: true)
62+
enableTouch: PropTypes.bool,
6163
};`}
6264
</PrismCode>
6365
</pre>

‎src/Carousel.js‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import PropTypes from 'prop-types';
33
import classNames from 'classnames';
44
import CarouselItem from './CarouselItem';
55
import { mapToCssModules } from './utils';
6+
7+
const SWIPE_THRESHOLD = 40;
68

79
class Carousel extends React.Component {
810
constructor(props) {
@@ -11,6 +13,10 @@ class Carousel extends React.Component {
1113
this.renderItems = this.renderItems.bind(this);
1214
this.hoverStart = this.hoverStart.bind(this);
1315
this.hoverEnd = this.hoverEnd.bind(this);
16+
this.handleTouchStart = this.handleTouchStart.bind(this);
17+
this.handleTouchEnd = this.handleTouchEnd.bind(this);
18+
this.touchStartX = 0;
19+
this.touchStartY = 0;
1420
this.state = {
1521
activeIndex: this.props.activeIndex,
1622
direction: 'right',
@@ -110,6 +116,40 @@ class Carousel extends React.Component {
110116
}
111117
}
112118

119+
handleTouchStart(e) {
120+
if(!this.props.enableTouch) {
121+
return;
122+
}
123+
this.touchStartX = e.changedTouches[0].screenX;
124+
this.touchStartY = e.changedTouches[0].screenY;
125+
}
126+
127+
handleTouchEnd(e) {
128+
if(!this.props.enableTouch) {
129+
return;
130+
}
131+
132+
const currentX = e.changedTouches[0].screenX;
133+
const currentY = e.changedTouches[0].screenY;
134+
const diffX = Math.abs(this.touchStartX - currentX);
135+
const diffY = Math.abs(this.touchStartY - currentY);
136+
137+
// Don't swipe if Y-movement is bigger than X-movement
138+
if(diffX < diffY) {
139+
return;
140+
}
141+
142+
if(diffX < SWIPE_THRESHOLD) {
143+
return;
144+
}
145+
146+
if(currentX < this.touchStartX) {
147+
this.props.next();
148+
} else {
149+
this.props.previous();
150+
}
151+
}
152+
113153
renderItems(carouselItems, className) {
114154
const { slide } = this.props;
115155
return (
@@ -179,7 +219,8 @@ class Carousel extends React.Component {
179219
const controlRight = children[3];
180220

181221
return (
182-
<div className={outerClasses} onMouseEnter={this.hoverStart} onMouseLeave={this.hoverEnd}>
222+
<div className={outerClasses} onMouseEnter={this.hoverStart} onMouseLeave={this.hoverEnd}
223+
onTouchStart={this.handleTouchStart} onTouchEnd={this.handleTouchEnd}>
183224
{wrappedIndicators}
184225
{this.renderItems(carouselItems, innerClasses)}
185226
{controlLeft}
@@ -221,13 +262,15 @@ Carousel.propTypes = {
221262
slide: PropTypes.bool,
222263
cssModule: PropTypes.object,
223264
className: PropTypes.string,
265+
enableTouch: PropTypes.bool,
224266
};
225267

226268
Carousel.defaultProps = {
227269
interval: 5000,
228270
pause: 'hover',
229271
keyboard: true,
230272
slide: true,
273+
enableTouch: true,
231274
};
232275

233276
Carousel.childContextTypes = {

0 commit comments

Comments
 (0)