Skip to content

Commit 42db86d

Browse files
committed
feat(core): possible to enable breakpoints based on container width (instead of window width)
fixes #4244
1 parent 6678f60 commit 42db86d

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/components/core/breakpoints/getBreakpoint.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { getWindow } from 'ssr-window';
22

3-
export default function getBreakpoints(breakpoints) {
4-
const window = getWindow();
5-
// Get breakpoint for window width
6-
if (!breakpoints) return undefined;
3+
export default function getBreakpoint(breakpoints, base = 'window', containerEl) {
4+
if (!breakpoints || (base === 'container' && !containerEl)) return undefined;
75
let breakpoint = false;
86

7+
const window = getWindow();
8+
const currentWidth = base === 'window' ? window.innerWidth : containerEl.clientWidth;
9+
const currentHeight = base === 'window' ? window.innerHeight : containerEl.clientHeight;
10+
911
const points = Object.keys(breakpoints).map((point) => {
1012
if (typeof point === 'string' && point.indexOf('@') === 0) {
1113
const minRatio = parseFloat(point.substr(1));
12-
const value = window.innerHeight * minRatio;
14+
const value = currentHeight * minRatio;
1315
return { value, point };
1416
}
1517
return { value: point, point };
@@ -18,7 +20,7 @@ export default function getBreakpoints(breakpoints) {
1820
points.sort((a, b) => parseInt(a.value, 10) - parseInt(b.value, 10));
1921
for (let i = 0; i < points.length; i += 1) {
2022
const { point, value } = points[i];
21-
if (value <= window.innerWidth) {
23+
if (value <= currentWidth) {
2224
breakpoint = point;
2325
}
2426
}

src/components/core/breakpoints/setBreakpoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function setBreakpoint() {
77
if (!breakpoints || (breakpoints && Object.keys(breakpoints).length === 0)) return;
88

99
// Get breakpoint for window width and update parameters
10-
const breakpoint = swiper.getBreakpoint(breakpoints);
10+
const breakpoint = swiper.getBreakpoint(breakpoints, swiper.params.breakpointsBase, swiper.el);
1111

1212
if (breakpoint && swiper.currentBreakpoint !== breakpoint) {
1313
const breakpointOnlyParams = breakpoint in breakpoints ? breakpoints[breakpoint] : undefined;

src/components/core/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default {
4848

4949
// Breakpoints
5050
breakpoints: undefined,
51+
breakpointsBase: 'window',
5152

5253
// Slides grid
5354
spaceBetween: 0,

src/types/swiper-options.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,15 @@ export interface SwiperOptions {
722722
[ratio: string]: SwiperOptions;
723723
};
724724

725+
/**
726+
* Base for breakpoints (beta). Can be `window` or `container`. If set to `window` (by default) then breakpoint keys mean window width. If set to `container` then breakpoint keys treated as swiper container width
727+
*
728+
* @default 'window'
729+
*
730+
* @note Currently in beta and not supported by Swiper Angular, React, Svelte and Vue components
731+
*/
732+
breakpointsBase?: string;
733+
725734
// Observer
726735
/**
727736
* Set to `true` to enable Mutation Observer on Swiper and its elements. In this case Swiper will be updated (reinitialized) each time if you change its style (like hide/show) or modify its child elements (like adding/removing slides)

0 commit comments

Comments
 (0)