feat: native carousel

This commit is contained in:
ner committed 2024-08-25 21:48:03 +03:00
1 parent 4ddaa89032
commit 5733bc0af0
20 files changed
+961 -534

No files matched your search

+28 -29
View File
@@ -1,19 +1,19 @@
import { FC } from 'react';
import { motion } from 'framer-motion';
import useFavorites, { useFavoritesMutation } from '@/app/(site)/favorites/useFavorites';
import Poster from '@/components/Movie/components/Poster';
import Rating from '@/components/Movie/components/Rating';
import Title from '@/components/Movie/components/Title';
import { AspectRatio } from '@/components/ui/aspect-ratio';
import { cn } from '@/lib/utils';
interface Props {
movie?: Movie;
onClick?: () => void;
fit?: boolean;
className?: string;
}
const Card: FC<Props> = ({ movie, onClick, fit }) => {
const Card: FC<Props> = ({ movie, onClick, className }) => {
const { add, remove } = useFavoritesMutation();
const { map } = useFavorites();
@@ -24,38 +24,37 @@ const Card: FC<Props> = ({ movie, onClick, fit }) => {
const handleToggleFavorite = () => (isFavorite ? remove(movie.id) : add(movie));
return (
<div
onDoubleClick={handleToggleFavorite}
className={cn(
'w-[300px] h-[450px] shrink-0 mx-auto relative',
fit ? 'xs:w-[300px] xs:h-[450px] w-full h-full aspect-w-2 aspect-h-3' : undefined
)}
>
<motion.div className='absolute top-0 left-0 flex flex-col w-full h-full mx-auto bg-card text-card-foreground shadow-sm transition-shadow hover:shadow-lg overflow-hidden rounded-lg'>
<motion.div onTap={onClick} className='flex w-full h-full'>
<Poster title={movie?.name || movie.title} size={100} poster={movie.poster_path} />
</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'>
<div className='h-full w-full mx-auto pt-1 px-3 text-white'>
<div className='grid items-center grid-cols-[1fr_50px]'>
<div className='xs:w-[300px] xs:h-[450px] w-full h-full shrink-0'>
<AspectRatio ratio={2 / 3}>
<div
onClick={onClick}
onDoubleClick={handleToggleFavorite}
className={cn(
'h-full relative bg-card text-card-foreground shadow-sm transition-shadow hover:shadow-lg overflow-hidden rounded-lg',
className
)}
>
<div className='flex w-full h-full'>
<Poster title={movie?.name || movie.title} poster={movie.poster_path} />
</div>
<div className='text-white absolute w-full h-full top-0 left-0 rounded-md flex flex-col justify-between p-2 card-gradient'>
<div className='grid items-center grid-cols-[1fr_50px] mt-[-5px] opacity-75'>
<Title key={movie?.name || movie.title} title={movie?.name || movie.title} />
{movie.release_date && (
<span className='flex justify-end text-lg font-semibold'>
{new Date(movie.release_date).getFullYear()}
</span>
<span className='flex justify-end text-lg'>{new Date(movie.release_date).getFullYear()}</span>
)}
</div>
<div className='opacity-75 mb-[-5px]'>
<Rating
isFavorite={isFavorite}
toggleFavorite={handleToggleFavorite}
rating={movie.vote_average}
iconsFillColor='fill-white'
/>
</div>
</div>
</div>
<div className='absolute left-0 bottom-0 w-full bg-gradient-to-t from-black to-transparent h-20 text-white p-3 pb-2 flex flex-col justify-end opacity-75'>
<Rating
isFavorite={isFavorite}
toggleFavorite={handleToggleFavorite}
rating={movie.vote_average}
iconsFillColor='fill-white'
/>
</div>
</motion.div>
</AspectRatio>
</div>
);
};
+32 -44
View File
@@ -1,5 +1,5 @@
import { FC, Fragment, useLayoutEffect, useMemo, useState } from 'react';
import { motion } from 'framer-motion';
import { FC, forwardRef, useMemo } from 'react';
import { VirtuosoGrid, VirtuosoGridProps } from 'react-virtuoso';
import Card from '@/components/Movie/Card';
import { showPreviewModal } from '@/components/Movie/Preview';
@@ -31,6 +31,18 @@ const item = {
}
};
const components: VirtuosoGridProps<Movie, { shows: Movie[]; onClick: (movie: Movie) => void }>['components'] = {
List: forwardRef(function List(props, ref) {
return <div {...props} className='flex gap-3 flex-wrap justify-center grow empty:hidden' ref={ref} />;
}),
Item: ({ context, 'data-index': index }) => {
const { shows, onClick } = context || {};
const movie = shows?.[index];
return <Card movie={movie} onClick={() => onClick?.(movie!)} />;
}
};
const List: FC<Props> = ({
pages,
hasNextPage,
@@ -39,27 +51,6 @@ const List: FC<Props> = ({
onPreviewClose,
type = 'movie'
}) => {
const [loader, setLoader] = useState<HTMLLIElement>();
useLayoutEffect(() => {
if (!loader || !hasNextPage) return;
const observer = new IntersectionObserver(
([{ isIntersecting }]) => {
if (!isIntersecting) return;
fetchNextPage?.();
},
{
threshold: 1
}
);
observer.observe(loader);
return () => observer.disconnect();
}, [fetchNextPage, loader, hasNextPage]);
const shows = useMemo(
() =>
pages?.reduce((shows, page) => {
@@ -71,29 +62,26 @@ const List: FC<Props> = ({
if (!shows.length) return null;
const context = {
shows,
onClick: (movie: Movie) => showPreviewModal({ movie, onClose: onPreviewClose, type })
};
const handleFetchMore = () => {
if (!hasNextPage) return;
fetchNextPage?.();
};
return (
<>
<motion.ul
className='flex gap-3 flex-wrap justify-center grow empty:hidden'
variants={container}
initial={pages.length === 1 ? 'hidden' : false}
animate='visible'
>
{shows?.map((movie, index, array) => {
const isAnchor = Math.round(array.length / 2) === index;
return (
<motion.li
key={movie.id}
variants={item}
className='flex xs:w-auto w-full cursor-pointer'
ref={isAnchor ? (element) => setLoader(element!) : undefined}
>
<Card fit movie={movie} onClick={() => showPreviewModal({ movie, onClose: onPreviewClose, type })} />
</motion.li>
);
})}
</motion.ul>
<VirtuosoGrid
useWindowScroll
totalCount={shows.length}
endReached={handleFetchMore}
data={shows}
context={context}
components={components}
/>
{withBottomGap && <div className='h-2 w-full' />}
</>
);
+7 -11
View File
@@ -13,7 +13,6 @@ import Card from '../Card';
import Actions from './components/Actions';
interface Props {
fit?: boolean;
movie?: Movie;
className?: string;
onClose?: () => void;
@@ -22,29 +21,27 @@ interface Props {
type?: ShowType;
}
const Preview: FC<Props> = ({ movie, className, onClose, children, card, fit, type = 'movie' }) => {
const Preview: FC<Props> = ({ movie, className, onClose, children, card, type = 'movie' }) => {
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'>
{card || (
<div className='xs:w-[300px] w-full shrink-0 mx-auto'>
<Card fit={fit} movie={movie} />
</div>
)}
<div className='flex flex-col md:flex-row max-w-[100%] gap-2'>
{card || <Card className='mx-auto' movie={movie} />}
<div className='flex flex-col grow'>
{children && <div className='flex hover-none:hidden w-full max-sm:mb-0 mb-2 gap-2'>{children}</div>}
<div className='flex w-full gap-2'>
<Actions type={type} movie={movie} onClose={onClose} />
</div>
<Separator className='my-2' />
<p key={movie.id} className='leading-7'>
<p key={movie.id} className='leading-7 mb-3'>
{movie.overview}
</p>
<div className='mt-auto grid grid-cols-1 rounded-lg'>
<Credits type={type} movieId={movie.id} onPersonClick={onClose} />
</div>
</div>
</div>
<Credits type={type} movieId={movie.id} onPersonClick={onClose} />
</div>
);
};
@@ -52,7 +49,6 @@ const Preview: FC<Props> = ({ movie, className, onClose, children, card, fit, ty
export const showPreviewModal = create(({ onResolve, onClose, movie, type = 'movie' }: Props & InstanceProps<void>) => (
<Modal className='block p-0' onClose={onResolve}>
<Preview
fit
type={type}
movie={movie}
className='border-none'
+41 -35
View File
@@ -37,42 +37,48 @@ const Credits: FC<Props> = ({ movieId, onPersonClick, type = 'movie' }) => {
return (
Boolean(credits?.length) && (
<div className='overflow-auto no-scrollbar max-w-[100%] py-2 pb-3 mb-[-0.75rem]'>
<motion.ul className='flex gap-3' variants={container} initial='hidden' animate='visible' whileInView='visible'>
{credits?.map((actor) => (
<motion.li
variants={item}
key={`${actor.id}_${actor.cast_id}`}
title={actor.name}
onTap={() => {
router.push(`/starring?actorId=${actor.id}&title=${encodeURIComponent(actor.name)}`);
onPersonClick?.(actor.id.toString());
}}
className='p-2 rounded-lg border bg-card text-card-foreground self-end max-w-[150px] w-full shrink-0 cursor-pointer transition-shadow hover:shadow-lg'
>
{Boolean(actor.character) && (
<p className='pb-2 overflow-ellipsis whitespace-nowrap overflow-hidden'>{actor.character}</p>
<motion.ul
className='flex gap-2 no-scrollbar overflow-auto rounded-lg snap-mandatory snap-x'
variants={container}
initial='hidden'
animate='visible'
whileInView='visible'
>
{credits?.map((actor) => (
<motion.li
variants={item}
key={`${actor.id}_${actor.cast_id}`}
title={actor.name}
onTap={() => {
router.push(`/starring?actorId=${actor.id}&title=${encodeURIComponent(actor.name)}`);
onPersonClick?.(actor.id.toString());
}}
className='bg-card text-card-foreground self-end max-w-[120px] w-full shrink-0 cursor-pointer transition-shadow hover:shadow-lg relative snap-start'
>
<div className='rounded-lg overflow-hidden'>
{actor.profile_path ? (
<Image
unoptimized
height={180}
width={120}
src={`https://image.tmdb.org/t/p/w500/${actor.profile_path}`}
alt={actor.name}
/>
) : (
<div className='h-[180px] w-[120px] flex items-center justify-center rounded-lg'>
<User2 size={120} strokeWidth={1} />
</div>
)}
<div className='rounded-md overflow-hidden'>
{actor.profile_path ? (
<Image
unoptimized
height={225}
width={150}
src={`https://image.tmdb.org/t/p/w500/${actor.profile_path}`}
alt={actor.name}
/>
) : (
<div className='w-[132px] h-[198px] flex items-center justify-center rounded-lg border'>
<User2 size={120} strokeWidth={1} />
</div>
)}
</div>
<p className='pt-2 overflow-ellipsis whitespace-nowrap overflow-hidden'>{actor.name}</p>
</motion.li>
))}
</motion.ul>
</div>
</div>
<div className='text-white text-sm absolute w-full h-full top-0 left-0 rounded-md flex flex-col justify-between p-2 py-1 card-gradient'>
<p className=' overflow-ellipsis whitespace-nowrap overflow-hidden'>{actor.name}</p>
{Boolean(actor.character) && (
<p className='overflow-ellipsis whitespace-nowrap overflow-hidden'>{actor.character}</p>
)}
</div>
</motion.li>
))}
</motion.ul>
)
);
};
+3 -4
View File
@@ -4,19 +4,18 @@ import { ImageOff } from 'lucide-react';
interface Props {
title: string;
poster: string;
size: number;
}
const Poster: FC<Props> = ({ title, poster, size = 250 }) => {
const Poster: FC<Props> = ({ title, poster }) => {
return poster ? (
<img
className='rounded-lg w-full h-full object-fill'
className='rounded-lg w-full h-full object-cover'
src={`https://image.tmdb.org/t/p/w500/${poster}`}
alt={title}
/>
) : (
<div className={'w-full flex items-center justify-center rounded-lg grow'}>
<ImageOff size={size} strokeWidth={1} color='hsl(215.4 16.3% 46.9%)' />
<ImageOff size={100} strokeWidth={1} color='hsl(215.4 16.3% 46.9%)' />
</div>
);
};
+1 -1
View File
@@ -26,7 +26,7 @@ const Rating: FC<Props> = ({ isFavorite, rating, toggleFavorite, iconsFillColor
</motion.span>
<span className='flex items-center'>
<Star size={18} className={`mr-1 ${iconsFillColor}`} />
<span className='font-semibold'>{rating?.toFixed(1)}</span>
<p className='mt-[3px]'>{rating?.toFixed(1)}</p>
</span>
</span>
);