Skip to content

Commit 89d8680

Browse files
author
Guy Elsmore-Paddock
committed
1262 - Fire afterChange via Separate Timer
This ensures `afterChange` is always fired even when the carousel is not animating and even if the animation gets interrupted by a window resize.
1 parent de674c0 commit 89d8680

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/inner-slider.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ export class InnerSlider extends React.Component {
378378
beforeChange,
379379
onLazyLoad,
380380
speed,
381-
afterChange
381+
afterChange,
382+
waitForAnimate
382383
} = this.props;
383384
// capture currentslide before state is updated
384385
const currentSlide = this.state.currentSlide;
@@ -395,9 +396,8 @@ export class InnerSlider extends React.Component {
395396
value => this.state.lazyLoadedList.indexOf(value) < 0
396397
);
397398
onLazyLoad && slidesToLoad.length > 0 && onLazyLoad(slidesToLoad);
398-
if (!this.props.waitForAnimate && this.animationEndCallback) {
399+
if (!waitForAnimate && this.animationEndCallback) {
399400
clearTimeout(this.animationEndCallback);
400-
afterChange && afterChange(currentSlide);
401401
delete this.animationEndCallback;
402402
}
403403
this.setState(state, () => {
@@ -406,17 +406,30 @@ export class InnerSlider extends React.Component {
406406
this.asNavForIndex = index;
407407
asNavFor.innerSlider.slideHandler(index);
408408
}
409+
410+
if (!waitForAnimate && afterChange) {
411+
afterChange(state.currentSlide);
412+
}
413+
409414
if (!nextState) return;
415+
410416
this.animationEndCallback = setTimeout(() => {
411417
const { animating, ...firstBatch } = nextState;
412418
this.setState(firstBatch, () => {
413419
this.callbackTimers.push(
414420
setTimeout(() => this.setState({ animating }), 10)
415421
);
416-
afterChange && afterChange(state.currentSlide);
417422
delete this.animationEndCallback;
418423
});
419424
}, speed);
425+
426+
// Ensure we always fire afterChange callbacks even if we are not
427+
// animating or if the animation gets interrupted by a window resize.
428+
if (waitForAnimate && afterChange) {
429+
this.callbackTimers.push(
430+
setTimeout(() => afterChange(state.currentSlide), speed)
431+
);
432+
}
420433
});
421434
};
422435
changeSlide = (options, dontAnimate = false) => {

0 commit comments

Comments
 (0)