Skip to content

Commit 88307f4

Browse files
committed
fix: onAnimate not called with -1 when immediately closed + add source
Reapply these commits: - gorhom@d66fae4 - gorhom@2216d85
1 parent d6693b1 commit 88307f4

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/components/bottomSheet/BottomSheet.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
542542
);
543543
// biome-ignore lint/correctness/useExhaustiveDependencies(BottomSheet.name): used for debug only
544544
const handleOnAnimate = useCallback(
545-
function handleOnAnimate(targetIndex: number, targetPosition: number) {
545+
function handleOnAnimate(targetIndex: number, targetPosition: number, source: ANIMATION_SOURCE) {
546546
if (__DEV__) {
547547
print({
548548
component: BottomSheet.name,
@@ -561,12 +561,16 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
561561
return;
562562
}
563563

564-
if (targetIndex !== animatedCurrentIndex.value) {
564+
if (targetIndex !== animatedCurrentIndex.value
565+
// there is a race condition when opening and immedately closing the bottom sheet, where
566+
// the animatedCurrentIndex is not updated yet. As we don't want to miss close events we always call the callback for -1 changes:
567+
|| targetIndex === -1) {
565568
_providedOnAnimate(
566569
animatedCurrentIndex.value,
567570
targetIndex,
568571
animatedPosition.value,
569-
targetPosition
572+
targetPosition,
573+
source,
570574
);
571575
}
572576
},
@@ -675,7 +679,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
675679
/**
676680
* fire `onAnimate` callback
677681
*/
678-
runOnJS(handleOnAnimate)(animatedNextPositionIndex.value, position);
682+
runOnJS(handleOnAnimate)(animatedNextPositionIndex.value, position, source);
679683

680684
/**
681685
* start animation

src/components/bottomSheet/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ export interface BottomSheetProps
290290
fromIndex: number,
291291
toIndex: number,
292292
fromPosition: number,
293-
toPosition: number
293+
toPosition: number,
294+
source: ANIMATION_SOURCE
294295
) => void;
295296
//#endregion
296297

src/components/bottomSheetModal/BottomSheetModal.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import React, {
99
useRef,
1010
useState,
1111
} from 'react';
12-
import type { SNAP_POINT_TYPE } from '../../constants';
12+
import type { ANIMATION_SOURCE, SNAP_POINT_TYPE } from '../../constants';
1313
import { useBottomSheetModalInternal } from '../../hooks';
1414
import type { BottomSheetMethods, BottomSheetModalMethods } from '../../types';
1515
import { print } from '../../utilities';
@@ -383,12 +383,13 @@ function BottomSheetModalComponent<T = any>(
383383
fromIndex: number,
384384
toIndex: number,
385385
fromPosition: number,
386-
toPosition: number
386+
toPosition: number,
387+
source: ANIMATION_SOURCE
387388
) => {
388389
nextIndexRef.current = toIndex;
389390

390391
if (_providedOnAnimate) {
391-
_providedOnAnimate(fromIndex, toIndex, fromPosition, toPosition);
392+
_providedOnAnimate(fromIndex, toIndex, fromPosition, toPosition, source);
392393
}
393394
},
394395
[_providedOnAnimate]

0 commit comments

Comments
 (0)