[bugfix]: carousel fixes
This commit is contained in:
parent
74075fc374
commit
9113c6cc2e
1 changed files with 44 additions and 21 deletions
|
@ -4,6 +4,7 @@ import {
|
||||||
ReactNode,
|
ReactNode,
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
useLayoutEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
|
@ -117,6 +118,7 @@ export const SwiperGridCarousel = ({
|
||||||
const swiperRef = useRef<SwiperCore | any>(null);
|
const swiperRef = useRef<SwiperCore | any>(null);
|
||||||
const playButtonBehavior = usePlayButtonBehavior();
|
const playButtonBehavior = usePlayButtonBehavior();
|
||||||
const handlePlayQueueAdd = usePlayQueueAdd();
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
||||||
|
const [slideCount, setSlideCount] = useState(4);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
swiperRef.current?.slideTo(0, 0);
|
swiperRef.current?.slideTo(0, 0);
|
||||||
|
@ -191,23 +193,24 @@ export const SwiperGridCarousel = ({
|
||||||
|
|
||||||
const handleNext = useCallback(() => {
|
const handleNext = useCallback(() => {
|
||||||
const activeIndex = swiperRef?.current?.activeIndex || 0;
|
const activeIndex = swiperRef?.current?.activeIndex || 0;
|
||||||
const slidesPerView = Math.round(Number(swiperProps?.slidesPerView || 4));
|
const slidesPerView = Math.round(Number(swiperProps?.slidesPerView || slideCount));
|
||||||
swiperRef?.current?.slideTo(activeIndex + slidesPerView);
|
swiperRef?.current?.slideTo(activeIndex + slidesPerView);
|
||||||
}, [swiperProps?.slidesPerView]);
|
}, [slideCount, swiperProps?.slidesPerView]);
|
||||||
|
|
||||||
const handlePrev = useCallback(() => {
|
const handlePrev = useCallback(() => {
|
||||||
const activeIndex = swiperRef?.current?.activeIndex || 0;
|
const activeIndex = swiperRef?.current?.activeIndex || 0;
|
||||||
const slidesPerView = Math.round(Number(swiperProps?.slidesPerView || 4));
|
const slidesPerView = Math.round(Number(swiperProps?.slidesPerView || slideCount));
|
||||||
swiperRef?.current?.slideTo(activeIndex - slidesPerView);
|
swiperRef?.current?.slideTo(activeIndex - slidesPerView);
|
||||||
}, [swiperProps?.slidesPerView]);
|
}, [slideCount, swiperProps?.slidesPerView]);
|
||||||
|
|
||||||
const handleOnSlideChange = useCallback((e: SwiperCore) => {
|
const handleOnSlideChange = useCallback((e: SwiperCore) => {
|
||||||
const { slides, isEnd, isBeginning, params } = e;
|
const { slides, isEnd, isBeginning, params } = e;
|
||||||
if (isEnd || isBeginning) return;
|
if (isEnd || isBeginning) return;
|
||||||
|
|
||||||
|
const slideCount = (params.slidesPerView as number | undefined) || 4;
|
||||||
setPagination({
|
setPagination({
|
||||||
hasNextPage: (params?.slidesPerView || 4) < slides.length,
|
hasNextPage: slideCount < slides.length,
|
||||||
hasPreviousPage: (params?.slidesPerView || 4) < slides.length,
|
hasPreviousPage: slideCount < slides.length,
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
@ -215,39 +218,61 @@ export const SwiperGridCarousel = ({
|
||||||
const { slides, isEnd, isBeginning, params } = e;
|
const { slides, isEnd, isBeginning, params } = e;
|
||||||
if (isEnd || isBeginning) return;
|
if (isEnd || isBeginning) return;
|
||||||
|
|
||||||
|
const slideCount = (params.slidesPerView as number | undefined) || 4;
|
||||||
setPagination({
|
setPagination({
|
||||||
hasNextPage: (params.slidesPerView || 4) < slides.length,
|
hasNextPage: slideCount < slides.length,
|
||||||
hasPreviousPage: (params.slidesPerView || 4) < slides.length,
|
hasPreviousPage: slideCount < slides.length,
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleOnReachEnd = useCallback((e: SwiperCore) => {
|
const handleOnReachEnd = useCallback((e: SwiperCore) => {
|
||||||
const { slides, params } = e;
|
const { slides, params } = e;
|
||||||
|
|
||||||
|
const slideCount = (params.slidesPerView as number | undefined) || 4;
|
||||||
setPagination({
|
setPagination({
|
||||||
hasNextPage: false,
|
hasNextPage: false,
|
||||||
hasPreviousPage: (params.slidesPerView || 4) < slides.length,
|
hasPreviousPage: slideCount < slides.length,
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleOnReachBeginning = useCallback((e: SwiperCore) => {
|
const handleOnReachBeginning = useCallback((e: SwiperCore) => {
|
||||||
const { slides, params } = e;
|
const { slides, params } = e;
|
||||||
|
|
||||||
|
const slideCount = (params.slidesPerView as number | undefined) || 4;
|
||||||
setPagination({
|
setPagination({
|
||||||
hasNextPage: (params.slidesPerView || 4) < slides.length,
|
hasNextPage: slideCount < slides.length,
|
||||||
hasPreviousPage: false,
|
hasPreviousPage: false,
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleOnResize = useCallback((e: SwiperCore) => {
|
useLayoutEffect(() => {
|
||||||
if (!e) return;
|
const handleResize = () => {
|
||||||
const { width } = e;
|
const { activeIndex, params, slides, width } =
|
||||||
const slidesPerView = getSlidesPerView(width);
|
(swiperRef.current as SwiperCore | undefined) ?? {};
|
||||||
if (!e.params) return;
|
|
||||||
e.params.slidesPerView = slidesPerView;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const throttledOnResize = throttle(handleOnResize, 200);
|
if (width) {
|
||||||
|
const slidesPerView = getSlidesPerView(width);
|
||||||
|
setSlideCount(slidesPerView);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeIndex !== undefined && slides && params?.slidesPerView) {
|
||||||
|
const slideCount = (params.slidesPerView as number | undefined) || 4;
|
||||||
|
setPagination({
|
||||||
|
hasNextPage: activeIndex + slideCount < slides.length,
|
||||||
|
hasPreviousPage: activeIndex > 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
handleResize();
|
||||||
|
|
||||||
|
const throttledResize = throttle(handleResize, 200);
|
||||||
|
window.addEventListener('resize', throttledResize);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('resize', throttledResize);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CarouselContainer
|
<CarouselContainer
|
||||||
|
@ -266,16 +291,14 @@ export const SwiperGridCarousel = ({
|
||||||
ref={swiperRef}
|
ref={swiperRef}
|
||||||
resizeObserver
|
resizeObserver
|
||||||
modules={[Virtual]}
|
modules={[Virtual]}
|
||||||
slidesPerView={4}
|
slidesPerView={slideCount}
|
||||||
spaceBetween={20}
|
spaceBetween={20}
|
||||||
style={{ height: '100%', width: '100%' }}
|
style={{ height: '100%', width: '100%' }}
|
||||||
onBeforeInit={(swiper) => {
|
onBeforeInit={(swiper) => {
|
||||||
swiperRef.current = swiper;
|
swiperRef.current = swiper;
|
||||||
}}
|
}}
|
||||||
onBeforeResize={handleOnResize}
|
|
||||||
onReachBeginning={handleOnReachBeginning}
|
onReachBeginning={handleOnReachBeginning}
|
||||||
onReachEnd={handleOnReachEnd}
|
onReachEnd={handleOnReachEnd}
|
||||||
onResize={throttledOnResize}
|
|
||||||
onSlideChange={handleOnSlideChange}
|
onSlideChange={handleOnSlideChange}
|
||||||
onZoomChange={handleOnZoomChange}
|
onZoomChange={handleOnZoomChange}
|
||||||
{...swiperProps}
|
{...swiperProps}
|
||||||
|
|
Reference in a new issue