Skip to content

Commit 3b36503

Browse files
authored
[#655] Fixes slides overlap when there are only 2 images (#751)
1 parent b8d156e commit 3b36503

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ class MyGallery extends React.Component {
9595
- `srcSet` - image srcset (html5 attribute)
9696
- `sizes` - image sizes (html5 attribute)
9797
- `bulletClass` - extra class for the bullet of the item
98-
- `bulletOnClick` - `callback({item, itemIndex, currentIndex})`
99-
- A function that will be called upon bullet click.
10098
- `infinite`: Boolean, default `true`
10199
- infinite sliding
102100
- `lazyLoad`: Boolean, default `false`
@@ -144,6 +142,7 @@ class MyGallery extends React.Component {
144142
- `onThumbnailError`: Function, `callback(event)`
145143
- overrides onErrorImage
146144
- `onThumbnailClick`: Function, `callback(event, index)`
145+
- `onBulletClick`: Function, `callback(event, index)`
147146
- `onImageLoad`: Function, `callback(event)`
148147
- `onSlide`: Function, `callback(currentIndex)`
149148
- `onBeforeSlide`: Function, `callback(nextIndex)`

src/components/ImageGallery.jsx

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,39 @@ class ImageGallery extends React.Component {
244244
}
245245

246246
onThumbnailClick(event, index) {
247-
const { onThumbnailClick } = this.props;
247+
const { onThumbnailClick, items } = this.props;
248248
const { currentIndex } = this.state;
249249
// blur element to remove outline cause by focus
250250
event.target.parentNode.parentNode.blur();
251251
if (currentIndex !== index) {
252-
this.slideToIndex(index, event);
252+
if (items.length === 2) {
253+
this.slideToIndexWithStyleReset(index, event);
254+
} else {
255+
this.slideToIndex(index, event);
256+
}
253257
}
254258
if (onThumbnailClick) {
255259
onThumbnailClick(event, index);
256260
}
257261
}
258262

263+
onBulletClick = (event, index) => {
264+
const { onBulletClick, items } = this.props;
265+
const { currentIndex } = this.state;
266+
// blur element to remove outline caused by focus
267+
event.target.blur();
268+
if (currentIndex !== index) {
269+
if (items.length === 2) {
270+
this.slideToIndexWithStyleReset(index, event);
271+
} else {
272+
this.slideToIndex(index, event);
273+
}
274+
}
275+
if (onBulletClick) {
276+
onBulletClick(event, index);
277+
}
278+
};
279+
259280
onThumbnailMouseOver(event, index) {
260281
if (this.thumbnailMouseOverTimer) {
261282
window.clearTimeout(this.thumbnailMouseOverTimer);
@@ -628,15 +649,6 @@ class ImageGallery extends React.Component {
628649
}
629650

630651
if (showBullets) {
631-
// generate bullet elements and store them in array
632-
const bulletOnClick = (event) => {
633-
if (item.bulletOnClick) {
634-
item.bulletOnClick({ item, itemIndex: index, currentIndex });
635-
}
636-
// blur element to remove outline caused by focus
637-
event.target.blur();
638-
return this.slideToIndex.call(this, index, event);
639-
};
640652
const igBulletClass = clsx("image-gallery-bullet", item.bulletClass, {
641653
active: currentIndex === index,
642654
});
@@ -645,7 +657,7 @@ class ImageGallery extends React.Component {
645657
type="button"
646658
key={`bullet-${index}`}
647659
className={igBulletClass}
648-
onClick={bulletOnClick}
660+
onClick={(event) => this.onBulletClick(event, index)}
649661
aria-pressed={currentIndex === index ? "true" : "false"}
650662
aria-label={`Go to Slide ${index + 1}`}
651663
/>
@@ -1199,34 +1211,39 @@ class ImageGallery extends React.Component {
11991211
}
12001212

12011213
slideTo(event, direction) {
1202-
const { currentIndex, currentSlideOffset, isTransitioning } = this.state;
1214+
const { currentIndex, isTransitioning } = this.state;
12031215
const { items } = this.props;
12041216
const nextIndex = currentIndex + (direction === "left" ? -1 : 1);
12051217

12061218
if (isTransitioning) return;
12071219

12081220
if (items.length === 2) {
1209-
/*
1210-
When there are only 2 slides fake a tiny swipe to get the slides
1211-
on the correct side for transitioning
1212-
*/
1213-
this.setState(
1214-
{
1215-
// this will reset once index changes
1216-
currentSlideOffset:
1217-
currentSlideOffset + (direction === "left" ? 0.001 : -0.001),
1218-
slideStyle: { transition: "none" }, // move the slide over instantly
1219-
},
1220-
() => {
1221-
// add 25ms timeout to avoid delay in moving slides over
1222-
window.setTimeout(() => this.slideToIndex(nextIndex, event), 25);
1223-
}
1224-
);
1221+
this.slideToIndexWithStyleReset(nextIndex, event);
12251222
} else {
12261223
this.slideToIndex(nextIndex, event);
12271224
}
12281225
}
12291226

1227+
slideToIndexWithStyleReset(nextIndex, event) {
1228+
/*
1229+
When there are only 2 slides fake a tiny swipe to get the slides
1230+
on the correct side for transitioning
1231+
*/
1232+
const { currentIndex, currentSlideOffset } = this.state;
1233+
this.setState(
1234+
{
1235+
// this will reset once index changes
1236+
currentSlideOffset:
1237+
currentSlideOffset + (currentIndex > nextIndex ? 0.001 : -0.001),
1238+
slideStyle: { transition: "none" }, // move the slide over instantly
1239+
},
1240+
() => {
1241+
// add 25ms timeout to avoid delay in moving slides over
1242+
window.setTimeout(() => this.slideToIndex(nextIndex, event), 25);
1243+
}
1244+
);
1245+
}
1246+
12301247
handleThumbnailMouseOver(event, index) {
12311248
const { slideOnThumbnailOver } = this.props;
12321249
if (slideOnThumbnailOver) this.onThumbnailMouseOver(event, index);
@@ -1626,6 +1643,7 @@ ImageGallery.propTypes = {
16261643
onTouchStart: func,
16271644
onMouseOver: func,
16281645
onMouseLeave: func,
1646+
onBulletClick: func,
16291647
onThumbnailError: func,
16301648
onThumbnailClick: func,
16311649
renderCustomControls: func,
@@ -1682,6 +1700,7 @@ ImageGallery.defaultProps = {
16821700
onTouchStart: null,
16831701
onMouseOver: null,
16841702
onMouseLeave: null,
1703+
onBulletClick: null,
16851704
onThumbnailError: null,
16861705
onThumbnailClick: null,
16871706
renderCustomControls: null,

0 commit comments

Comments
 (0)