fix: swiper
This commit is contained in:
1 parent
1b87355925
commit
1aa145a98c
13 files changed
+125
-123
No files matched your search
+37
-28
@@ -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} />
|
||||
|
||||
Reference in new issue
Block a user