Skip to content

Commit

Permalink
refactor: added forceClose and remove force param from close method
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Aug 21, 2021
1 parent 8e002e1 commit 3dd5796
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
53 changes: 48 additions & 5 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -833,10 +833,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
);
const handleClose = useCallback(
function handleClose(
animationConfigs?:
| Animated.WithSpringConfig
| Animated.WithTimingConfig,
force?: boolean
animationConfigs?: Animated.WithSpringConfig | Animated.WithTimingConfig
) {
print({
component: BottomSheet.name,
Expand All @@ -862,10 +859,53 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
*/
isInTemporaryPosition.value = false;

runOnUI(animateToPosition)(
nextPosition,
ANIMATION_SOURCE.USER,
0,
animationConfigs
);
},
[
animateToPosition,
isForcedClosing,
isInTemporaryPosition,
animatedNextPosition,
animatedClosedPosition,
]
);
const handleForceClose = useCallback(
function handleForceClose(
animationConfigs?: Animated.WithSpringConfig | Animated.WithTimingConfig
) {
print({
component: BottomSheet.name,
method: handleForceClose.name,
});

const nextPosition = animatedClosedPosition.value;

/**
* exit method if :
* - already animating to next position.
* - sheet is forced closing.
*/
if (
nextPosition === animatedNextPosition.value ||
isForcedClosing.value
) {
return;
}

/**
* reset temporary position variable.
*/
isInTemporaryPosition.value = false;

/**
* set force closing variable.
*/
isForcedClosing.value = force === undefined ? false : force;
isForcedClosing.value = true;

runOnUI(animateToPosition)(
nextPosition,
Expand Down Expand Up @@ -980,6 +1020,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
expand: handleExpand,
collapse: handleCollapse,
close: handleClose,
forceClose: handleForceClose,
}));
//#endregion

Expand Down Expand Up @@ -1075,6 +1116,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
expand: handleExpand,
collapse: handleCollapse,
close: handleClose,
forceClose: handleForceClose,
}),
[
animatedIndex,
Expand All @@ -1084,6 +1126,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
handleExpand,
handleCollapse,
handleClose,
handleForceClose,
]
);
//#endregion
Expand Down
11 changes: 9 additions & 2 deletions src/components/bottomSheetModal/BottomSheetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ const BottomSheetModalComponent = forwardRef<
}
bottomSheetRef.current?.close(...args);
}, []);
const handleForceClose = useCallback((...args) => {
if (minimized.current) {
return;
}
bottomSheetRef.current?.forceClose(...args);
}, []);
//#endregion

//#region bottom sheet modal methods
Expand Down Expand Up @@ -197,7 +203,7 @@ const BottomSheetModalComponent = forwardRef<
}
willUnmountSheet(key);
forcedDismissed.current = true;
bottomSheetRef.current?.close(animationConfigs, true);
bottomSheetRef.current?.forceClose(animationConfigs);
},
[willUnmountSheet, unmount, key, enablePanDownToClose]
);
Expand Down Expand Up @@ -325,8 +331,9 @@ const BottomSheetModalComponent = forwardRef<
expand: handleExpand,
collapse: handleCollapse,
close: handleClose,
dismiss: handleDismiss,
forceClose: handleForceClose,
// modal methods
dismiss: handleDismiss,
present: handlePresent,
// internal
minimize: handleMinimize,
Expand Down
13 changes: 11 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ export interface BottomSheetMethods {
* @see {Animated.WithTimingConfig}
*/
close: (
animationConfigs?: Animated.WithSpringConfig | Animated.WithTimingConfig,
force?: boolean
animationConfigs?: Animated.WithSpringConfig | Animated.WithTimingConfig
) => void;
/**
* Force close the bottom sheet, this prevent any interruptions till the sheet is closed.
* @param animationConfigs snap animation configs.
*
* @see {Animated.WithSpringConfig}
* @see {Animated.WithTimingConfig}
*/
forceClose: (
animationConfigs?: Animated.WithSpringConfig | Animated.WithTimingConfig
) => void;
}
export interface BottomSheetModalMethods extends BottomSheetMethods {