Skip to content

Commit

Permalink
feat: add wrapperClass to swiper-wrapper in React & Vue components
Browse files Browse the repository at this point in the history
fixes #6254
fixes #5942
  • Loading branch information
nolimits4web committed Feb 1, 2023
1 parent 2f5ed48 commit 7aaa0d1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/components-shared/update-swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ function updateSwiper({
scrollbarEl,
paginationEl,
}) {
const updateParams = changedParams.filter((key) => key !== 'children' && key !== 'direction');
const updateParams = changedParams.filter(
(key) => key !== 'children' && key !== 'direction' && key !== 'wrapperClass',
);
const { params: currentParams, pagination, navigation, scrollbar, virtual, thumbs } = swiper;
let needThumbsInit;
let needControllerInit;
Expand Down
7 changes: 7 additions & 0 deletions src/components-shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ function attrToProp(attrName = '') {
return attrName.replace(/-[a-z]/g, (l) => l.toUpperCase().replace('-', ''));
}

function wrapperClass(className = '') {
if (!className) return 'swiper-wrapper';
if (!className.includes('swiper-wrapper')) return `swiper-wrapper ${className}`;
return className;
}

export {
isObject,
extend,
Expand All @@ -59,4 +65,5 @@ export {
needsScrollbar,
uniqueClasses,
attrToProp,
wrapperClass,
};
7 changes: 5 additions & 2 deletions src/react/swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
needsPagination,
uniqueClasses,
extend,
wrapperClass,
} from '../components-shared/utils.js';
import { getChangedParams } from '../components-shared/get-changed-params.js';
import { getChildren } from './get-children.js';
Expand Down Expand Up @@ -62,7 +63,9 @@ const Swiper = forwardRef(
// init swiper
Object.assign(swiperParams.on, events);
eventsAssigned = true;
swiperRef.current = new SwiperCore(swiperParams);
const passParams = { ...swiperParams };
delete passParams.wrapperClass;
swiperRef.current = new SwiperCore(passParams);
if (swiperRef.current.virtual && swiperRef.current.params.virtual.enabled) {
swiperRef.current.virtual.slides = slides;
const extendWith = {
Expand Down Expand Up @@ -196,7 +199,7 @@ const Swiper = forwardRef(
>
<SwiperContext.Provider value={swiperRef.current}>
{slots['container-start']}
<WrapperTag className="swiper-wrapper">
<WrapperTag className={wrapperClass(swiperParams.wrapperClass)}>
{slots['wrapper-start']}
{renderSlides()}
{slots['wrapper-end']}
Expand Down
7 changes: 5 additions & 2 deletions src/vue/swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
needsPagination,
uniqueClasses,
extend,
wrapperClass,
} from '../components-shared/utils.js';
import { getChangedParams } from '../components-shared/get-changed-params.js';
import { getChildren } from './get-children.js';
Expand Down Expand Up @@ -261,7 +262,9 @@ const Swiper = {
});

// init Swiper
swiperRef.value = new SwiperCore(swiperParams);
const passParams = { ...swiperParams };
delete passParams.wrapperClass;
swiperRef.value = new SwiperCore(passParams);
if (swiperRef.value.virtual && swiperRef.value.params.virtual.enabled) {
swiperRef.value.virtual.slides = slidesRef.value;
const extendWith = {
Expand Down Expand Up @@ -368,7 +371,7 @@ const Swiper = {
},
[
slots['container-start'],
h(WrapperTag, { class: 'swiper-wrapper' }, [
h(WrapperTag, { class: wrapperClass(swiperParams.wrapperClass) }, [
slots['wrapper-start'],
renderSlides(slides),
slots['wrapper-end'],
Expand Down

0 comments on commit 7aaa0d1

Please sign in to comment.