Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-yaks-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackla/widget-utils": patch
---

Resolve bug around autoplay not working
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"new-change": "changeset",
"presnapshot": "npm run new-change",
"snapshot": "npx changeset version --snapshot",
"postsnapshot": "npm publish --no-git-checks"
"postsnapshot": "npm publish --no-git-checks --tag snapshot"
},
"author": "",
"license": "ISC",
Expand Down
62 changes: 62 additions & 0 deletions src/libs/extensions/swiper/swiper.extension.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Autoplay } from "swiper/modules"
import { establishSwiperConfig } from "./swiper.extension"

beforeAll(() => {
document.body.innerHTML = `
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
`
})

describe("establishSwiperConfig", () => {
const userProperties = {
prev: window.document.querySelector(".swiper-button-prev") as HTMLElement,
next: window.document.querySelector(".swiper-button-next") as HTMLElement,
paramsOverrides: {
slidesPerView: 3,
spaceBetween: 10
}
}
it("should create a valid swiper config and not contain autoplay", () => {
const config = establishSwiperConfig(userProperties)
expect(config).toBeDefined()

if (config.navigation && typeof config.navigation === "object") {
expect(config.navigation.nextEl).toBe(userProperties.next)
expect(config.navigation.prevEl).toBe(userProperties.prev)
} else {
throw new Error("config.navigation is not NavigationOptions")
}

expect(config.slidesPerView).toBe(3)
expect(config.spaceBetween).toBe(10)
expect(config.modules).not.toContain(Autoplay)
})

it("should create a valid swiper config and contain autoplay", () => {
const mutatedUserProperties = {
...userProperties,
paramsOverrides: {
autoplay: {
delay: 3000,
disableOnInteraction: false,
pauseOnMouseEnter: true
}
}
}

const config = establishSwiperConfig(mutatedUserProperties)
expect(config).toBeDefined()

if (config.navigation && typeof config.navigation === "object") {
expect(config.navigation.nextEl).toBe(userProperties.next)
expect(config.navigation.prevEl).toBe(userProperties.prev)
} else {
throw new Error("config.navigation is not NavigationOptions")
}

expect(config.slidesPerView).toBe(undefined)
expect(config.spaceBetween).toBe(10)
expect(config.modules).toContain(Autoplay)
})
})
66 changes: 41 additions & 25 deletions src/libs/extensions/swiper/swiper.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Pagination
} from "swiper/modules"
import Swiper from "swiper"
import { SwiperOptions } from "swiper/types"

export interface SwiperWithExtensions extends Swiper {
getSlideIndex?: (element: HTMLElement) => number | undefined
Expand All @@ -25,6 +26,44 @@ export type LookupAttr = {
value: string
}

type SwiperConfigContainer = {
prev: HTMLElement | null | undefined
next: HTMLElement | null | undefined
paramsOverrides: SwiperOptions | undefined
}

export function establishSwiperConfig(userConfig: SwiperConfigContainer) {
const { prev, next, paramsOverrides } = userConfig
const config = {
modules: [Navigation, Manipulation, Keyboard, Mousewheel, EffectCoverflow, Pagination, FreeMode],
spaceBetween: 10,
observer: true,
grabCursor: true,
allowTouchMove: true,
direction: "horizontal",
watchSlidesProgress: true,
normalizeSlideIndex: true,
watchOverflow: true,
mousewheel: {
enabled: false
},
touchStartPreventDefault: false,
navigation: {
enabled: !!(prev && next),
nextEl: next,
prevEl: prev
},
resizeObserver: true,
...paramsOverrides
}

if (config.autoplay) {
config.modules.push(Autoplay)
}

return config as SwiperOptions
}

export function initializeSwiper(sdk: ISdk, swiperProps: SwiperProps) {
// check if constructor is available
if (!window.ugc.libs.Swiper) {
Expand Down Expand Up @@ -64,32 +103,9 @@ export function initializeSwiper(sdk: ISdk, swiperProps: SwiperProps) {
window.ugc.swiperContainer[mutatedId] = { pageIndex: 1 }
}

const settings = new window.ugc.libs.Swiper(widgetSelector, {
modules: [Navigation, Manipulation, Keyboard, Mousewheel, EffectCoverflow, Pagination, FreeMode],
spaceBetween: 10,
observer: true,
grabCursor: true,
allowTouchMove: true,
direction: "horizontal",
watchSlidesProgress: true,
normalizeSlideIndex: true,
watchOverflow: true,
mousewheel: {
enabled: false
},
touchStartPreventDefault: false,
navigation: {
enabled: !!(prev && next),
nextEl: next,
prevEl: prev
},
resizeObserver: true,
...paramsOverrides
})
const config = establishSwiperConfig({ prev, next, paramsOverrides })

if (settings.autoplay) {
settings.modules.push(Autoplay)
}
const settings = new window.ugc.libs.Swiper(widgetSelector, config)

window.ugc.swiperContainer[mutatedId]!.instance = settings
}
Expand Down
Loading