@momo-kits/carousel 0.160.1-beta.10 → 0.160.1-beta.10-test.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.tsx +60 -5
  2. package/package.json +1 -1
package/index.tsx CHANGED
@@ -361,6 +361,10 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
361
361
  const realIndex = getRealIndex(index);
362
362
 
363
363
  if (realIndex !== currentIndexRef.current) {
364
+ // eslint-disable-next-line no-console
365
+ console.log(
366
+ `[Carousel] SCROLL idx=${index} real=${realIndex} offsetX=${offsetX.toFixed(1)} layoutW=${layoutWidth.toFixed(1)} contentW=${contentWidth.toFixed(1)} itemWidth=${itemWidth.toFixed(1)} count=${dataWithClones.length}`
367
+ );
364
368
  currentIndexRef.current = realIndex;
365
369
  setCurrentIndex(realIndex);
366
370
 
@@ -369,7 +373,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
369
373
  }
370
374
  }
371
375
  },
372
- [resolveIndex, getRealIndex, onScrollIndexChanged]
376
+ [resolveIndex, getRealIndex, onScrollIndexChanged, itemWidth, dataWithClones.length]
373
377
  );
374
378
 
375
379
  const handleMomentumScrollEnd = useCallback(
@@ -384,8 +388,23 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
384
388
  );
385
389
  const realIndex = getRealIndex(index);
386
390
 
387
- currentIndexRef.current = realIndex;
388
- setCurrentIndex(realIndex);
391
+ // eslint-disable-next-line no-console
392
+ console.log(
393
+ `[Carousel] MOMENTUM_END idx=${index} real=${realIndex} offsetX=${offsetX.toFixed(1)} layoutW=${layoutMeasurement.width.toFixed(1)} contentW=${contentSize.width.toFixed(1)} itemWidth=${itemWidth.toFixed(1)} count=${dataWithClones.length}`
394
+ );
395
+
396
+ // Report the final resting index here as well. The reanimated scroll
397
+ // handler isn't guaranteed to deliver the last momentum frame to JS in
398
+ // every runtime (e.g. miniapp hosts), so relying on handleScroll alone
399
+ // can leave onScrollIndexChanged stuck one item short of the end.
400
+ if (realIndex !== currentIndexRef.current) {
401
+ currentIndexRef.current = realIndex;
402
+ setCurrentIndex(realIndex);
403
+
404
+ if (onScrollIndexChanged) {
405
+ onScrollIndexChanged(realIndex);
406
+ }
407
+ }
389
408
 
390
409
  if (onSnapToItem) {
391
410
  onSnapToItem(realIndex);
@@ -397,7 +416,38 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
397
416
  onMomentumScrollEndProp(event);
398
417
  }
399
418
  },
400
- [resolveIndex, getRealIndex, onSnapToItem, handleLoopReposition, onMomentumScrollEndProp]
419
+ [resolveIndex, getRealIndex, onScrollIndexChanged, onSnapToItem, handleLoopReposition, onMomentumScrollEndProp, itemWidth, dataWithClones.length]
420
+ );
421
+
422
+ const handleScrollEndDrag = useCallback(
423
+ (event: NativeSyntheticEvent<NativeScrollEvent>) => {
424
+ // Fallback for a drag released without enough velocity to fire a momentum
425
+ // scroll: report the resting index so onScrollIndexChanged isn't left
426
+ // stuck one item short of the end.
427
+ const { contentOffset, layoutMeasurement, contentSize } =
428
+ event.nativeEvent;
429
+ const index = resolveIndex(
430
+ contentOffset.x,
431
+ layoutMeasurement.width,
432
+ contentSize.width
433
+ );
434
+ const realIndex = getRealIndex(index);
435
+
436
+ // eslint-disable-next-line no-console
437
+ console.log(
438
+ `[Carousel] DRAG_END idx=${index} real=${realIndex} offsetX=${contentOffset.x.toFixed(1)} layoutW=${layoutMeasurement.width.toFixed(1)} contentW=${contentSize.width.toFixed(1)} itemWidth=${itemWidth.toFixed(1)} count=${dataWithClones.length}`
439
+ );
440
+
441
+ if (realIndex !== currentIndexRef.current) {
442
+ currentIndexRef.current = realIndex;
443
+ setCurrentIndex(realIndex);
444
+
445
+ if (onScrollIndexChanged) {
446
+ onScrollIndexChanged(realIndex);
447
+ }
448
+ }
449
+ },
450
+ [resolveIndex, getRealIndex, onScrollIndexChanged, itemWidth, dataWithClones.length]
401
451
  );
402
452
 
403
453
  const handleTouchStart = useCallback(
@@ -427,6 +477,10 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
427
477
  const handleLayout = useCallback(
428
478
  (event: LayoutChangeEvent) => {
429
479
  const { width } = event.nativeEvent.layout;
480
+ // eslint-disable-next-line no-console
481
+ console.log(
482
+ `[Carousel] LAYOUT width=${width.toFixed(1)} window=${viewportWidth.toFixed(1)} full=${full}`
483
+ );
430
484
  containerWidthRef.current = width;
431
485
  setContainerWidth(width);
432
486
  setIsLayoutReady(true);
@@ -435,7 +489,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
435
489
  onLayoutProp(event);
436
490
  }
437
491
  },
438
- [onLayoutProp]
492
+ [onLayoutProp, full]
439
493
  );
440
494
 
441
495
  const getItemLayout = useCallback(
@@ -581,6 +635,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
581
635
  scrollEventThrottle={16}
582
636
  onScroll={onScrollEvent}
583
637
  onMomentumScrollEnd={handleMomentumScrollEnd}
638
+ onScrollEndDrag={handleScrollEndDrag}
584
639
  onTouchStart={handleTouchStart}
585
640
  onTouchEnd={handleTouchEnd}
586
641
  snapToOffsets={snapOffsets}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/carousel",
3
- "version": "0.160.1-beta.10",
3
+ "version": "0.160.1-beta.10-test.2",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "peerDependencies": {