Skip to content

Commit

Permalink
fix(slides): update swiper instance after initialization (#24257)
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-perkins authored Nov 24, 2021
1 parent fb96ab5 commit 89e4bc5
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions core/src/components/slides/slides.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, ComponentInterface, Element, Event, EventEmitter, Host, Method, Prop, Watch, h } from '@stencil/core';
import { Build, Component, ComponentInterface, Element, Event, EventEmitter, Host, Method, Prop, Watch, h } from '@stencil/core';

import { getIonMode } from '../../global/ionic-global';
import { componentOnReady } from '../../utils/helpers'
Expand All @@ -24,8 +24,6 @@ export class Slides implements ComponentInterface {
private mutationO?: MutationObserver;
private readySwiper!: (swiper: SwiperInterface) => void;
private swiper: Promise<SwiperInterface> = new Promise(resolve => { this.readySwiper = resolve; });
private syncSwiper?: SwiperInterface;
private didInit = false;

@Element() el!: HTMLIonSlidesElement;

Expand Down Expand Up @@ -141,8 +139,7 @@ export class Slides implements ComponentInterface {
}

connectedCallback() {
// tslint:disable-next-line: strict-type-predicates
if (typeof MutationObserver !== 'undefined') {
if (Build.isBrowser) {
const mut = this.mutationO = new MutationObserver(() => {
if (this.swiperReady) {
this.update();
Expand All @@ -154,10 +151,7 @@ export class Slides implements ComponentInterface {
});

componentOnReady(this.el, () => {
if (!this.didInit) {
this.didInit = true;
this.initSwiper();
}
})
}
}
Expand All @@ -167,23 +161,6 @@ export class Slides implements ComponentInterface {
this.mutationO.disconnect();
this.mutationO = undefined;
}

/**
* We need to synchronously destroy
* swiper otherwise it is possible
* that it will be left in a
* destroyed state if connectedCallback
* is called multiple times
*/
const swiper = this.syncSwiper;
if (swiper !== undefined) {
swiper.destroy(true, true);
this.swiper = new Promise(resolve => { this.readySwiper = resolve; });
this.swiperReady = false;
this.syncSwiper = undefined;
}

this.didInit = false;
}

/**
Expand Down Expand Up @@ -369,7 +346,6 @@ export class Slides implements ComponentInterface {
await waitForSlides(this.el);
const swiper = new Swiper(this.el, finalOptions);
this.swiperReady = true;
this.syncSwiper = swiper;
this.readySwiper(swiper);
}

Expand Down Expand Up @@ -483,6 +459,8 @@ export class Slides implements ComponentInterface {
init: () => {
setTimeout(() => {
this.ionSlidesDidLoad.emit();
// Forces the swiper instance to update after initializing.
this.update();
}, 20);
},
slideChangeTransitionStart: this.ionSlideWillChange.emit,
Expand Down

0 comments on commit 89e4bc5

Please sign in to comment.