From 89d86806739fb23191491a6644824dc428532b57 Mon Sep 17 00:00:00 2001 From: Guy Elsmore-Paddock Date: Mon, 28 Dec 2020 18:09:33 -0500 Subject: [PATCH] 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. --- src/inner-slider.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/inner-slider.js b/src/inner-slider.js index 24820f6bc..815c0d7a3 100644 --- a/src/inner-slider.js +++ b/src/inner-slider.js @@ -378,7 +378,8 @@ export class InnerSlider extends React.Component { beforeChange, onLazyLoad, speed, - afterChange + afterChange, + waitForAnimate } = this.props; // capture currentslide before state is updated const currentSlide = this.state.currentSlide; @@ -395,9 +396,8 @@ export class InnerSlider extends React.Component { value => this.state.lazyLoadedList.indexOf(value) < 0 ); onLazyLoad && slidesToLoad.length > 0 && onLazyLoad(slidesToLoad); - if (!this.props.waitForAnimate && this.animationEndCallback) { + if (!waitForAnimate && this.animationEndCallback) { clearTimeout(this.animationEndCallback); - afterChange && afterChange(currentSlide); delete this.animationEndCallback; } this.setState(state, () => { @@ -406,17 +406,30 @@ export class InnerSlider extends React.Component { this.asNavForIndex = index; asNavFor.innerSlider.slideHandler(index); } + + if (!waitForAnimate && afterChange) { + afterChange(state.currentSlide); + } + if (!nextState) return; + this.animationEndCallback = setTimeout(() => { const { animating, ...firstBatch } = nextState; this.setState(firstBatch, () => { this.callbackTimers.push( setTimeout(() => this.setState({ animating }), 10) ); - afterChange && afterChange(state.currentSlide); delete this.animationEndCallback; }); }, speed); + + // Ensure we always fire afterChange callbacks even if we are not + // animating or if the animation gets interrupted by a window resize. + if (waitForAnimate && afterChange) { + this.callbackTimers.push( + setTimeout(() => afterChange(state.currentSlide), speed) + ); + } }); }; changeSlide = (options, dontAnimate = false) => {