Skip to content

Commit

Permalink
Merge pull request #660 from davidjerleke/bug/#644
Browse files Browse the repository at this point in the history
Bugfix #644
  • Loading branch information
davidjerleke authored Dec 22, 2023
2 parents ded98fe + 55c2bd9 commit 83c171a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
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;
}

0 comments on commit 83c171a

Please sign in to comment.