Skip to content

Commit

Permalink
fix(thumbs): active class gets removed
Browse files Browse the repository at this point in the history
when using library / framework components the classes get updated on events or
lifeycle hooks that are conflicting with the synchronous execution expectation
of the thumbs module
  • Loading branch information
fiat-multipla committed Jun 27, 2022
1 parent baafb55 commit ccd11e6
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/modules/thumbs/thumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ export default function Thumb({ swiper, extendParams, on }) {
? thumbsSwiper.slidesPerViewDynamic()
: thumbsSwiper.params.slidesPerView;

// Activate thumbs
let thumbsToActivate = 1;
const thumbActiveClass = swiper.params.thumbs.slideThumbActiveClass;

if (swiper.params.slidesPerView > 1 && !swiper.params.centeredSlides) {
thumbsToActivate = swiper.params.slidesPerView;
}

if (!swiper.params.thumbs.multipleActiveThumbs) {
thumbsToActivate = 1;
}

thumbsToActivate = Math.floor(thumbsToActivate);

thumbsSwiper.slides.removeClass(thumbActiveClass);
if (
thumbsSwiper.params.loop ||
(thumbsSwiper.params.virtual && thumbsSwiper.params.virtual.enabled)
) {
for (let i = 0; i < thumbsToActivate; i += 1) {
thumbsSwiper.$wrapperEl
.children(`[data-swiper-slide-index="${swiper.realIndex + i}"]`)
.addClass(thumbActiveClass);
}
} else {
for (let i = 0; i < thumbsToActivate; i += 1) {
thumbsSwiper.slides.eq(swiper.realIndex + i).addClass(thumbActiveClass);
}
}

const autoScrollOffset = swiper.params.thumbs.autoScrollOffset;
const useOffset = autoScrollOffset && !thumbsSwiper.params.loop;
if (swiper.realIndex !== thumbsSwiper.realIndex || useOffset) {
Expand Down Expand Up @@ -166,36 +196,6 @@ export default function Thumb({ swiper, extendParams, on }) {
thumbsSwiper.slideTo(newThumbsIndex, initial ? 0 : undefined);
}
}

// Activate thumbs
let thumbsToActivate = 1;
const thumbActiveClass = swiper.params.thumbs.slideThumbActiveClass;

if (swiper.params.slidesPerView > 1 && !swiper.params.centeredSlides) {
thumbsToActivate = swiper.params.slidesPerView;
}

if (!swiper.params.thumbs.multipleActiveThumbs) {
thumbsToActivate = 1;
}

thumbsToActivate = Math.floor(thumbsToActivate);

thumbsSwiper.slides.removeClass(thumbActiveClass);
if (
thumbsSwiper.params.loop ||
(thumbsSwiper.params.virtual && thumbsSwiper.params.virtual.enabled)
) {
for (let i = 0; i < thumbsToActivate; i += 1) {
thumbsSwiper.$wrapperEl
.children(`[data-swiper-slide-index="${swiper.realIndex + i}"]`)
.addClass(thumbActiveClass);
}
} else {
for (let i = 0; i < thumbsToActivate; i += 1) {
thumbsSwiper.slides.eq(swiper.realIndex + i).addClass(thumbActiveClass);
}
}
}

on('beforeInit', () => {
Expand Down

0 comments on commit ccd11e6

Please sign in to comment.