Skip to content

Commit

Permalink
feat(core): Allow using a CSS selector as breakpointsBase (#7818)
Browse files Browse the repository at this point in the history
* Allow using a CSS selector as breakpointsBase

* Fix to use document selector instead of closets as the el can be inside shadow DOM

* add CSS Selector to types and use swiper.el

---------

Co-authored-by: Vladimir Kharlampidi <[email protected]>
  • Loading branch information
kilobyte2007 and nolimits4web authored Jan 2, 2025
1 parent 21610bd commit 44d3443
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/core/breakpoints/setBreakpoint.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDocument } from 'ssr-window';
import { extend } from '../../shared/utils.mjs';

const isGridEnabled = (swiper, params) => {
Expand All @@ -9,9 +10,19 @@ export default function setBreakpoint() {
const { realIndex, initialized, params, el } = swiper;
const breakpoints = params.breakpoints;
if (!breakpoints || (breakpoints && Object.keys(breakpoints).length === 0)) return;

// Get breakpoint for window width and update parameters
const breakpoint = swiper.getBreakpoint(breakpoints, swiper.params.breakpointsBase, swiper.el);
const document = getDocument();

// Get breakpoint for window/container width and update parameters
const breakpointsBase =
params.breakpointsBase === 'window' || !params.breakpointsBase
? params.breakpointsBase
: 'container';
const breakpointContainer =
['window', 'container'].includes(params.breakpointsBase) || !params.breakpointsBase
? swiper.el
: document.querySelector(params.breakpointsBase);

const breakpoint = swiper.getBreakpoint(breakpoints, breakpointsBase, breakpointContainer);

if (!breakpoint || swiper.currentBreakpoint === breakpoint) return;

Expand Down
2 changes: 1 addition & 1 deletion src/types/swiper-options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ export interface SwiperOptions {
*
* @default 'window'
*/
breakpointsBase?: 'window' | 'container';
breakpointsBase?: 'window' | 'container' | CSSSelector;

// Observer
/**
Expand Down

0 comments on commit 44d3443

Please sign in to comment.