@@ -51,16 +51,15 @@ export default function EventCarousel({ items }: EventCarouselProps) {
5151
5252 useEffect ( ( ) => {
5353 const updateVisibleCount = ( ) => {
54- if ( window . innerWidth < 768 ) {
55- setVisibleCount ( 1 ) ;
56- } else {
57- setVisibleCount ( 3 ) ;
58- }
54+ const newVisibleCount = window . innerWidth < 768 ? 1 : 3 ;
55+ const newMaxIndex = Math . max ( 0 , items . length - newVisibleCount ) ;
56+ setVisibleCount ( newVisibleCount ) ;
57+ setCurrentIndex ( ( prev ) => Math . min ( prev , newMaxIndex ) ) ;
5958 } ;
6059 updateVisibleCount ( ) ;
6160 window . addEventListener ( "resize" , updateVisibleCount ) ;
6261 return ( ) => window . removeEventListener ( "resize" , updateVisibleCount ) ;
63- } , [ ] ) ;
62+ } , [ items . length ] ) ;
6463
6564 return (
6665 < div className = "container mx-auto rounded-lg bg-primary-foreground px-4 py-8 lg:px-12" >
@@ -99,7 +98,7 @@ export default function EventCarousel({ items }: EventCarouselProps) {
9998 ) }
10099
101100 < div className = "mt-10 px-10" >
102- < div ref = { viewportRef } className = "overflow-hidden" >
101+ < div ref = { viewportRef } className = "overflow-hidden px-2 md:px-4 " >
103102 < div
104103 className = "flex transition-transform duration-300 ease-out"
105104 style = { {
@@ -112,7 +111,7 @@ export default function EventCarousel({ items }: EventCarouselProps) {
112111 href = { `/events/${ event . id } ` }
113112 key = { event . id }
114113 ref = { index === 0 ? firstItemRef : undefined }
115- className = { `block w-full flex-shrink-0 rounded-xl transition-transform duration-200 ease-in-out hover:scale-110 md:w-[calc((100%-80px)/3)] ${ index === 0 ? "origin-left" : "" } ` }
114+ className = { `block w-full flex-shrink-0 rounded-xl transition-transform duration-200 ease-in-out hover:scale-110 md:w-[calc((100%-80px)/3)] ${ index === currentIndex ? "origin-left" : "" } ` }
116115 >
117116 < div className = "relative aspect-[16/9] w-full overflow-hidden rounded-lg" >
118117 < Image
0 commit comments