diff --git a/components/Random/components/Carousel/index.tsx b/components/Random/components/Carousel/index.tsx index d456738..bea6887 100644 --- a/components/Random/components/Carousel/index.tsx +++ b/components/Random/components/Carousel/index.tsx @@ -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 void>['components'] = { @@ -42,8 +43,31 @@ const components: VirtuosoProps void>['components'] = { return
; }), Scroller: forwardRef(function Scroller({ context, ...props }, ref) { + const handleRef = + (ref: ForwardedRef, setRef: (element: HTMLDivElement) => void) => + (element: HTMLDivElement | null) => { + if (!element || !ref) return; + + setRef(element); + + typeof ref === 'function' ? ref(element) : (ref.current = element); + }; + return ( -
+ + className='size-full' + arrowsClassName='h-20 bg-secondary' + backClassName='rounded-none rounded-r-full' + nextClassName='rounded-none rounded-l-full' + > + {(setRef) => ( +
+ )} + ); }) }; @@ -56,33 +80,11 @@ interface Props { } const Carousel: FC = ({ index, shows, onIndexChange, onEndReached }) => { - const ref = useRef(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 (
{ + className?: string; + arrowsClassName?: string; + backClassName?: string; + nextClassName?: string; + iconClassName?: string; + children: (setScrollElement: (element: E) => void) => ReactNode; +} + +function ScrollNavigation({ + className, + arrowsClassName, + backClassName, + nextClassName, + iconClassName, + children +}: Props) { + const [scrollElement, setScrollElement] = useState(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 ( +
+ {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 }) => ( +
+ +
+ ))} +
+ ); +} + +export default ScrollNavigation; diff --git a/components/Show/components/Credits/index.tsx b/components/Show/components/Credits/index.tsx index ae5f1e7..cd53fdb 100644 --- a/components/Show/components/Credits/index.tsx +++ b/components/Show/components/Credits/index.tsx @@ -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,42 +20,47 @@ const Credits: FC = ({ showId, showType = 'movie', onPersonClick }) => { return ( Boolean(credits?.length) && ( -
    - {credits?.map((actor) => ( -
  • > + {(setRef) => ( +
      - onPersonClick?.(actor.id.toString())} - > -
      - {actor.profile_path ? ( - {actor.name} - ) : ( -
      - + {credits?.map((actor) => ( +
    • + onPersonClick?.(actor.id.toString())} + > +
      + {actor.profile_path ? ( + {actor.name} + ) : ( +
      + +
      + )}
      - )} -
    • -
      -

      {actor.name}

      - {Boolean(actor.character) && ( -

      {actor.character}

      - )} -
      - - - ))} -
    +
    +

    {actor.name}

    + {Boolean(actor.character) &&

    {actor.character}

    } +
    + +
  • + ))} +
+ )} + ) ); }; diff --git a/components/Trending/components/Gallery/index.tsx b/components/Trending/components/Gallery/index.tsx new file mode 100644 index 0000000..3c182ac --- /dev/null +++ b/components/Trending/components/Gallery/index.tsx @@ -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 = ({ shows }) => ( + > + {(setRef) => ( +
    + {shows.map((show) => ( +
  • + showPreviewModal({ show })} /> +
  • + ))} +
+ )} + +); + +export default Gallery; diff --git a/components/Trending/components/Marquee/index.tsx b/components/Trending/components/Marquee/index.tsx deleted file mode 100644 index 07f115b..0000000 --- a/components/Trending/components/Marquee/index.tsx +++ /dev/null @@ -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 = ({ shows }) => ( -
    - {shows.map((show) => ( -
  • - showPreviewModal({ show })} /> -
  • - ))} -
-); - -export default Marquee; diff --git a/components/Trending/index.tsx b/components/Trending/index.tsx index 648de72..fbe93c4 100644 --- a/components/Trending/index.tsx +++ b/components/Trending/index.tsx @@ -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 && (