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

[Bug]: Autoplay fast scroll when opened in a new tab #812

Merged
merged 1 commit into from
Apr 3, 2024
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
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
Loading