feat: scroll navigation
This commit is contained in:
1 parent
eaaf46082c
commit
058ecd452f
7 files changed
+208
-102
No files matched your search
@@ -1,8 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import { FC, forwardRef, useEffect, useLayoutEffect, useRef } from 'react';
|
||||
import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso';
|
||||
import { FC, ForwardedRef, forwardRef, useLayoutEffect, useRef } from 'react';
|
||||
import { Virtuoso, VirtuosoProps } from 'react-virtuoso';
|
||||
|
||||
import ScrollNavigation from '@/components/ScrollNavigation';
|
||||
import Card from '@/components/Show';
|
||||
|
||||
const components: VirtuosoProps<Show, (index: number) => void>['components'] = {
|
||||
@@ -42,8 +43,31 @@ const components: VirtuosoProps<Show, (index: number) => void>['components'] = {
|
||||
return <div {...props} ref={ref} style={{ ...props.style, display: 'flex' }} className='gap-2' />;
|
||||
}),
|
||||
Scroller: forwardRef(function Scroller({ context, ...props }, ref) {
|
||||
const handleRef =
|
||||
(ref: ForwardedRef<HTMLDivElement>, setRef: (element: HTMLDivElement) => void) =>
|
||||
(element: HTMLDivElement | null) => {
|
||||
if (!element || !ref) return;
|
||||
|
||||
setRef(element);
|
||||
|
||||
typeof ref === 'function' ? ref(element) : (ref.current = element);
|
||||
};
|
||||
|
||||
return (
|
||||
<div {...props} ref={ref} className='no-scrollbar h-full snap-x snap-mandatory overflow-hidden rounded-lg' />
|
||||
<ScrollNavigation<HTMLDivElement>
|
||||
className='size-full'
|
||||
arrowsClassName='h-20 bg-secondary'
|
||||
backClassName='rounded-none rounded-r-full'
|
||||
nextClassName='rounded-none rounded-l-full'
|
||||
>
|
||||
{(setRef) => (
|
||||
<div
|
||||
{...props}
|
||||
ref={handleRef(ref, setRef)}
|
||||
className='no-scrollbar h-full snap-x snap-mandatory overflow-hidden rounded-lg'
|
||||
/>
|
||||
)}
|
||||
</ScrollNavigation>
|
||||
);
|
||||
})
|
||||
};
|
||||
@@ -56,33 +80,11 @@ interface Props {
|
||||
}
|
||||
|
||||
const Carousel: FC<Props> = ({ index, shows, onIndexChange, onEndReached }) => {
|
||||
const ref = useRef<VirtuosoHandle>(null);
|
||||
const initialIndex = useRef(index);
|
||||
|
||||
useEffect(() => {
|
||||
const scrollTo = ({ key }: globalThis.KeyboardEvent) => {
|
||||
if (key === 'ArrowLeft') {
|
||||
if (index <= 0) return;
|
||||
ref.current?.scrollIntoView({ index: index - 1, behavior: 'smooth' });
|
||||
}
|
||||
|
||||
if (key === 'ArrowRight') {
|
||||
if (index >= (shows?.length || 0) - 1) return;
|
||||
ref.current?.scrollIntoView({ index: index + 1, behavior: 'smooth' });
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('keydown', scrollTo);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('keydown', scrollTo);
|
||||
};
|
||||
}, [index, shows?.length]);
|
||||
|
||||
return (
|
||||
<div className='card-aspect-ratio mx-auto'>
|
||||
<Virtuoso
|
||||
ref={ref}
|
||||
data={shows}
|
||||
context={onIndexChange}
|
||||
endReached={onEndReached}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { ReactNode, useEffect, useState } from 'react';
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
||||
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface Props<E extends HTMLElement> {
|
||||
className?: string;
|
||||
arrowsClassName?: string;
|
||||
backClassName?: string;
|
||||
nextClassName?: string;
|
||||
iconClassName?: string;
|
||||
children: (setScrollElement: (element: E) => void) => ReactNode;
|
||||
}
|
||||
|
||||
function ScrollNavigation<E extends HTMLElement>({
|
||||
className,
|
||||
arrowsClassName,
|
||||
backClassName,
|
||||
nextClassName,
|
||||
iconClassName,
|
||||
children
|
||||
}: Props<E>) {
|
||||
const [scrollElement, setScrollElement] = useState<E | null>(null);
|
||||
const [isShowBack, setIsShowBack] = useState(true);
|
||||
const [isShowNext, setIsShowNext] = useState(true);
|
||||
const [childWidth, setChildWidth] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (!scrollElement) return;
|
||||
|
||||
let timeout: NodeJS.Timeout;
|
||||
|
||||
if (scrollElement.firstChild) {
|
||||
setChildWidth((scrollElement.firstChild as HTMLElement).clientWidth);
|
||||
}
|
||||
|
||||
const handleScroll = () => {
|
||||
clearTimeout(timeout);
|
||||
|
||||
const DELAY = 100;
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
setIsShowBack(Boolean(scrollElement.scrollLeft));
|
||||
setIsShowNext(Boolean(scrollElement.scrollWidth - (scrollElement.clientWidth + scrollElement.scrollLeft)));
|
||||
}, DELAY);
|
||||
};
|
||||
|
||||
handleScroll();
|
||||
|
||||
scrollElement.addEventListener('scroll', handleScroll);
|
||||
|
||||
return () => {
|
||||
scrollElement.removeEventListener('scroll', handleScroll);
|
||||
};
|
||||
}, [scrollElement]);
|
||||
|
||||
const handleScrollNext = (direction: 1 | -1) => () => {
|
||||
if (!scrollElement) return;
|
||||
|
||||
scrollElement.scrollTo({ left: scrollElement.scrollLeft + childWidth * direction, behavior: 'smooth' });
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn('relative group', className)}>
|
||||
{children(setScrollElement)}
|
||||
{[
|
||||
{
|
||||
Arrow: ChevronLeft,
|
||||
isShow: isShowBack,
|
||||
handleScroll: handleScrollNext(-1),
|
||||
className: cn(
|
||||
'left-0 bg-gradient-to-r from-background to-transparent justify-start rounded-l-md',
|
||||
backClassName
|
||||
)
|
||||
},
|
||||
{
|
||||
Arrow: ChevronRight,
|
||||
isShow: isShowNext,
|
||||
handleScroll: handleScrollNext(1),
|
||||
className: cn(
|
||||
'right-0 bg-gradient-to-r from-transparent to-background justify-end rounded-r-md',
|
||||
nextClassName
|
||||
)
|
||||
}
|
||||
].map(({ Arrow, isShow, handleScroll, className }) => (
|
||||
<div
|
||||
key={className}
|
||||
onClick={handleScroll}
|
||||
className={cn(
|
||||
'opacity-0 transition-opacity absolute top-1/2 transform -translate-y-1/2 bg-gradient-to-r h-full w-14 flex items-center cursor-pointer',
|
||||
{
|
||||
'group-hover:opacity-100 hover:!opacity-60': isShow,
|
||||
'pointer-events-none': !isShow
|
||||
},
|
||||
className,
|
||||
arrowsClassName
|
||||
)}
|
||||
>
|
||||
<Arrow className={cn('text-primary', iconClassName)} size={30} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default ScrollNavigation;
|
||||
@@ -5,6 +5,8 @@ import { User2 } from 'lucide-react';
|
||||
import Image from 'next/image';
|
||||
import Link from 'next/link';
|
||||
|
||||
import ScrollNavigation from '@/components/ScrollNavigation';
|
||||
|
||||
import useCredits from './useCredits';
|
||||
|
||||
interface Props {
|
||||
@@ -18,7 +20,12 @@ const Credits: FC<Props> = ({ showId, showType = 'movie', onPersonClick }) => {
|
||||
|
||||
return (
|
||||
Boolean(credits?.length) && (
|
||||
<ul className='no-scrollbar flex animate-fade-aside-slide-in snap-x snap-mandatory gap-2 overflow-auto rounded-lg opacity-0'>
|
||||
<ScrollNavigation<HTMLUListElement>>
|
||||
{(setRef) => (
|
||||
<ul
|
||||
ref={setRef}
|
||||
className='no-scrollbar flex animate-fade-aside-slide-in snap-x snap-mandatory gap-2 overflow-auto rounded-lg opacity-0'
|
||||
>
|
||||
{credits?.map((actor) => (
|
||||
<li
|
||||
key={`${actor.id}_${actor.cast_id}`}
|
||||
@@ -46,14 +53,14 @@ const Credits: FC<Props> = ({ showId, showType = 'movie', onPersonClick }) => {
|
||||
</div>
|
||||
<div className='absolute left-0 top-0 flex size-full flex-col justify-between rounded-md bg-vignette p-2 py-1 text-sm text-white'>
|
||||
<p className=' truncate'>{actor.name}</p>
|
||||
{Boolean(actor.character) && (
|
||||
<p className='truncate'>{actor.character}</p>
|
||||
)}
|
||||
{Boolean(actor.character) && <p className='truncate'>{actor.character}</p>}
|
||||
</div>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</ScrollNavigation>
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { FC } from 'react';
|
||||
|
||||
import { showPreviewModal } from '@/components/Preview';
|
||||
import ScrollNavigation from '@/components/ScrollNavigation';
|
||||
import Card from '@/components/Show';
|
||||
|
||||
interface Props {
|
||||
shows: Show[];
|
||||
}
|
||||
|
||||
const Gallery: FC<Props> = ({ shows }) => (
|
||||
<ScrollNavigation<HTMLUListElement>>
|
||||
{(setRef) => (
|
||||
<ul
|
||||
ref={setRef}
|
||||
className='no-scrollbar flex animate-fade-aside-slide-in snap-x snap-mandatory gap-2 overflow-auto rounded-lg opacity-0'
|
||||
>
|
||||
{shows.map((show) => (
|
||||
<li key={show.id} className='snap-start'>
|
||||
<Card className='!h-[350px] !w-[230px] text-sm' show={show} onClick={() => showPreviewModal({ show })} />
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</ScrollNavigation>
|
||||
);
|
||||
|
||||
export default Gallery;
|
||||
@@ -1,20 +0,0 @@
|
||||
import { FC } from 'react';
|
||||
|
||||
import { showPreviewModal } from '@/components/Preview';
|
||||
import Card from '@/components/Show';
|
||||
|
||||
interface Props {
|
||||
shows: Show[];
|
||||
}
|
||||
|
||||
const Marquee: FC<Props> = ({ shows }) => (
|
||||
<ul className='no-scrollbar flex animate-fade-aside-slide-in snap-x snap-mandatory gap-2 overflow-auto rounded-lg opacity-0'>
|
||||
{shows.map((show) => (
|
||||
<li key={show.id} className='snap-start'>
|
||||
<Card className='!h-[350px] !w-[230px] text-sm' show={show} onClick={() => showPreviewModal({ show })} />
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
|
||||
export default Marquee;
|
||||
@@ -4,7 +4,7 @@ import { FC, useState } from 'react';
|
||||
|
||||
import Nav from '@/components/ui/nav';
|
||||
|
||||
import Marquee from './components/Marquee';
|
||||
import Gallery from './components/Gallery';
|
||||
import { tabs } from './constants';
|
||||
import useTrending from './useTrending';
|
||||
|
||||
@@ -22,7 +22,7 @@ const Trending: FC = () => {
|
||||
isFetched && (
|
||||
<div>
|
||||
<Nav tabs={tabs} active={active} onChange={(active) => setActive(active)} className='mb-3' />
|
||||
<Marquee key={active.type} shows={shows} />
|
||||
<Gallery key={active.type} shows={shows} />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
|
||||
+34
-51
@@ -1,13 +1,13 @@
|
||||
'use client'
|
||||
'use client';
|
||||
|
||||
import * as React from 'react'
|
||||
import * as ToastPrimitives from '@radix-ui/react-toast'
|
||||
import { cva, type VariantProps } from 'class-variance-authority'
|
||||
import { X } from 'lucide-react'
|
||||
import * as React from 'react';
|
||||
import * as ToastPrimitives from '@radix-ui/react-toast';
|
||||
import { cva, type VariantProps } from 'class-variance-authority';
|
||||
import { X } from 'lucide-react';
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const ToastProvider = ToastPrimitives.Provider
|
||||
const ToastProvider = ToastPrimitives.Provider;
|
||||
|
||||
const ToastViewport = React.forwardRef<
|
||||
React.ElementRef<typeof ToastPrimitives.Viewport>,
|
||||
@@ -21,39 +21,30 @@ const ToastViewport = React.forwardRef<
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
ToastViewport.displayName = ToastPrimitives.Viewport.displayName
|
||||
));
|
||||
ToastViewport.displayName = ToastPrimitives.Viewport.displayName;
|
||||
|
||||
const toastVariants = cva(
|
||||
'group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full',
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: 'border bg-background text-foreground',
|
||||
destructive:
|
||||
'destructive group border-destructive bg-destructive text-destructive-foreground',
|
||||
},
|
||||
default: 'border bg-background text-foreground'
|
||||
}
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: 'default',
|
||||
},
|
||||
variant: 'default'
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
const Toast = React.forwardRef<
|
||||
React.ElementRef<typeof ToastPrimitives.Root>,
|
||||
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
|
||||
VariantProps<typeof toastVariants>
|
||||
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants>
|
||||
>(({ className, variant, ...props }, ref) => {
|
||||
return (
|
||||
<ToastPrimitives.Root
|
||||
ref={ref}
|
||||
className={cn(toastVariants({ variant }), className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
})
|
||||
Toast.displayName = ToastPrimitives.Root.displayName
|
||||
return <ToastPrimitives.Root ref={ref} className={cn(toastVariants({ variant }), className)} {...props} />;
|
||||
});
|
||||
Toast.displayName = ToastPrimitives.Root.displayName;
|
||||
|
||||
const ToastAction = React.forwardRef<
|
||||
React.ElementRef<typeof ToastPrimitives.Action>,
|
||||
@@ -67,8 +58,8 @@ const ToastAction = React.forwardRef<
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
ToastAction.displayName = ToastPrimitives.Action.displayName
|
||||
));
|
||||
ToastAction.displayName = ToastPrimitives.Action.displayName;
|
||||
|
||||
const ToastClose = React.forwardRef<
|
||||
React.ElementRef<typeof ToastPrimitives.Close>,
|
||||
@@ -80,41 +71,33 @@ const ToastClose = React.forwardRef<
|
||||
'absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600',
|
||||
className
|
||||
)}
|
||||
toast-close=""
|
||||
toast-close=''
|
||||
{...props}
|
||||
>
|
||||
<X className="size-4" />
|
||||
<X className='size-4' />
|
||||
</ToastPrimitives.Close>
|
||||
))
|
||||
ToastClose.displayName = ToastPrimitives.Close.displayName
|
||||
));
|
||||
ToastClose.displayName = ToastPrimitives.Close.displayName;
|
||||
|
||||
const ToastTitle = React.forwardRef<
|
||||
React.ElementRef<typeof ToastPrimitives.Title>,
|
||||
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<ToastPrimitives.Title
|
||||
ref={ref}
|
||||
className={cn('text-sm font-semibold', className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
ToastTitle.displayName = ToastPrimitives.Title.displayName
|
||||
<ToastPrimitives.Title ref={ref} className={cn('text-sm font-semibold', className)} {...props} />
|
||||
));
|
||||
ToastTitle.displayName = ToastPrimitives.Title.displayName;
|
||||
|
||||
const ToastDescription = React.forwardRef<
|
||||
React.ElementRef<typeof ToastPrimitives.Description>,
|
||||
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<ToastPrimitives.Description
|
||||
ref={ref}
|
||||
className={cn('text-sm opacity-90', className)}
|
||||
{...props}
|
||||
/>
|
||||
))
|
||||
ToastDescription.displayName = ToastPrimitives.Description.displayName
|
||||
<ToastPrimitives.Description ref={ref} className={cn('text-sm opacity-90', className)} {...props} />
|
||||
));
|
||||
ToastDescription.displayName = ToastPrimitives.Description.displayName;
|
||||
|
||||
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>
|
||||
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>;
|
||||
|
||||
type ToastActionElement = React.ReactElement<typeof ToastAction>
|
||||
type ToastActionElement = React.ReactElement<typeof ToastAction>;
|
||||
|
||||
export {
|
||||
Toast,
|
||||
@@ -125,5 +108,5 @@ export {
|
||||
type ToastProps,
|
||||
ToastProvider,
|
||||
ToastTitle,
|
||||
ToastViewport,
|
||||
}
|
||||
ToastViewport
|
||||
};
|
||||
Reference in new issue
Block a user