@@ -14,7 +14,7 @@ import {
1414} from "react" ;
1515
1616import clsx from "classnames" ;
17- import useMeasure from "react-use-measure " ;
17+ import useMeasure from "./hooks/useMeasure " ;
1818
1919import CarouselArrows from "./CarouselArrows" ;
2020import CarouselDots , { DotRenderFnProps } from "./CarouselDots" ;
@@ -81,7 +81,8 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
8181 const containerReactRef = useRef < HTMLDivElement | null > ( null ) ;
8282 const autoPlayIntervalRef = useRef < NodeJS . Timeout | null > ( null ) ;
8383
84- const [ containerRef , containerBounds ] = useMeasure ( { debounce : 100 } ) ;
84+ const [ containerRef , { width } ] = useMeasure ( ) ;
85+ const containerWidth = useMemo ( ( ) => width ?? 0 , [ width ] ) ;
8586
8687 const [ currentSlide , setCurrentSlide ] = useState ( 0 ) ;
8788
@@ -94,18 +95,25 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
9495 const scrollTimeout = useRef < NodeJS . Timeout | null > ( null ) ;
9596
9697 /** Maximum offset when dragging */
97- const maxDragOffset = useMemo (
98- ( ) => containerBounds . width ,
99- [ containerBounds . width ] ,
100- ) ;
98+ const maxDragOffset = useMemo ( ( ) => containerWidth , [ containerWidth ] ) ;
10199 /** Maximum offset when dragging out of bounds on last or first slide */
102100 const maxDragOffsetEnd = useMemo (
103- ( ) => containerBounds . width / 5 ,
104- [ containerBounds . width ] ,
101+ ( ) => containerWidth / 5 ,
102+ [ containerWidth ] ,
103+ ) ;
104+
105+ const mappedChildren = useMemo (
106+ ( ) => Children . toArray ( children ) . filter ( Boolean ) ,
107+ [ children ] ,
105108 ) ;
106109
107110 /** Total number of slides */
108- const slides = useMemo ( ( ) => Children . count ( children ) , [ children ] ) ;
111+ const slides = useMemo ( ( ) => mappedChildren . length , [ mappedChildren ] ) ;
112+
113+ const slideWidth = useMemo (
114+ ( ) => containerWidth / shownSlides ,
115+ [ containerWidth , shownSlides ] ,
116+ ) ;
109117
110118 // Slide change logic
111119
@@ -142,11 +150,11 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
142150
143151 const translateX = useMemo ( ( ) => {
144152 const maxTranslateX =
145- ( slides - shownSlides ) * ( containerBounds . width / shownSlides ) ;
153+ ( slides - shownSlides ) * ( containerWidth / shownSlides ) ;
146154 const calculatedTranslateX =
147- currentSlide * ( containerBounds . width / shownSlides ) ;
155+ currentSlide * ( containerWidth / shownSlides ) ;
148156 return Math . min ( calculatedTranslateX , maxTranslateX ) ;
149- } , [ currentSlide , containerBounds . width , shownSlides , slides ] ) ;
157+ } , [ currentSlide , containerWidth , shownSlides , slides ] ) ;
150158
151159 const handleDragStart = useCallback ( ( event : MouseEvent | TouchEvent ) => {
152160 const clientX =
@@ -196,7 +204,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
196204 dragStart . y ,
197205 translateX ,
198206 trackRef . current ,
199- containerBounds . width ,
207+ containerWidth ,
200208 scrolling ,
201209 ] ,
202210 ) ;
@@ -211,13 +219,13 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
211219
212220 // Maximum allowable translation in pixels
213221 const maxTranslateX =
214- ( containerBounds . width * ( slides - shownSlides ) ) / shownSlides ;
222+ ( containerWidth * ( slides - shownSlides ) ) / shownSlides ;
215223
216224 // Determine the new slide index based on drag offset
217225 let newSlide = currentSlide ;
218- if ( dragOffset > containerBounds . width / 2 ) {
226+ if ( dragOffset > containerWidth / 2 ) {
219227 newSlide = Math . max ( 0 , currentSlide - 1 ) ;
220- } else if ( dragOffset < - containerBounds . width / 2 ) {
228+ } else if ( dragOffset < - containerWidth / 2 ) {
221229 newSlide = Math . min ( slides - shownSlides , currentSlide + 1 ) ;
222230 }
223231
@@ -229,18 +237,15 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
229237 if ( trackRef . current ) {
230238 const newTranslateX = Math . max (
231239 0 ,
232- Math . min (
233- maxTranslateX ,
234- ( newSlide * containerBounds . width ) / shownSlides ,
235- ) ,
240+ Math . min ( maxTranslateX , ( newSlide * containerWidth ) / shownSlides ) ,
236241 ) ;
237242 trackRef . current . style . transform = `translateX(-${ newTranslateX } px)` ;
238243 }
239244 } ,
240245 [
241246 dragging ,
242247 dragStart . x ,
243- containerBounds . width ,
248+ containerWidth ,
244249 slides ,
245250 shownSlides ,
246251 currentSlide ,
@@ -265,7 +270,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
265270 if ( scrollTimeout . current ) {
266271 clearTimeout ( scrollTimeout . current ) ;
267272 }
268- scrollTimeout . current = setTimeout ( handleScrollEnd , 500 ) ;
273+ scrollTimeout . current = setTimeout ( handleScrollEnd , 300 ) ;
269274 } ;
270275
271276 const handleScrollEnd = ( ) => {
@@ -325,8 +330,8 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
325330 return (
326331 < div
327332 ref = { ( node ) => {
328- containerRef ( node ) ;
329333 containerReactRef . current = node ;
334+ containerRef ( node ) ;
330335 } }
331336 className = { clsx ( "Carousel" , containerClassName ) }
332337 style = { {
@@ -338,28 +343,32 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
338343 style = { trackStyle }
339344 className = { clsx ( "Carousel__track" , trackClassName ) }
340345 >
341- { Children . map ( children , ( child , i ) => (
342- < div
343- key = { `slide-${ i } ` }
344- data-index = { i }
345- style = { {
346- width : containerBounds . width / shownSlides ,
347- } }
348- className = { clsx ( {
349- Carousel__slide : true ,
350- "Carousel__slide--dragging" : dragging ,
351- "Carousel__slide--active" : currentSlide === i ,
352-
353- ...( slideClassName && {
354- [ slideClassName ] : true ,
355- [ `${ slideClassName } --dragging` ] : dragging ,
356- [ `${ slideClassName } --active` ] : currentSlide === i ,
357- } ) ,
358- } ) }
359- >
360- { child }
361- </ div >
362- ) ) }
346+ { Children . toArray ( children )
347+ . filter ( Boolean )
348+ . map ( ( child , i ) => {
349+ return (
350+ < div
351+ key = { `slide-${ i } ` }
352+ data-index = { i }
353+ style = { {
354+ width : slideWidth ,
355+ } }
356+ className = { clsx ( {
357+ Carousel__slide : true ,
358+ "Carousel__slide--dragging" : dragging ,
359+ "Carousel__slide--active" : currentSlide === i ,
360+
361+ ...( slideClassName && {
362+ [ slideClassName ] : true ,
363+ [ `${ slideClassName } --dragging` ] : dragging ,
364+ [ `${ slideClassName } --active` ] : currentSlide === i ,
365+ } ) ,
366+ } ) }
367+ >
368+ { child }
369+ </ div >
370+ ) ;
371+ } ) }
363372 </ div >
364373 { customDots &&
365374 customDots ( {
0 commit comments