Skip to content

Commit e4f3fe3

Browse files
committed
fix: updated animation sequencing to respect force closing by user (#1941)
1 parent 2962a2d commit e4f3fe3

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/components/bottomSheet/BottomSheet.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,11 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
615615
//#region animation
616616
const stopAnimation = useWorkletCallback(() => {
617617
cancelAnimation(animatedPosition);
618-
isForcedClosing.value = false;
619618
animatedAnimationSource.value = ANIMATION_SOURCE.NONE;
620619
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
621620
}, [animatedPosition, animatedAnimationState, animatedAnimationSource]);
622621
const animateToPositionCompleted = useWorkletCallback(
623622
function animateToPositionCompleted(isFinished?: boolean) {
624-
isForcedClosing.value = false;
625-
626623
if (!isFinished) {
627624
return;
628625
}
@@ -643,6 +640,8 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
643640
isAnimatedOnMount.value = true;
644641
}
645642

643+
isForcedClosing.value = false;
644+
646645
// reset values
647646
animatedAnimationSource.value = ANIMATION_SOURCE.NONE;
648647
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
@@ -678,7 +677,10 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
678677
return;
679678
}
680679

681-
stopAnimation();
680+
// stop animation if it is running
681+
if (animatedAnimationState.value === ANIMATION_STATE.RUNNING) {
682+
stopAnimation();
683+
}
682684

683685
/**
684686
* set animation state to running, and source
@@ -883,6 +885,12 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
883885
source: ANIMATION_SOURCE,
884886
animationConfigs?: WithSpringConfig | WithTimingConfig
885887
) {
888+
/**
889+
* if a force closing is running and source not from user, then we early exit
890+
*/
891+
if (isForcedClosing.value && source !== ANIMATION_SOURCE.USER) {
892+
return;
893+
}
886894
/**
887895
* when evaluating the position while layout is not calculated, then we early exit till it is.
888896
*/

src/components/bottomSheetModal/BottomSheetModal.tsx

+29-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const BottomSheetModalComponent = forwardRef<
4444
stackBehavior = DEFAULT_STACK_BEHAVIOR,
4545
enableDismissOnClose = DEFAULT_ENABLE_DISMISS_ON_CLOSE,
4646
onDismiss: _providedOnDismiss,
47+
onAnimate: _providedOnAnimate,
4748

4849
// bottom sheet props
4950
index = 0,
@@ -78,6 +79,7 @@ const BottomSheetModalComponent = forwardRef<
7879
//#region refs
7980
const bottomSheetRef = useRef<BottomSheet>(null);
8081
const currentIndexRef = useRef(!animateOnMount ? index : -1);
82+
const nextIndexRef = useRef<number | null>(null);
8183
const restoreIndexRef = useRef(-1);
8284
const minimized = useRef(false);
8385
const forcedDismissed = useRef(false);
@@ -225,16 +227,27 @@ const BottomSheetModalComponent = forwardRef<
225227
},
226228
});
227229
}
230+
231+
const animating = nextIndexRef.current != null;
232+
228233
/**
229-
* if modal is already been dismiss, we exit the method.
234+
* early exit, if not minimized, it is in closed position and not animating
230235
*/
231-
if (currentIndexRef.current === -1 && minimized.current === false) {
236+
if (
237+
currentIndexRef.current === -1 &&
238+
minimized.current === false &&
239+
!animating
240+
) {
232241
return;
233242
}
234243

244+
/**
245+
* unmount and early exit, if minimized or it is in closed position and not animating
246+
*/
235247
if (
236-
minimized.current ||
237-
(currentIndexRef.current === -1 && enablePanDownToClose)
248+
!animating &&
249+
(minimized.current ||
250+
(currentIndexRef.current === -1 && enablePanDownToClose))
238251
) {
239252
unmount();
240253
return;
@@ -355,13 +368,24 @@ const BottomSheetModalComponent = forwardRef<
355368
});
356369
}
357370
currentIndexRef.current = _index;
371+
nextIndexRef.current = null;
358372

359373
if (_providedOnChange) {
360374
_providedOnChange(_index, _position, _type);
361375
}
362376
},
363377
[_providedOnChange]
364378
);
379+
const handleBottomSheetOnAnimate = useCallback(
380+
(fromIndex: number, toIndex: number) => {
381+
nextIndexRef.current = toIndex;
382+
383+
if (_providedOnAnimate) {
384+
_providedOnAnimate(fromIndex, toIndex);
385+
}
386+
},
387+
[_providedOnAnimate]
388+
);
365389
// biome-ignore lint/correctness/useExhaustiveDependencies(BottomSheetModal.name): used for debug only
366390
const handleBottomSheetOnClose = useCallback(
367391
function handleBottomSheetOnClose() {
@@ -429,6 +453,7 @@ const BottomSheetModalComponent = forwardRef<
429453
containerOffset={containerOffset}
430454
onChange={handleBottomSheetOnChange}
431455
onClose={handleBottomSheetOnClose}
456+
onAnimate={handleBottomSheetOnAnimate}
432457
$modal={true}
433458
>
434459
{typeof Content === 'function' ? <Content data={data} /> : Content}

0 commit comments

Comments
 (0)