UNPKG

12.8 kBJavaScriptView Raw
1/**
2 * Swiper Custom Element 10.0.4
3 * Most modern mobile touch slider and framework with hardware accelerated transitions
4 * https://swiperjs.com
5 *
6 * Copyright 2014-2023 Vladimir Kharlampidi
7 *
8 * Released under the MIT License
9 *
10 * Released on: July 8, 2023
11 */
12
13import { S as Swiper } from './shared/swiper-core.mjs';
14import { p as paramsList, n as needsNavigation, a as needsPagination, b as needsScrollbar, u as updateSwiper, c as attrToProp } from './shared/update-swiper.mjs';
15import { g as getParams } from './shared/get-element-params.mjs';
16
17/* eslint-disable spaced-comment */
18
19const SwiperCSS = `:host{--swiper-theme-color:#007aff}:host{position:relative;display:block;margin-left:auto;margin-right:auto;z-index:1}.swiper{width:100%;height:100%;margin-left:auto;margin-right:auto;position:relative;overflow:hidden;overflow:clip;list-style:none;padding:0;z-index:1;display:block}.swiper-vertical>.swiper-wrapper{flex-direction:column}.swiper-wrapper{position:relative;width:100%;height:100%;z-index:1;display:flex;transition-property:transform;transition-timing-function:var(--swiper-wrapper-transition-timing-function,initial);box-sizing:content-box}.swiper-android ::slotted(swiper-slide),.swiper-ios ::slotted(swiper-slide),.swiper-wrapper{transform:translate3d(0px,0,0)}.swiper-horizontal{touch-action:pan-y}.swiper-vertical{touch-action:pan-x}::slotted(swiper-slide){flex-shrink:0;width:100%;height:100%;position:relative;transition-property:transform;display:block}::slotted(.swiper-slide-invisible-blank){visibility:hidden}.swiper-autoheight,.swiper-autoheight ::slotted(swiper-slide){height:auto}.swiper-autoheight .swiper-wrapper{align-items:flex-start;transition-property:transform,height}.swiper-backface-hidden ::slotted(swiper-slide){transform:translateZ(0);-webkit-backface-visibility:hidden;backface-visibility:hidden}.swiper-3d.swiper-css-mode .swiper-wrapper{perspective:1200px}.swiper-3d .swiper-wrapper{transform-style:preserve-3d}.swiper-3d{perspective:1200px}.swiper-3d .swiper-cube-shadow,.swiper-3d ::slotted(swiper-slide){transform-style:preserve-3d}.swiper-css-mode>.swiper-wrapper{overflow:auto;scrollbar-width:none;-ms-overflow-style:none}.swiper-css-mode>.swiper-wrapper::-webkit-scrollbar{display:none}.swiper-css-mode ::slotted(swiper-slide){scroll-snap-align:start start}.swiper-css-mode.swiper-horizontal>.swiper-wrapper{scroll-snap-type:x mandatory}.swiper-css-mode.swiper-vertical>.swiper-wrapper{scroll-snap-type:y mandatory}.swiper-css-mode.swiper-free-mode>.swiper-wrapper{scroll-snap-type:none}.swiper-css-mode.swiper-free-mode ::slotted(swiper-slide){scroll-snap-align:none}.swiper-css-mode.swiper-centered>.swiper-wrapper::before{content:'';flex-shrink:0;order:9999}.swiper-css-mode.swiper-centered ::slotted(swiper-slide){scroll-snap-align:center center;scroll-snap-stop:always}.swiper-css-mode.swiper-centered.swiper-horizontal ::slotted(swiper-slide):first-child{margin-inline-start:var(--swiper-centered-offset-before)}.swiper-css-mode.swiper-centered.swiper-horizontal>.swiper-wrapper::before{height:100%;min-height:1px;width:var(--swiper-centered-offset-after)}.swiper-css-mode.swiper-centered.swiper-vertical ::slotted(swiper-slide):first-child{margin-block-start:var(--swiper-centered-offset-before)}.swiper-css-mode.swiper-centered.swiper-vertical>.swiper-wrapper::before{width:100%;min-width:1px;height:var(--swiper-centered-offset-after)}`
20const SwiperSlideCSS = `::slotted(.swiper-slide-shadow),::slotted(.swiper-slide-shadow-bottom),::slotted(.swiper-slide-shadow-left),::slotted(.swiper-slide-shadow-right),::slotted(.swiper-slide-shadow-top){position:absolute;left:0;top:0;width:100%;height:100%;pointer-events:none;z-index:10}::slotted(.swiper-slide-shadow){background:rgba(0,0,0,.15)}::slotted(.swiper-slide-shadow-left){background-image:linear-gradient(to left,rgba(0,0,0,.5),rgba(0,0,0,0))}::slotted(.swiper-slide-shadow-right){background-image:linear-gradient(to right,rgba(0,0,0,.5),rgba(0,0,0,0))}::slotted(.swiper-slide-shadow-top){background-image:linear-gradient(to top,rgba(0,0,0,.5),rgba(0,0,0,0))}::slotted(.swiper-slide-shadow-bottom){background-image:linear-gradient(to bottom,rgba(0,0,0,.5),rgba(0,0,0,0))}::slotted(.swiper-lazy-preloader){animation:swiper-preloader-spin 1s infinite linear;width:42px;height:42px;position:absolute;left:50%;top:50%;margin-left:-21px;margin-top:-21px;z-index:10;transform-origin:50%;box-sizing:border-box;border:4px solid var(--swiper-preloader-color,var(--swiper-theme-color));border-radius:50%;border-top-color:transparent}@keyframes swiper-preloader-spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}::slotted(.swiper-slide-shadow-cube.swiper-slide-shadow-bottom),::slotted(.swiper-slide-shadow-cube.swiper-slide-shadow-left),::slotted(.swiper-slide-shadow-cube.swiper-slide-shadow-right),::slotted(.swiper-slide-shadow-cube.swiper-slide-shadow-top){z-index:0;-webkit-backface-visibility:hidden;backface-visibility:hidden}::slotted(.swiper-slide-shadow-flip.swiper-slide-shadow-bottom),::slotted(.swiper-slide-shadow-flip.swiper-slide-shadow-left),::slotted(.swiper-slide-shadow-flip.swiper-slide-shadow-right),::slotted(.swiper-slide-shadow-flip.swiper-slide-shadow-top){z-index:0;-webkit-backface-visibility:hidden;backface-visibility:hidden}`
21
22class DummyHTMLElement {}
23const ClassToExtend = typeof window === 'undefined' || typeof HTMLElement === 'undefined' ? DummyHTMLElement : HTMLElement;
24const arrowSvg = `<svg width="11" height="20" viewBox="0 0 11 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0.38296 20.0762C0.111788 19.805 0.111788 19.3654 0.38296 19.0942L9.19758 10.2796L0.38296 1.46497C0.111788 1.19379 0.111788 0.754138 0.38296 0.482966C0.654131 0.211794 1.09379 0.211794 1.36496 0.482966L10.4341 9.55214C10.8359 9.9539 10.8359 10.6053 10.4341 11.007L1.36496 20.0762C1.09379 20.3474 0.654131 20.3474 0.38296 20.0762Z" fill="currentColor"/></svg>
25 `;
26const addStyle = (shadowRoot, styles) => {
27 if (typeof CSSStyleSheet !== 'undefined' && shadowRoot.adoptedStyleSheets) {
28 const styleSheet = new CSSStyleSheet();
29 styleSheet.replaceSync(styles);
30 shadowRoot.adoptedStyleSheets = [styleSheet];
31 } else {
32 const style = document.createElement('style');
33 style.rel = 'stylesheet';
34 style.textContent = styles;
35 shadowRoot.appendChild(style);
36 }
37};
38class SwiperContainer extends ClassToExtend {
39 constructor() {
40 super();
41 this.attachShadow({
42 mode: 'open'
43 });
44 }
45 static get nextButtonSvg() {
46 return arrowSvg;
47 }
48 static get prevButtonSvg() {
49 return arrowSvg.replace('/></svg>', ' transform-origin="center" transform="rotate(180)"/></svg>');
50 }
51 cssStyles() {
52 return [SwiperCSS,
53 // eslint-disable-line
54 ...(this.injectStyles && Array.isArray(this.injectStyles) ? this.injectStyles : [])].join('\n');
55 }
56 cssLinks() {
57 return this.injectStylesUrls || [];
58 }
59 render() {
60 if (this.rendered) return;
61
62 // local styles
63 const localStyles = this.cssStyles();
64 if (localStyles.length) {
65 addStyle(this.shadowRoot, localStyles);
66 }
67 this.cssLinks().forEach(url => {
68 const linkExists = this.shadowRoot.querySelector(`link[href="${url}"]`);
69 if (linkExists) return;
70 const linkEl = document.createElement('link');
71 linkEl.rel = 'stylesheet';
72 linkEl.href = url;
73 this.shadowRoot.appendChild(linkEl);
74 });
75 // prettier-ignore
76 const el = document.createElement('div');
77 el.classList.add('swiper');
78 el.part = 'container';
79 // prettier-ignore
80 el.innerHTML = `
81 <slot name="container-start"></slot>
82 <div class="swiper-wrapper" part="wrapper">
83 <slot></slot>
84 </div>
85 <slot name="container-end"></slot>
86 ${needsNavigation(this.passedParams) ? `
87 <div part="button-prev" class="swiper-button-prev">${this.constructor.prevButtonSvg}</div>
88 <div part="button-next" class="swiper-button-next">${this.constructor.nextButtonSvg}</div>
89 ` : ''}
90 ${needsPagination(this.passedParams) ? `
91 <div part="pagination" class="swiper-pagination"></div>
92 ` : ''}
93 ${needsScrollbar(this.passedParams) ? `
94 <div part="scrollbar" class="swiper-scrollbar"></div>
95 ` : ''}
96 `;
97 this.shadowRoot.appendChild(el);
98 this.rendered = true;
99 }
100 initialize() {
101 var _this = this;
102 if (this.initialized) return;
103 this.initialized = true;
104 const {
105 params: swiperParams,
106 passedParams
107 } = getParams(this);
108 this.swiperParams = swiperParams;
109 this.passedParams = passedParams;
110 delete this.swiperParams.init;
111 this.render();
112 // eslint-disable-next-line
113 this.swiper = new Swiper(this.shadowRoot.querySelector('.swiper'), {
114 ...swiperParams,
115 touchEventsTarget: 'container',
116 ...(swiperParams.virtual ? {} : {
117 observer: true
118 }),
119 onAny: function (name) {
120 const eventName = swiperParams.eventsPrefix ? `${swiperParams.eventsPrefix}${name.toLowerCase()}` : name.toLowerCase();
121 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
122 args[_key - 1] = arguments[_key];
123 }
124 const event = new CustomEvent(eventName, {
125 detail: args,
126 bubbles: true,
127 cancelable: true
128 });
129 _this.dispatchEvent(event);
130 }
131 });
132 }
133 connectedCallback() {
134 if (this.initialized && this.nested && this.closest('swiper-slide') && this.closest('swiper-slide').swiperLoopMoveDOM) {
135 return;
136 }
137 if (this.init === false || this.getAttribute('init') === 'false') {
138 return;
139 }
140 this.initialize();
141 }
142 disconnectedCallback() {
143 if (this.nested && this.closest('swiper-slide') && this.closest('swiper-slide').swiperLoopMoveDOM) {
144 return;
145 }
146 if (this.swiper && this.swiper.destroy) {
147 this.swiper.destroy();
148 }
149 this.initialized = false;
150 }
151 updateSwiperOnPropChange(propName, propValue) {
152 const {
153 params: swiperParams,
154 passedParams
155 } = getParams(this, propName, propValue);
156 this.passedParams = passedParams;
157 this.swiperParams = swiperParams;
158 updateSwiper({
159 swiper: this.swiper,
160 passedParams: this.passedParams,
161 changedParams: [attrToProp(propName)],
162 ...(propName === 'navigation' && passedParams[propName] ? {
163 prevEl: '.swiper-button-prev',
164 nextEl: '.swiper-button-next'
165 } : {}),
166 ...(propName === 'pagination' && passedParams[propName] ? {
167 paginationEl: '.swiper-pagination'
168 } : {}),
169 ...(propName === 'scrollbar' && passedParams[propName] ? {
170 scrollbarEl: '.swiper-scrollbar'
171 } : {})
172 });
173 }
174 attributeChangedCallback(attr, prevValue, newValue) {
175 if (!this.initialized) return;
176 if (prevValue === 'true' && newValue === null) {
177 newValue = false;
178 }
179 this.updateSwiperOnPropChange(attr, newValue);
180 }
181 static get observedAttributes() {
182 const attrs = paramsList.filter(param => param.includes('_')).map(param => param.replace(/[A-Z]/g, v => `-${v}`).replace('_', '').toLowerCase());
183 return attrs;
184 }
185}
186paramsList.forEach(paramName => {
187 if (paramName === 'init') return;
188 paramName = paramName.replace('_', '');
189 Object.defineProperty(SwiperContainer.prototype, paramName, {
190 configurable: true,
191 get() {
192 return (this.passedParams || {})[paramName];
193 },
194 set(value) {
195 if (!this.passedParams) this.passedParams = {};
196 this.passedParams[paramName] = value;
197 if (!this.initialized) return;
198 this.updateSwiperOnPropChange(paramName);
199 }
200 });
201});
202class SwiperSlide extends ClassToExtend {
203 constructor() {
204 super();
205 this.attachShadow({
206 mode: 'open'
207 });
208 }
209 render() {
210 const lazy = this.lazy || this.getAttribute('lazy') === '' || this.getAttribute('lazy') === 'true';
211 addStyle(this.shadowRoot, SwiperSlideCSS);
212 this.shadowRoot.appendChild(document.createElement('slot'));
213 if (lazy) {
214 const lazyDiv = document.createElement('div');
215 lazyDiv.classList.add('swiper-lazy-preloader');
216 this.appendChild(lazyDiv);
217 }
218 }
219 initialize() {
220 this.render();
221 }
222 connectedCallback() {
223 this.initialize();
224 }
225}
226
227// eslint-disable-next-line
228const register = () => {
229 if (typeof window === 'undefined') return;
230 if (!window.customElements.get('swiper-container')) window.customElements.define('swiper-container', SwiperContainer);
231 if (!window.customElements.get('swiper-slide')) window.customElements.define('swiper-slide', SwiperSlide);
232};
233if (typeof window !== 'undefined') {
234 window.SwiperElementRegisterParams = params => {
235 paramsList.push(...params);
236 };
237}
238
239export { SwiperContainer, SwiperSlide, register };