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

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 +40 -3
  2. package/package.json +1 -1
package/index.tsx CHANGED
@@ -384,8 +384,18 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
384
384
  );
385
385
  const realIndex = getRealIndex(index);
386
386
 
387
- currentIndexRef.current = realIndex;
388
- setCurrentIndex(realIndex);
387
+ // Report the final resting index here as well. The reanimated scroll
388
+ // handler isn't guaranteed to deliver the last momentum frame to JS in
389
+ // every runtime (e.g. miniapp hosts), so relying on handleScroll alone
390
+ // can leave onScrollIndexChanged stuck one item short of the end.
391
+ if (realIndex !== currentIndexRef.current) {
392
+ currentIndexRef.current = realIndex;
393
+ setCurrentIndex(realIndex);
394
+
395
+ if (onScrollIndexChanged) {
396
+ onScrollIndexChanged(realIndex);
397
+ }
398
+ }
389
399
 
390
400
  if (onSnapToItem) {
391
401
  onSnapToItem(realIndex);
@@ -397,7 +407,33 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
397
407
  onMomentumScrollEndProp(event);
398
408
  }
399
409
  },
400
- [resolveIndex, getRealIndex, onSnapToItem, handleLoopReposition, onMomentumScrollEndProp]
410
+ [resolveIndex, getRealIndex, onScrollIndexChanged, onSnapToItem, handleLoopReposition, onMomentumScrollEndProp]
411
+ );
412
+
413
+ const handleScrollEndDrag = useCallback(
414
+ (event: NativeSyntheticEvent<NativeScrollEvent>) => {
415
+ // Fallback for a drag released without enough velocity to fire a momentum
416
+ // scroll: report the resting index so onScrollIndexChanged isn't left
417
+ // stuck one item short of the end.
418
+ const { contentOffset, layoutMeasurement, contentSize } =
419
+ event.nativeEvent;
420
+ const index = resolveIndex(
421
+ contentOffset.x,
422
+ layoutMeasurement.width,
423
+ contentSize.width
424
+ );
425
+ const realIndex = getRealIndex(index);
426
+
427
+ if (realIndex !== currentIndexRef.current) {
428
+ currentIndexRef.current = realIndex;
429
+ setCurrentIndex(realIndex);
430
+
431
+ if (onScrollIndexChanged) {
432
+ onScrollIndexChanged(realIndex);
433
+ }
434
+ }
435
+ },
436
+ [resolveIndex, getRealIndex, onScrollIndexChanged]
401
437
  );
402
438
 
403
439
  const handleTouchStart = useCallback(
@@ -581,6 +617,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>((props, ref) => {
581
617
  scrollEventThrottle={16}
582
618
  onScroll={onScrollEvent}
583
619
  onMomentumScrollEnd={handleMomentumScrollEnd}
620
+ onScrollEndDrag={handleScrollEndDrag}
584
621
  onTouchStart={handleTouchStart}
585
622
  onTouchEnd={handleTouchEnd}
586
623
  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.1",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "peerDependencies": {