Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix #644 #660

Merged
merged 1 commit into from
Dec 22, 2023
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
23 changes: 17 additions & 6 deletions packages/embla-carousel-autoplay/src/components/Autoplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {
let emblaApi: EmblaCarouselType
let destroyed: boolean
let playing = false
let wasPlaying = false
let resume = true
let jump = false
let animationFrame = 0
let timer = 0
Expand All @@ -58,13 +58,22 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {
const root = (options.rootNode && options.rootNode(emblaRoot)) || emblaRoot

emblaApi.on('pointerDown', clearTimer)
if (!options.stopOnInteraction) emblaApi.on('pointerUp', startTimer)

if (!options.stopOnInteraction) {
emblaApi.on('pointerUp', startTimer)
}

if (options.stopOnMouseEnter) {
eventStore.add(root, 'mouseenter', clearTimer)
eventStore.add(root, 'mouseenter', () => {
resume = false
clearTimer()
})

if (!options.stopOnInteraction) {
eventStore.add(root, 'mouseleave', startTimer)
eventStore.add(root, 'mouseleave', () => {
resume = true
startTimer()
})
}
}

Expand All @@ -78,11 +87,11 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {

eventStore.add(ownerDocument, 'visibilitychange', () => {
if (ownerDocument.visibilityState === 'hidden') {
wasPlaying = playing
resume = playing
return clearTimer()
}

if (wasPlaying) startTimer()
if (resume) startTimer()
})

if (options.playOnInit) {
Expand All @@ -103,6 +112,7 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {

function startTimer(): void {
if (destroyed) return
if (!resume) return
if (!playing) emblaApi.emit('autoplay:play')
const { ownerWindow } = emblaApi.internalEngine()
ownerWindow.clearInterval(timer)
Expand All @@ -121,6 +131,7 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {

function play(jumpOverride?: boolean): void {
if (typeof jumpOverride !== 'undefined') jump = jumpOverride
resume = true
startTimer()
}

Expand Down
1 change: 0 additions & 1 deletion playgrounds/embla-carousel-playground-vanilla/src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
font-size: 3.6rem;
font-weight: 900;
text-align: center;
display: none;
}