fix: swiper

This commit is contained in:
ner committed 2024-03-25 15:31:17 +02:00
1 parent 1b87355925
commit 1aa145a98c
13 files changed
+125 -123

No files matched your search

+37 -28
View File
@@ -1,12 +1,12 @@
'use client';
import { useEffect } from 'react';
import { useEffect, useMemo, useRef } from 'react';
import { createPortal } from 'react-dom';
import { useAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils';
import { ArrowLeft, ArrowRight, SlidersHorizontal } from 'lucide-react';
import { EffectCoverflow, Virtual } from 'swiper/modules';
import { Swiper, SwiperSlide } from 'swiper/react';
import { Swiper, SwiperProps, SwiperSlide } from 'swiper/react';
import Card from '@/components/Movie/Card';
import Movie from '@/components/Movie/Preview';
@@ -20,15 +20,24 @@ import useRandomMovie from './useRandomMovie';
const isShowFilterAtom = atomWithStorage('is-show-filters', false);
const creativeEffectProps = {
const slideEffectProps: SwiperProps = {
effect: 'coverflow',
coverflowEffect: {
slideShadows: false,
depth: 200
},
virtual: {
addSlidesAfter: 3,
addSlidesBefore: 3
},
modules: [EffectCoverflow, Virtual]
};
export default function Container() {
const [isShowFilter, setIsShowFilter] = useAtom(isShowFilterAtom);
const { ref, hasPrevious, movies, movie, next, previous, index, onIndexChange } = useRandomMovie();
const { ref, hasPrevious, movies, movie, next, previous, index, onDrag, onIndexChange } = useRandomMovie();
const isMounted = useMounted();
const initialIndex = useRef(index);
useEffect(() => {
const queryClient = getQueryClient();
@@ -36,6 +45,29 @@ export default function Container() {
queryClient.prefetchQuery({ queryKey: ['genres'], queryFn: () => genres() });
}, []);
const slides = useMemo(
() => (
<Swiper
onSwiper={(instance) => (ref.current = instance)}
lazyPreloadPrevNext={3}
longSwipesRatio={0.2}
speed={500}
initialSlide={initialIndex.current}
onActiveIndexChange={onIndexChange}
onTouchMove={(swiper) => onDrag(swiper)}
className='xs:w-[300px] w-full shrink-0'
{...slideEffectProps}
>
{movies?.map((movie, index) => (
<SwiperSlide key={movie.id} virtualIndex={index}>
<Card fit movie={movie} />
</SwiperSlide>
))}
</Swiper>
),
[movies, onDrag, onIndexChange, ref]
);
return (
<>
{isShowFilter && <Filter />}
@@ -51,30 +83,7 @@ export default function Container() {
</Button>,
document.getElementById('actions')!
)}
<Movie
movie={movie}
className='bg-background rounded-md'
renderCard={() => (
<Swiper
onSwiper={(instance) => (ref.current = instance)}
lazyPreloadPrevNext={3}
resistance={false}
longSwipesRatio={0.3}
speed={400}
virtual
initialSlide={index}
onActiveIndexChange={onIndexChange}
className='w-[300px] shrink-0'
{...creativeEffectProps}
>
{movies?.map((movie, index) => (
<SwiperSlide key={movie.id} virtualIndex={index}>
<Card movie={movie} />
</SwiperSlide>
))}
</Swiper>
)}
>
<Movie movie={movie} className='bg-background rounded-md' card={slides}>
<div className='gap-2 flex w-full bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 lg:static max-sm:z-10 max-sm:fixed max-sm:bottom-0 max-sm:left-0 max-sm:w-full max-sm:p-2 max-sm:px-2 max-sm:mb-0'>
<Button className='grow h-8' onClick={previous} disabled={!hasPrevious} title='Previous movie'>
<ArrowLeft size={24} />
+26 -8
View File
@@ -1,6 +1,6 @@
'use client';
import { useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Swiper } from 'swiper/types';
@@ -17,6 +17,7 @@ const useRandomMovie = () => {
const indexByFilter = previousIndexes[JSON.stringify(filters)];
const [index, setIndex] = useState(indexByFilter);
const ref = useRef<Swiper>();
const timeout = useRef<NodeJS.Timeout>();
const { data: movies, refetch } = useQuery({
queryKey: ['random-movie', filters],
@@ -49,13 +50,30 @@ const useRandomMovie = () => {
movies,
movie: movies?.[index],
hasPrevious: index > 0,
onIndexChange: ({ activeIndex }: Swiper) => {
if ((activeIndex && activeIndex % UPDATE_RATE === 0) || movies!.length < UPDATE_RATE) {
refetch();
}
previousIndexes[JSON.stringify(filters)] = activeIndex;
setIndex(activeIndex);
},
onIndexChange: useCallback(
({ activeIndex }: Swiper) => {
if ((activeIndex && activeIndex % UPDATE_RATE === 0) || movies!.length < UPDATE_RATE) {
refetch();
}
clearTimeout(timeout.current);
previousIndexes[JSON.stringify(filters)] = activeIndex;
timeout.current = setTimeout(() => {
setIndex(activeIndex);
}, 100);
},
[movies, filters]
),
onDrag: useCallback(
({ activeIndex }: Swiper) => {
clearTimeout(timeout.current);
timeout.current = setTimeout(() => {
if (index === activeIndex) return;
setIndex(activeIndex);
}, 100);
},
[index]
),
next,
previous,
index,