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

+1
View File
@@ -27,6 +27,7 @@ yarn-error.log*
# local env files
.env*.local
.env
# vercel
.vercel
+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,
@@ -11,7 +11,7 @@ import { Input } from '@/components/ui/input';
import useSearch from './useSearch';
const Search: FC<InstanceProps<void>> = ({ isOpen, onResolve }) => {
const Search: FC<InstanceProps<void>> = ({ onResolve }) => {
const [title, setTitle] = useState('');
const [query, setQuery] = useState('');
const timeout = useRef<NodeJS.Timeout>();
@@ -34,10 +34,11 @@ const Search: FC<InstanceProps<void>> = ({ isOpen, onResolve }) => {
const handleClose = () => {
handleReset();
return onResolve();
};
return (
<Modal className='block p-3 max-w-[950px]' isOpen={isOpen} onClose={() => onResolve()}>
<Modal className='block p-3 max-w-[950px]' onClose={handleClose}>
<div className='relative'>
<Input
ref={inputRef}
+2 -2
View File
@@ -27,7 +27,7 @@ const Card: FC<Props> = ({ movie, onClick, fit }) => {
<div
onDoubleClick={handleToggleFavorite}
className={cn(
'w-[300px] h-[450px] min-w-[300px] shrink-0 mx-auto',
'w-[300px] h-[450px] min-w-[300px] min-h-[450px] shrink-0 mx-auto',
fit ? 'w-full h-auto xs:w-[300px] xs:h-[450px]' : undefined
)}
>
@@ -37,7 +37,7 @@ const Card: FC<Props> = ({ movie, onClick, fit }) => {
title={movie.title}
size={100}
poster={movie.poster_path}
className={fit ? 'h-auto xs:h-full' : undefined}
className={fit ? 'h-auto min-h-[450px] xs:h-full' : undefined}
/>
</motion.div>
<div className='absolute left-0 top-0 w-full h-20 bg-gradient-to-t from-transparent to-black opacity-75 rounded-t-lg'>
+2 -19
View File
@@ -2,8 +2,7 @@ import { FC, Fragment, useLayoutEffect, useState } from 'react';
import { motion } from 'framer-motion';
import Card from '@/components/Movie/Card';
import Preview from '@/components/Movie/Preview';
import { Modal } from '@/components/ui/dialog';
import { showPreviewModal } from '@/components/Movie/Preview';
import filterUnknownMovies from '@/lib/filterUnknownMovies';
interface Props {
@@ -32,7 +31,6 @@ const item = {
};
const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage, withBottomGap = true, onPreviewClose }) => {
const [movie, setMovie] = useState<Movie | null>(null);
const [loader, setLoader] = useState<HTMLLIElement>();
useLayoutEffect(() => {
@@ -74,7 +72,7 @@ const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage, withBottomGap = tr
className='flex cursor-pointer'
ref={isAnchor ? (element) => setLoader(element!) : undefined}
>
<Card fit movie={movie} onClick={() => setMovie(movie)} />
<Card fit movie={movie} onClick={() => showPreviewModal({ movie, onClose: onPreviewClose })} />
</motion.li>
);
})}
@@ -82,21 +80,6 @@ const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage, withBottomGap = tr
))}
</motion.ul>
{withBottomGap && <div className='h-4 w-full' />}
<Modal isOpen={Boolean(movie)} onClose={() => setMovie(null)} className='block p-0'>
{movie && (
<Preview
fit
key={movie.id}
movie={movie}
className='border-none'
onClose={() => {
setMovie(null);
window.scrollTo({ top: 0, behavior: 'smooth' });
onPreviewClose?.();
}}
/>
)}
</Modal>
</>
);
};
+20 -3
View File
@@ -1,8 +1,10 @@
'use client';
import { FC, ReactNode } from 'react';
import { create, InstanceProps } from 'react-modal-promise';
import Credits from '@/components/Movie/components/Credits';
import { Modal } from '@/components/ui/dialog';
import { Separator } from '@/components/ui/separator';
import { cn } from '@/lib/utils';
@@ -16,16 +18,16 @@ interface Props {
className?: string;
onClose?: () => void;
children?: ReactNode;
renderCard?: () => ReactNode;
card?: ReactNode;
}
const Preview: FC<Props> = ({ movie, className, onClose, children, renderCard, fit }) => {
const Preview: FC<Props> = ({ movie, className, onClose, children, card, fit }) => {
if (!movie) return null;
return (
<div className={cn('rounded-xl text-card-foreground', { 'p-2': Boolean(onClose) }, className)}>
<div className='flex flex-col lg:flex-row max-w-[100%] gap-2'>
{renderCard?.() || <Card fit={fit} movie={movie} />}
{card || <Card fit={fit} movie={movie} />}
<div className='flex flex-col grow'>
{children && <div className='flex hover-none:hidden w-full mb-2 gap-2'>{children}</div>}
<div className='flex w-full gap-2'>
@@ -42,4 +44,19 @@ const Preview: FC<Props> = ({ movie, className, onClose, children, renderCard, f
);
};
export const showPreviewModal = create(({ onResolve, onClose, movie }: Props & InstanceProps<void>) => (
<Modal className='block p-0' onClose={onResolve}>
<Preview
fit
movie={movie}
className='border-none'
onClose={() => {
window.scrollTo({ top: 0, behavior: 'smooth' });
onClose?.();
onResolve();
}}
/>
</Modal>
));
export default Preview;
@@ -51,10 +51,10 @@ const TableHeadSortable = ({
const hostAtom = atomWithStorage<string>('magnet-host', '');
const showHostManagerModal = create(({ onResolve, isOpen }) => {
const showHostManagerModal = create(({ onResolve }) => {
const [host, setHost] = useAtom(hostAtom);
return (
<Modal className='p-3 max-w-[200px] m-auto' isOpen={isOpen} onClose={() => onResolve()}>
<Modal className='p-3 max-w-[200px] m-auto' onClose={onResolve}>
<Input
autoFocus
placeholder='Enter host of Elementum'
@@ -66,7 +66,7 @@ const showHostManagerModal = create(({ onResolve, isOpen }) => {
);
});
const Torrents: FC<Props> = ({ title, year, isOpen, onResolve }) => {
const Torrents: FC<Props> = ({ title, year, onResolve }) => {
const hosts = useAtomValue(hostAtom);
const [sorting, setSorting] = useState<Sorting>({ by: ORDER.seeders });
const [withYear, setWithYear] = useState(true);
@@ -87,7 +87,7 @@ const Torrents: FC<Props> = ({ title, year, isOpen, onResolve }) => {
};
return (
<Modal className='p-0' isOpen={isOpen} onClose={() => onResolve()}>
<Modal className='p-0' onClose={onResolve}>
<Table className='rounded-xl overflow-hidden'>
<TableHeader>
<TableRow>
@@ -13,15 +13,11 @@ interface Props {
children?: ({ onPlay, disabled }: { onPlay: () => void; disabled: boolean }) => ReactNode;
}
const Trailer: FC<{ trailerKey?: string } & InstanceProps<void>> = ({ trailerKey, onResolve, isOpen }) => {
const Trailer: FC<{ trailerKey?: string } & InstanceProps<void>> = ({ trailerKey, onResolve }) => {
if (!trailerKey) return;
return (
<Modal
className='block p-0 min-h-[400px] border-none aspect-w-16 aspect-h-9'
onClose={() => onResolve()}
isOpen={isOpen}
>
<Modal className='block p-0 min-h-[400px] border-none aspect-w-16 aspect-h-9' onClose={onResolve}>
<iframe
allow='autoplay'
className='border-none rounded-lg'
+26 -46
View File
@@ -1,9 +1,9 @@
'use client';
import * as React from 'react';
import { useState } from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { AnimatePresence, motion, useDragControls } from 'framer-motion';
import { GripHorizontal } from 'lucide-react';
import { X } from 'lucide-react';
import { cn } from '@/lib/utils';
@@ -12,23 +12,19 @@ const Dialog = DialogPrimitive.Root;
const DialogPortal = ({ ...props }: DialogPrimitive.DialogPortalProps) => <DialogPrimitive.Portal {...props} />;
DialogPortal.displayName = DialogPrimitive.Portal.displayName;
const transition = { duration: 0.35, type: 'spring', bounce: 0 };
const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
>(({ className, children, ...props }, ref) => (
<DialogPrimitive.Overlay ref={ref} {...props}>
<motion.div
onScroll={props.onScroll}
className={cn('fixed inset-0 z-50 transform-gpu backdrop-brightness-50 select-none', className)}
transition={transition}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
<div
className={cn(
'fixed inset-0 z-50 bg-black/60 duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
className
)}
>
{props?.children}
</motion.div>
{children}
</div>
</DialogPrimitive.Overlay>
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
@@ -37,38 +33,19 @@ const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & { onClose?: () => void }
>(({ className, children, ...props }, ref) => {
const controls = useDragControls();
return (
<DialogPortal>
<DialogOverlay className='overflow-auto flex justify-center select-none'>
<motion.div
className='absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] select-none'
transition={transition}
initial={{ top: '100vh' }}
animate={{ top: '20vh' }}
exit={{ top: '100vh' }}
drag='y'
dragConstraints={{ top: 0, bottom: 0 }}
onDragEnd={(_, info) => info.offset.y > 100 && props.onClose?.()}
dragListener={false}
dragControls={controls}
dragElastic={0.8}
>
<div className='absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] gap-4 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-50 data-[state=open]:zoom-in-50 select-none'>
<DialogPrimitive.Content
ref={ref}
className={cn('gap-4 border bg-background p-6 shadow-lg rounded-xl relative', className)}
{...props}
>
<motion.div
className='w-full h-10 flex p-2 justify-center cursor-grab absolute top-[-36px] z-10'
onPointerDown={(e) => controls.start(e)}
>
<GripHorizontal color='white' />
</motion.div>
{children}
<div className='absolute w-full block h-[20vh] pointer-events-none' />
</DialogPrimitive.Content>
</motion.div>
</div>
</DialogOverlay>
</DialogPortal>
);
@@ -78,19 +55,22 @@ DialogContent.displayName = DialogPrimitive.Content.displayName;
const Modal: React.FC<{
children: React.ReactNode;
className?: string;
isOpen: boolean;
onClose: () => void;
}> = ({ children, onClose, isOpen, className }) => {
}> = ({ children, onClose, className }) => {
const [open, setOpen] = useState(true);
const handleClose = () => {
console.log(onClose());
console.log('closing');
setOpen(false);
};
return (
<AnimatePresence>
{isOpen && (
<Dialog defaultOpen={true} onOpenChange={onClose}>
<DialogContent forceMount className={className} onClose={onClose}>
{children}
</DialogContent>
</Dialog>
)}
</AnimatePresence>
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent className={className} onClose={onClose}>
{children}
</DialogContent>
</Dialog>
);
};
+1 -3
View File
@@ -3,7 +3,6 @@
import { ReactNode, useState } from 'react';
import ModalContainer from 'react-modal-promise';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { queryClientOptions } from '@/lib/queryClient';
import ThemeProvider from '@/providers/Theme';
@@ -14,8 +13,7 @@ const Providers = ({ children, theme }: { children: ReactNode; theme?: Theme })
return (
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
<ModalContainer />
{/* <ReactQueryDevtools initialIsOpen={false} /> */}
<ModalContainer exitTimeout={500} enterTimeout={0} />
</QueryClientProvider>
);
};
-1
View File
@@ -94,7 +94,6 @@
}
* {
user-select: none;
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
-webkit-user-select: none;
+1 -1
View File
@@ -12,7 +12,7 @@ module.exports = {
},
extend: {
screens: {
xs: '400px'
xs: '500px'
},
colors: {
border: 'hsl(var(--border))',