Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix auto play direction in 2 slides #786

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
70 changes: 32 additions & 38 deletions src/components/ImageGallery.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1193,8 +1193,8 @@ class ImageGallery extends React.Component {
}

slideToIndex(index, event) {
const { currentIndex, isTransitioning } = this.state;
const { items, slideDuration, onBeforeSlide } = this.props;
const { currentIndex, isTransitioning, currentSlideOffset } = this.state;
const { items, slideDuration, onBeforeSlide, infinite } = this.props;

if (!isTransitioning) {
if (event) {
Expand All @@ -1217,16 +1217,35 @@ class ImageGallery extends React.Component {
onBeforeSlide(nextIndex);
}

this.setState(
{
previousIndex: currentIndex,
currentIndex: nextIndex,
isTransitioning: nextIndex !== currentIndex,
currentSlideOffset: 0,
slideStyle: { transition: `all ${slideDuration}ms ease-out` },
},
this.onSliding
);
const slideHandler = () => {
this.setState(
{
previousIndex: currentIndex,
currentIndex: nextIndex,
isTransitioning: nextIndex !== currentIndex,
currentSlideOffset: 0,
slideStyle: { transition: `all ${slideDuration}ms ease-out` },
},
this.onSliding
);
};

if (infinite && items.length === 2) {
this.setState(
{
// this will reset once index changes
currentSlideOffset:
currentSlideOffset + (currentIndex > index ? 0.001 : -0.001),
slideStyle: { transition: "none" }, // move the slide over instantly
},
() => {
// add 25ms timeout to avoid delay in moving slides over
window.setTimeout(slideHandler, 25);
}
);
} else {
slideHandler();
}
}
}

Expand All @@ -1242,36 +1261,11 @@ class ImageGallery extends React.Component {

slideTo(event, direction) {
const { currentIndex, isTransitioning } = this.state;
const { items } = this.props;
const nextIndex = currentIndex + (direction === "left" ? -1 : 1);

if (isTransitioning) return;

if (items.length === 2) {
this.slideToIndexWithStyleReset(nextIndex, event);
} else {
this.slideToIndex(nextIndex, event);
}
}

slideToIndexWithStyleReset(nextIndex, event) {
/*
When there are only 2 slides fake a tiny swipe to get the slides
on the correct side for transitioning
*/
const { currentIndex, currentSlideOffset } = this.state;
this.setState(
{
// this will reset once index changes
currentSlideOffset:
currentSlideOffset + (currentIndex > nextIndex ? 0.001 : -0.001),
slideStyle: { transition: "none" }, // move the slide over instantly
},
() => {
// add 25ms timeout to avoid delay in moving slides over
window.setTimeout(() => this.slideToIndex(nextIndex, event), 25);
}
);
this.slideToIndex(nextIndex, event);
}

handleThumbnailMouseOver(event, index) {
Expand Down
Loading