Skip to content

Commit fd9ae1c

Browse files
authored
fix: improve modal dismissing (#346)
* refactor: updated modal dismiss handling * fix: prevent crash when trying to findnode when component is unmount
1 parent 36136e2 commit fd9ae1c

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ SPEC CHECKSUMS:
516516
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
517517
DoubleConversion: cde416483dac037923206447da6e1454df403714
518518
FBLazyVector: 49cbe4b43e445b06bf29199b6ad2057649e4c8f5
519-
FBReactNativeSpec: 8856510b0a0bfdb6c496df732875a76198f16cad
519+
FBReactNativeSpec: c426cca9d43fd70ac19a77e504a6fd12ecb473f6
520520
Flipper: da2ca681187fb82c668a2c3c54b003cbd3c26859
521521
Flipper-DoubleConversion: 38631e41ef4f9b12861c67d17cb5518d06badc41
522522
Flipper-Folly: f7a3caafbd74bda4827954fd7a6e000e36355489

src/components/bottomSheet/BottomSheet.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Animated, {
2121
interpolate,
2222
Extrapolate,
2323
runOnUI,
24+
cancelAnimation,
2425
useWorkletCallback,
2526
} from 'react-native-reanimated';
2627
import { State } from 'react-native-gesture-handler';
@@ -292,15 +293,27 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
292293
// callbacks
293294
const animateToPointCompleted = useWorkletCallback(() => {
294295
animationState.value = ANIMATION_STATE.STOPPED;
295-
}, [animatedIndex, animationState, handleOnChange, refreshUIElements]);
296+
});
296297
const animateToPoint = useWorkletCallback(
297298
(
298299
point: number,
299300
velocity: number = 0,
300301
animationDuration?: number,
301302
animationEasing?: Animated.EasingFunction
302303
) => {
304+
/**
305+
* cancel current running animation
306+
*/
307+
cancelAnimation(animatedPosition);
308+
309+
/**
310+
* set animation state to running
311+
*/
303312
animationState.value = ANIMATION_STATE.RUNNING;
313+
314+
/**
315+
* fire `onAnimate` callback
316+
*/
304317
runOnJS(handleOnAnimate)(point);
305318

306319
/**

src/components/bottomSheetModal/BottomSheetModal.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,18 @@ const BottomSheetModalComponent = forwardRef<
167167
return;
168168
}
169169

170-
if (minimized.current) {
170+
if (
171+
minimized.current ||
172+
(currentIndexRef.current === 0 && dismissOnPanDown)
173+
) {
171174
unmount();
172175
return;
173176
}
174177
willUnmountSheet(key);
175178
forcedDismissed.current = true;
176179
bottomSheetRef.current?.close(...args);
177180
},
178-
[willUnmountSheet, unmount, key]
181+
[willUnmountSheet, unmount, key, dismissOnPanDown]
179182
);
180183
const handleMinimize = useCallback(() => {
181184
if (minimized.current) {

src/components/bottomSheetModalProvider/BottomSheetModalProvider.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,20 @@ const BottomSheetModalProviderWrapper = ({
8989
_sheetsQueue.splice(sheetIndex, 1);
9090
sheetsQueueRef.current = _sheetsQueue;
9191

92-
// /**
93-
// * Here we try to restore previous sheet position if unmounted
94-
// * sheet was on top. This is needed when user dismiss
95-
// * the modal by panning down.
96-
// */
92+
/**
93+
* Here we try to restore previous sheet position if unmounted
94+
* sheet was on top. This is needed when user dismiss
95+
* the modal by panning down.
96+
*/
9797
const hasMinimizedSheet = sheetsQueueRef.current.length > 0;
98-
if (sheetOnTop && hasMinimizedSheet) {
98+
const minimizedSheet =
99+
sheetsQueueRef.current[sheetsQueueRef.current.length - 1];
100+
if (
101+
sheetOnTop &&
102+
hasMinimizedSheet &&
103+
minimizedSheet &&
104+
!minimizedSheet.willUnmount
105+
) {
99106
sheetsQueueRef.current[
100107
sheetsQueueRef.current.length - 1
101108
].ref.current.restore();

src/hooks/useScrollable.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ export const useScrollable = () => {
2929

3030
const removeScrollableRef = useCallback((ref: RefObject<Scrollable>) => {
3131
// find node handle id
32-
let id = findNodeHandle(ref.current);
32+
let id;
33+
try {
34+
id = findNodeHandle(ref.current);
35+
} catch {
36+
return;
37+
}
3338

3439
// get current node handle id
3540
let currentRefId = scrollableRef.current?.id ?? null;

0 commit comments

Comments
 (0)