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

[Feat]: Scroll to snap that holds a specific slide #1086

Merged
merged 1 commit into from
Dec 15, 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 @@ -204,7 +204,7 @@ function Autoplay(userOptions: AutoplayOptionsType = {}): AutoplayType {
if (emblaApi.canScrollNext()) {
emblaApi.scrollNext(jump)
} else {
emblaApi.scrollTo(0, jump)
emblaApi.scrollToSnap(0, jump)
}

emblaApi.emit('autoplay:select', null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const SLIDES = arrayFromNumber(5)
const OPTIONS: EmblaOptionsType = {
dragFree: true,
containScroll: 'keepSnaps',
watchSlides: false,
watchResize: false
slideChanges: true,
resize: false
}
const STYLES = examplesCarouselInfiniteScrollStyles()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useDotButton = (
const onDotButtonClick = useCallback(
(index) => {
if (!emblaApi) return
emblaApi.scrollTo(index)
emblaApi.scrollToSnap(index)
/*__NAV_AUTOPLAY_REPLACE_START__*/
if (onButtonClick) onButtonClick(emblaApi)
/*__NAV_AUTOPLAY_REPLACE_END__*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const useDotButton = (
const onDotButtonClick = useCallback(
(index: number) => {
if (!emblaApi) return
emblaApi.scrollTo(index)
emblaApi.scrollToSnap(index)
/*__NAV_AUTOPLAY_REPLACE_START__*/
if (onButtonClick) onButtonClick(emblaApi)
/*__NAV_AUTOPLAY_REPLACE_END__*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useDotButton = (emblaApi, onButtonClick) => {
const onDotButtonClick = useCallback(
(index) => {
if (!emblaApi) return
emblaApi.scrollTo(index)
emblaApi.scrollToSnap(index)
if (onButtonClick) onButtonClick(emblaApi)
},
[emblaApi, onButtonClick]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useDotButton = (
const onDotButtonClick = useCallback(
(index: number) => {
if (!emblaApi) return
emblaApi.scrollTo(index)
emblaApi.scrollToSnap(index)
if (onButtonClick) onButtonClick(emblaApi)
},
[emblaApi, onButtonClick]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,7 @@ const EmblaCarousel = (props) => {
const [slides, setSlides] = useState(propSlides)
const [hasMoreToLoad, setHasMoreToLoad] = useState(true)
const [loadingMore, setLoadingMore] = useState(false)

const [emblaRef, emblaApi] = useEmblaCarousel({
...options,
watchSlides: (emblaApi) => {
const reloadEmbla = () => {
const oldEngine = emblaApi.internalEngine()

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

newEngine.translate.to(oldEngine.location.get())
const { index } = newEngine.scrollTarget.byDistance(0, false)
newEngine.index.set(index)
newEngine.animation.start()

setLoadingMore(false)
listenForScrollRef.current = true
}

const reloadAfterPointerUp = () => {
emblaApi.off('pointerup', reloadAfterPointerUp)
reloadEmbla()
}

const engine = emblaApi.internalEngine()

if (hasMoreToLoadRef.current && engine.dragHandler.pointerDown()) {
const boundsActive = engine.limit.reachedMax(engine.target.get())
engine.scrollBounds.toggleActive(boundsActive)
emblaApi.on('pointerup', reloadAfterPointerUp)
} else {
reloadEmbla()
}
}
})
const [emblaRef, emblaApi] = useEmblaCarousel(options)

const {
prevBtnDisabled,
Expand All @@ -74,6 +30,50 @@ const EmblaCarousel = (props) => {
onNextButtonClick
} = usePrevNextButtons(emblaApi)

const onSlideChanges = useCallback((emblaApi) => {
const reloadEmbla = () => {
const oldEngine = emblaApi.internalEngine()

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

newEngine.translate.to(oldEngine.location.get())
const { index } = newEngine.scrollTarget.byDistance(0, false)
newEngine.index.set(index)
newEngine.animation.start()

setLoadingMore(false)
listenForScrollRef.current = true
}

const reloadAfterPointerUp = () => {
emblaApi.off('pointerup', reloadAfterPointerUp)
reloadEmbla()
}

const engine = emblaApi.internalEngine()

if (hasMoreToLoadRef.current && engine.dragHandler.pointerDown()) {
const boundsActive = engine.limit.reachedMax(engine.target.get())
engine.scrollBounds.toggleActive(boundsActive)
emblaApi.on('pointerup', reloadAfterPointerUp)
} else {
reloadEmbla()
}

return false
}, [])

const onScroll = useCallback((emblaApi) => {
if (!listenForScrollRef.current) return

Expand Down Expand Up @@ -117,7 +117,8 @@ const EmblaCarousel = (props) => {
const onResize = () => emblaApi.reInit()
window.addEventListener('resize', onResize)
emblaApi.on('destroy', () => window.removeEventListener('resize', onResize))
}, [emblaApi, addScrollListener])
emblaApi.onWatch('slideschanged', onSlideChanges)
}, [emblaApi, addScrollListener, onSlideChanges])

useEffect(() => {
hasMoreToLoadRef.current = hasMoreToLoad
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { EngineType } from 'embla-carousel/components/Engine'
import { EmblaCarouselType, EmblaOptionsType } from 'embla-carousel'
import {
EmblaCarouselType,
EmblaOptionsType,
EmblaWatchCallbackType
} from 'embla-carousel'
import useEmblaCarousel from 'embla-carousel-react'
import {
NextButton,
Expand Down Expand Up @@ -32,10 +36,17 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
const [slides, setSlides] = useState(propSlides)
const [hasMoreToLoad, setHasMoreToLoad] = useState(true)
const [loadingMore, setLoadingMore] = useState(false)
const [emblaRef, emblaApi] = useEmblaCarousel(options)

const [emblaRef, emblaApi] = useEmblaCarousel({
...options,
watchSlides: (emblaApi) => {
const {
prevBtnDisabled,
nextBtnDisabled,
onPrevButtonClick,
onNextButtonClick
} = usePrevNextButtons(emblaApi)

const onSlideChanges: EmblaWatchCallbackType<'slideschanged'> = useCallback(
(emblaApi: EmblaCarouselType) => {
const reloadEmbla = (): void => {
const oldEngine = emblaApi.internalEngine()

Expand Down Expand Up @@ -75,15 +86,11 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
} else {
reloadEmbla()
}
}
})

const {
prevBtnDisabled,
nextBtnDisabled,
onPrevButtonClick,
onNextButtonClick
} = usePrevNextButtons(emblaApi)
return false
},
[]
)

const onScroll = useCallback((emblaApi: EmblaCarouselType) => {
if (!listenForScrollRef.current) return
Expand Down Expand Up @@ -128,7 +135,8 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
const onResize = () => emblaApi.reInit()
window.addEventListener('resize', onResize)
emblaApi.on('destroy', () => window.removeEventListener('resize', onResize))
}, [emblaApi, addScrollListener])
emblaApi.onWatch('slideschanged', onSlideChanges)
}, [emblaApi, addScrollListener, onSlideChanges])

useEffect(() => {
hasMoreToLoadRef.current = hasMoreToLoad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const IosPickerItem = (props) => {
axis: 'y',
dragFree: true,
containScroll: false,
watchSlides: false
slideChanges: false
})
const rootNodeRef = useRef(null)
const totalRadius = slideCount * WHEEL_ITEM_RADIUS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const IosPickerItem: React.FC<PropType> = (props) => {
axis: 'y',
dragFree: true,
containScroll: false,
watchSlides: false
slideChanges: false
})
const rootNodeRef = useRef<HTMLDivElement>(null)
const totalRadius = slideCount * WHEEL_ITEM_RADIUS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ const EmblaCarousel = (props) => {
const onThumbClick = useCallback(
(index) => {
if (!emblaMainApi || !emblaThumbsApi) return
emblaMainApi.scrollTo(index)
emblaMainApi.scrollToSnap(index)
},
[emblaMainApi, emblaThumbsApi]
)

const onSelect = useCallback(() => {
if (!emblaMainApi || !emblaThumbsApi) return
setSelectedIndex(emblaMainApi.selectedScrollSnap())
emblaThumbsApi.scrollTo(emblaMainApi.selectedScrollSnap())
emblaThumbsApi.scrollToSnap(emblaMainApi.selectedScrollSnap())
}, [emblaMainApi, emblaThumbsApi, setSelectedIndex])

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
const onThumbClick = useCallback(
(index: number) => {
if (!emblaMainApi || !emblaThumbsApi) return
emblaMainApi.scrollTo(index)
emblaMainApi.scrollToSnap(index)
},
[emblaMainApi, emblaThumbsApi]
)

const onSelect = useCallback(() => {
if (!emblaMainApi || !emblaThumbsApi) return
setSelectedIndex(emblaMainApi.selectedScrollSnap())
emblaThumbsApi.scrollTo(emblaMainApi.selectedScrollSnap())
emblaThumbsApi.scrollToSnap(emblaMainApi.selectedScrollSnap())
}, [emblaMainApi, emblaThumbsApi, setSelectedIndex])

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const useDotButton = (
const onDotButtonClick = useCallback(
(index: number) => {
if (!emblaApi) return
emblaApi.scrollTo(index)
emblaApi.scrollToSnap(index)
/*__NAV_AUTOPLAY_REPLACE_START__*/
if (onButtonClick) onButtonClick(emblaApi)
/*__NAV_AUTOPLAY_REPLACE_END__*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useDotButton = (
const onDotButtonClick = useCallback(
(index: number) => {
if (!emblaApi) return
emblaApi.scrollTo(index)
emblaApi.scrollToSnap(index)
if (onButtonClick) onButtonClick(emblaApi)
},
[emblaApi, onButtonClick]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { EngineType } from 'embla-carousel/components/Engine'
import { EmblaCarouselType, EmblaOptionsType } from 'embla-carousel'
import {
EmblaCarouselType,
EmblaOptionsType,
EmblaWatchCallbackType
} from 'embla-carousel'
import useEmblaCarousel from 'embla-carousel-react'
import {
NextButton,
Expand Down Expand Up @@ -32,10 +36,17 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
const [slides, setSlides] = useState(propSlides)
const [hasMoreToLoad, setHasMoreToLoad] = useState(true)
const [loadingMore, setLoadingMore] = useState(false)
const [emblaRef, emblaApi] = useEmblaCarousel(options)

const [emblaRef, emblaApi] = useEmblaCarousel({
...options,
watchSlides: (emblaApi) => {
const {
prevBtnDisabled,
nextBtnDisabled,
onPrevButtonClick,
onNextButtonClick
} = usePrevNextButtons(emblaApi)

const onSlideChanges: EmblaWatchCallbackType<'slideschanged'> = useCallback(
(emblaApi: EmblaCarouselType) => {
const reloadEmbla = (): void => {
const oldEngine = emblaApi.internalEngine()

Expand Down Expand Up @@ -75,15 +86,11 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
} else {
reloadEmbla()
}
}
})

const {
prevBtnDisabled,
nextBtnDisabled,
onPrevButtonClick,
onNextButtonClick
} = usePrevNextButtons(emblaApi)
return false
},
[]
)

const onScroll = useCallback((emblaApi: EmblaCarouselType) => {
if (!listenForScrollRef.current) return
Expand Down Expand Up @@ -128,7 +135,8 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
const onResize = () => emblaApi.reInit()
window.addEventListener('resize', onResize)
emblaApi.on('destroy', () => window.removeEventListener('resize', onResize))
}, [emblaApi, addScrollListener])
emblaApi.onWatch('slideschanged', onSlideChanges)
}, [emblaApi, addScrollListener, onSlideChanges])

useEffect(() => {
hasMoreToLoadRef.current = hasMoreToLoad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const IosPickerItem: React.FC<PropType> = (props) => {
axis: 'y',
dragFree: true,
containScroll: false,
watchSlides: false
slideChanges: false
})
const rootNodeRef = useRef<HTMLDivElement>(null)
const totalRadius = slideCount * WHEEL_ITEM_RADIUS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const EmblaCarousel: React.FC<PropType> = (props) => {
const onThumbClick = useCallback(
(index: number) => {
if (!emblaMainApi || !emblaThumbsApi) return
emblaMainApi.scrollTo(index)
emblaMainApi.scrollToSnap(index)
},
[emblaMainApi, emblaThumbsApi]
)

const onSelect = useCallback(() => {
if (!emblaMainApi || !emblaThumbsApi) return
setSelectedIndex(emblaMainApi.selectedScrollSnap())
emblaThumbsApi.scrollTo(emblaMainApi.selectedScrollSnap())
emblaThumbsApi.scrollToSnap(emblaMainApi.selectedScrollSnap())
}, [emblaMainApi, emblaThumbsApi, setSelectedIndex])

useEffect(() => {
Expand Down
Loading
Loading