Skip to content

update react-swipeable to v5 #399

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
May 27, 2019
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"lodash.debounce": "^4.0.8",
"lodash.throttle": "^4.1.1",
"prop-types": "^15.5.8",
"react-swipeable": "^4.3.2",
"react-swipeable": "^5.2.0",
"resize-observer-polyfill": "^1.5.0"
}
}
48 changes: 25 additions & 23 deletions src/ImageGallery.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import Swipeable from 'react-swipeable';
import { Swipeable, LEFT, RIGHT } from 'react-swipeable';
import throttle from 'lodash.throttle';
import debounce from 'lodash.debounce';
import ResizeObserver from 'resize-observer-polyfill';
Expand Down Expand Up @@ -482,22 +482,23 @@ export default class ImageGallery extends React.Component {
}
};

_setScrollDirection(deltaX, deltaY) {
_setScrollDirection(dir) {
const { scrollingUpDown, scrollingLeftRight } = this.state;
const x = Math.abs(deltaX);
const y = Math.abs(deltaY);

// If y > x the user is scrolling up and down
if (y > x && !scrollingUpDown && !scrollingLeftRight) {
this.setState({ scrollingUpDown: true });
} else if (!scrollingLeftRight && !scrollingUpDown) {
this.setState({ scrollingLeftRight: true });

if (!scrollingUpDown && !scrollingLeftRight) {
if (dir === LEFT || dir === RIGHT) {
this.setState({ scrollingLeftRight: true });
} else {
this.setState({ scrollingUpDown: true });
}
}
};

_handleOnSwiped = (e, deltaX, deltaY, isFlick) => {
_handleOnSwiped = ({ event, dir, velocity }) => {
if (this.props.disableSwipe) return;
const { scrollingUpDown, scrollingLeftRight } = this.state;
const { isRTL } = this.props;
if (this.props.stopPropagation) event.stopPropagation();
if (scrollingUpDown) {
// user stopped scrollingUpDown
this.setState({ scrollingUpDown: false });
Expand All @@ -509,7 +510,8 @@ export default class ImageGallery extends React.Component {
}

if (!scrollingUpDown) { // don't swipe if user is scrolling
const side = (deltaX > 0 ? 1 : -1) * (isRTL ? -1 : 1);//if it is RTL the direction is reversed
const side = (dir === LEFT ? 1 : -1) * (isRTL ? -1 : 1); // if it is RTL the direction is reversed
const isFlick = velocity > this.props.flickThreshold;
this._handleOnSwipedTo(side, isFlick);
}
};
Expand Down Expand Up @@ -539,14 +541,20 @@ export default class ImageGallery extends React.Component {
return Math.abs(this.state.offsetPercentage) > this.props.swipeThreshold;
}

_handleSwiping = (e, deltaX, deltaY, delta) => {
const { galleryWidth, isTransitioning, scrollingUpDown } = this.state;
_handleSwiping = ({ event, absX, dir }) => {
if (this.props.disableSwipe) return;
const { galleryWidth, isTransitioning, scrollingUpDown, scrollingLeftRight } = this.state;
const { swipingTransitionDuration } = this.props;
this._setScrollDirection(deltaX, deltaY);
this._setScrollDirection(dir);
if (this.props.stopPropagation) event.stopPropagation();
if (
(this.props.preventDefaultTouchmoveEvent || scrollingLeftRight)
&& event.cancelable
) event.preventDefault();
if (!isTransitioning && !scrollingUpDown) {
const side = deltaX < 0 ? 1 : -1;
const side = dir === RIGHT ? 1 : -1;

let offsetPercentage = (delta / galleryWidth * 100);
let offsetPercentage = (absX / galleryWidth * 100);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delta here in react-swipeable v4 was actually absX so updated to use absX in v5.

if (Math.abs(offsetPercentage) >= 100) {
offsetPercentage = 100;
}
Expand Down Expand Up @@ -972,12 +980,10 @@ export default class ImageGallery extends React.Component {
isFullscreen,
modalFullscreen,
isPlaying,
scrollingLeftRight,
} = this.state;

const {
infinite,
preventDefaultTouchmoveEvent,
slideOnThumbnailOver,
isRTL,
showThumbnails,
Expand Down Expand Up @@ -1114,14 +1120,10 @@ export default class ImageGallery extends React.Component {

<Swipeable
className='image-gallery-swipe'
disabled={this.props.disableSwipe}
key='swipeable'
delta={0}
flickThreshold={this.props.flickThreshold}
onSwiping={this._handleSwiping}
onSwiped={this._handleOnSwiped}
stopPropagation={this.props.stopPropagation}
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent || scrollingLeftRight}
>
<div className='image-gallery-slides'>
{slides}
Expand Down