Skip to content

Commit 7aaa0d1

Browse files
committed
feat: add wrapperClass to swiper-wrapper in React & Vue components
fixes #6254 fixes #5942
1 parent 2f5ed48 commit 7aaa0d1

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

src/components-shared/update-swiper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ function updateSwiper({
1010
scrollbarEl,
1111
paginationEl,
1212
}) {
13-
const updateParams = changedParams.filter((key) => key !== 'children' && key !== 'direction');
13+
const updateParams = changedParams.filter(
14+
(key) => key !== 'children' && key !== 'direction' && key !== 'wrapperClass',
15+
);
1416
const { params: currentParams, pagination, navigation, scrollbar, virtual, thumbs } = swiper;
1517
let needThumbsInit;
1618
let needControllerInit;

src/components-shared/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ function attrToProp(attrName = '') {
5151
return attrName.replace(/-[a-z]/g, (l) => l.toUpperCase().replace('-', ''));
5252
}
5353

54+
function wrapperClass(className = '') {
55+
if (!className) return 'swiper-wrapper';
56+
if (!className.includes('swiper-wrapper')) return `swiper-wrapper ${className}`;
57+
return className;
58+
}
59+
5460
export {
5561
isObject,
5662
extend,
@@ -59,4 +65,5 @@ export {
5965
needsScrollbar,
6066
uniqueClasses,
6167
attrToProp,
68+
wrapperClass,
6269
};

src/react/swiper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
needsPagination,
99
uniqueClasses,
1010
extend,
11+
wrapperClass,
1112
} from '../components-shared/utils.js';
1213
import { getChangedParams } from '../components-shared/get-changed-params.js';
1314
import { getChildren } from './get-children.js';
@@ -62,7 +63,9 @@ const Swiper = forwardRef(
6263
// init swiper
6364
Object.assign(swiperParams.on, events);
6465
eventsAssigned = true;
65-
swiperRef.current = new SwiperCore(swiperParams);
66+
const passParams = { ...swiperParams };
67+
delete passParams.wrapperClass;
68+
swiperRef.current = new SwiperCore(passParams);
6669
if (swiperRef.current.virtual && swiperRef.current.params.virtual.enabled) {
6770
swiperRef.current.virtual.slides = slides;
6871
const extendWith = {
@@ -196,7 +199,7 @@ const Swiper = forwardRef(
196199
>
197200
<SwiperContext.Provider value={swiperRef.current}>
198201
{slots['container-start']}
199-
<WrapperTag className="swiper-wrapper">
202+
<WrapperTag className={wrapperClass(swiperParams.wrapperClass)}>
200203
{slots['wrapper-start']}
201204
{renderSlides()}
202205
{slots['wrapper-end']}

src/vue/swiper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
needsPagination,
99
uniqueClasses,
1010
extend,
11+
wrapperClass,
1112
} from '../components-shared/utils.js';
1213
import { getChangedParams } from '../components-shared/get-changed-params.js';
1314
import { getChildren } from './get-children.js';
@@ -261,7 +262,9 @@ const Swiper = {
261262
});
262263

263264
// init Swiper
264-
swiperRef.value = new SwiperCore(swiperParams);
265+
const passParams = { ...swiperParams };
266+
delete passParams.wrapperClass;
267+
swiperRef.value = new SwiperCore(passParams);
265268
if (swiperRef.value.virtual && swiperRef.value.params.virtual.enabled) {
266269
swiperRef.value.virtual.slides = slidesRef.value;
267270
const extendWith = {
@@ -368,7 +371,7 @@ const Swiper = {
368371
},
369372
[
370373
slots['container-start'],
371-
h(WrapperTag, { class: 'swiper-wrapper' }, [
374+
h(WrapperTag, { class: wrapperClass(swiperParams.wrapperClass) }, [
372375
slots['wrapper-start'],
373376
renderSlides(slides),
374377
slots['wrapper-end'],

0 commit comments

Comments
 (0)