@@ -32,6 +32,10 @@ type CarouselContextProps = {
3232 canScrollNext : boolean ;
3333} & CarouselProps ;
3434
35+ type CarouselNavProps = {
36+ customOnClick ?: ( ) => void ;
37+ } ;
38+
3539const CarouselContext = React . createContext < CarouselContextProps | null > ( null ) ;
3640
3741function useCarousel ( ) {
@@ -235,10 +239,15 @@ CarouselItem.displayName = 'CarouselItem';
235239
236240const CarouselPrevious = React . forwardRef <
237241 HTMLButtonElement ,
238- React . ComponentProps < typeof Button >
239- > ( ( { className, variant = 'transparent' , size = 'icon' , ...props } , ref ) => {
242+ React . ComponentProps < typeof Button > & CarouselNavProps
243+ > ( ( { className, variant = 'transparent' , size = 'icon' , customOnClick , ...props } , ref ) => {
240244 const { orientation, scrollPrev, canScrollPrev } = useCarousel ( ) ;
241245
246+ const handleClick = ( ) => {
247+ scrollPrev ( ) ;
248+ if ( customOnClick ) customOnClick ( ) ;
249+ } ;
250+
242251 return (
243252 < Button
244253 ref = { ref }
@@ -252,7 +261,7 @@ const CarouselPrevious = React.forwardRef<
252261 className
253262 ) }
254263 disabled = { ! canScrollPrev }
255- onClick = { scrollPrev }
264+ onClick = { handleClick }
256265 { ...props }
257266 >
258267 < ChevronLeft className = "!h-full !w-full text-primary" />
@@ -264,10 +273,15 @@ CarouselPrevious.displayName = 'CarouselPrevious';
264273
265274const CarouselNext = React . forwardRef <
266275 HTMLButtonElement ,
267- React . ComponentProps < typeof Button >
268- > ( ( { className, variant = 'transparent' , size = 'icon' , ...props } , ref ) => {
276+ React . ComponentProps < typeof Button > & CarouselNavProps
277+ > ( ( { className, variant = 'transparent' , size = 'icon' , customOnClick , ...props } , ref ) => {
269278 const { orientation, scrollNext, canScrollNext } = useCarousel ( ) ;
270279
280+ const handleClick = ( ) => {
281+ scrollNext ( ) ;
282+ if ( customOnClick ) customOnClick ( ) ;
283+ }
284+
271285 return (
272286 < Button
273287 ref = { ref }
@@ -281,7 +295,7 @@ const CarouselNext = React.forwardRef<
281295 className
282296 ) }
283297 disabled = { ! canScrollNext }
284- onClick = { scrollNext }
298+ onClick = { handleClick }
285299 { ...props }
286300 >
287301 < ChevronRight className = "!h-full !w-full text-primary" />
0 commit comments