Skip to content

Commit

Permalink
Merge pull request #812 from davidjerleke/bug/#790
Browse files Browse the repository at this point in the history
[Bug]: Autoplay fast scroll when opened in a new tab
  • Loading branch information
davidjerleke committed Apr 3, 2024
2 parents e453e61 + 3587fc3 commit 625bbd7
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,11 @@ function AutoScroll(userOptions: AutoScrollOptionsType = {}): AutoScrollType {
}
}

if (options.playOnInit) {
emblaApi.on('init', startScroll).on('reInit', startScroll)
}
if (options.playOnInit) startScroll()
}

function destroy(): void {
emblaApi
.off('init', startScroll)
.off('reInit', startScroll)
.off('pointerDown', stopScroll)
.off('pointerUp', startScrollOnSettle)
.off('settle', onSettle)
Expand Down
48 changes: 20 additions & 28 deletions packages/embla-carousel-autoplay/src/components/Autoplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {
let playing = false
let resume = true
let jump = false
let animationFrame = 0
let timer = 0

function init(
Expand Down Expand Up @@ -89,20 +88,12 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {

eventStore.add(ownerDocument, 'visibilitychange', visibilityChange)

if (options.playOnInit) {
emblaApi.on('init', startTimer).on('reInit', startTimer)
}
if (options.playOnInit && !documentIsHidden()) startTimer()
}

function destroy(): void {
emblaApi
.off('init', startTimer)
.off('reInit', startTimer)
.off('pointerDown', stopTimer)
.off('pointerUp', startTimer)
emblaApi.off('pointerDown', stopTimer).off('pointerUp', startTimer)
stopTimer()
cancelAnimationFrame(animationFrame)
animationFrame = 0
destroyed = true
playing = false
}
Expand All @@ -127,16 +118,19 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {
}

function visibilityChange(): void {
const { ownerDocument } = emblaApi.internalEngine()

if (ownerDocument.visibilityState === 'hidden') {
if (documentIsHidden()) {
resume = playing
return stopTimer()
}

if (resume) startTimer()
}

function documentIsHidden(): boolean {
const { ownerDocument } = emblaApi.internalEngine()
return ownerDocument.visibilityState === 'hidden'
}

function play(jumpOverride?: boolean): void {
if (typeof jumpOverride !== 'undefined') jump = jumpOverride
resume = true
Expand All @@ -156,20 +150,18 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {
}

function next(): void {
animationFrame = requestAnimationFrame(() => {
const { index } = emblaApi.internalEngine()
const nextIndex = index.clone().add(1).get()
const lastIndex = emblaApi.scrollSnapList().length - 1
const kill = options.stopOnLastSnap && nextIndex === lastIndex

if (kill) stopTimer()

if (emblaApi.canScrollNext()) {
emblaApi.scrollNext(jump)
} else {
emblaApi.scrollTo(0, jump)
}
})
const { index } = emblaApi.internalEngine()
const nextIndex = index.clone().add(1).get()
const lastIndex = emblaApi.scrollSnapList().length - 1
const kill = options.stopOnLastSnap && nextIndex === lastIndex

if (kill) stopTimer()

if (emblaApi.canScrollNext()) {
emblaApi.scrollNext(jump)
} else {
emblaApi.scrollTo(0, jump)
}
}

const self: AutoplayType = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const EmblaCarousel = (props) => {
emblaApi
.on('autoScroll:play', () => setIsPlaying(true))
.on('autoScroll:stop', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(autoScroll.isPlaying()))
}, [emblaApi])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
emblaApi
.on('autoScroll:play', () => setIsPlaying(true))
.on('autoScroll:stop', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(autoScroll.isPlaying()))
}, [emblaApi])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const EmblaCarousel = (props) => {
emblaApi
.on('autoplay:play', () => setIsPlaying(true))
.on('autoplay:stop', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(autoplay.isPlaying()))
}, [emblaApi])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
emblaApi
.on('autoplay:play', () => setIsPlaying(true))
.on('autoplay:stop', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(autoplay.isPlaying()))
}, [emblaApi])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
emblaApi
.on('autoScroll:play', () => setIsPlaying(true))
.on('autoScroll:stop', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(autoScroll.isPlaying()))
}, [emblaApi])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
emblaApi
.on('autoplay:play', () => setIsPlaying(true))
.on('autoplay:stop', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(false))
.on('reInit', () => setIsPlaying(autoplay.isPlaying()))
}, [emblaApi])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export const setupInfiniteScroll = (emblaApi, loadMoreCallback) => {

emblaApi.reInit()
const newEngine = emblaApi.internalEngine()
const copyEngineModules = ['scrollBody', 'location', 'target']
const copyEngineModules = [
'scrollBody',
'location',
'offsetLocation',
'target'
]
copyEngineModules.forEach((engineModule) =>
Object.assign(newEngine[engineModule], oldEngine[engineModule])
)
Expand Down

0 comments on commit 625bbd7

Please sign in to comment.