diff --git a/src/pages/discover/components/CarouselNavButtons.tsx b/src/pages/discover/components/CarouselNavButtons.tsx index 1170fcaf..bd9d3261 100644 --- a/src/pages/discover/components/CarouselNavButtons.tsx +++ b/src/pages/discover/components/CarouselNavButtons.tsx @@ -8,6 +8,37 @@ interface CarouselNavButtonsProps { }>; } +interface NavButtonProps { + direction: "left" | "right"; + onClick: () => void; +} + +function NavButton({ direction, onClick }: NavButtonProps) { + return ( + + + + + + + + + ); +} + export function CarouselNavButtons({ categorySlug, carouselRefs, @@ -19,52 +50,34 @@ export function CarouselNavButtons({ const movieElements = carousel.getElementsByTagName("a"); if (movieElements.length === 0) return; - const movieWidth = movieElements[0].offsetWidth; - const visibleMovies = Math.floor(carousel.offsetWidth / movieWidth); - const scrollAmount = movieWidth * (visibleMovies > 5 ? 4 : 2); + // Wait for next frame to ensure measurements are available + requestAnimationFrame(() => { + const movieWidth = movieElements[0].getBoundingClientRect().width; - carousel.scrollBy({ - left: direction === "left" ? -scrollAmount : scrollAmount, - behavior: "smooth", + const carouselWidth = carousel.getBoundingClientRect().width; + + if (movieWidth === 0 || carouselWidth === 0) { + return; + } + + const visibleMovies = Math.floor(carouselWidth / movieWidth); + const scrollAmount = movieWidth * (visibleMovies > 5 ? 4 : 2); + + const newScrollPosition = + carousel.scrollLeft + + (direction === "left" ? -scrollAmount : scrollAmount); + + carousel.scrollTo({ + left: newScrollPosition, + behavior: "smooth", + }); }); }; return ( <> - handleScroll("left")} - > - - - - - - - - handleScroll("right")} - > - - - - - - - + handleScroll("left")} /> + handleScroll("right")} /> > ); } diff --git a/src/pages/discover/components/MediaCarousel.tsx b/src/pages/discover/components/MediaCarousel.tsx index 6c160145..319863fd 100644 --- a/src/pages/discover/components/MediaCarousel.tsx +++ b/src/pages/discover/components/MediaCarousel.tsx @@ -78,7 +78,7 @@ export function MediaCarousel({ { carouselRefs.current[categorySlug] = el; }}