-
-
Notifications
You must be signed in to change notification settings - Fork 733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bottom Sheet does not close on physical back button press #556
Comments
Hi @Ripul01 , i think this should be handling by you not the library. you could look for back button handler implementation on trigger the close method on the bottom sheet 👍 |
Thank you very much. It seems I was confused on a trivial issue. |
Hey Gorhom, I had another question. How will we check if the bottom sheet is open to handle that case and if not handle the normal case? |
Use |
const { dismiss } = useBottomSheetModal()
useEffect(() => {
const handleBackButton = () => {
return dismiss() // dismiss() returns true/false, it means there is any instance of Bottom Sheet visible on current screen.
}
BackHandler.addEventListener('hardwareBackPress', handleBackButton);
return () => {
BackHandler.removeEventListener('hardwareBackPress', handleBackButton);
};
}, []); |
That doesn't work for me with react-navigation. |
This is a working solution that plays well with react-navigation.
|
One more snippet of how to handle the hardware back button when your bottom sheet is in front of a user (i.e. visible): /**
* hook that dismisses the bottom sheet on the hardware back button press if it is visible
* @param bottomSheetRef ref to the bottom sheet which is going to be closed/dismissed on the back press
*/
export const useBottomSheetBackHandler = (bottomSheetRef: React.RefObject<BottomSheetModal | null>) => {
const backHandlerSubscriptionRef = useRef<NativeEventSubscription | null>(null)
const handleSheetPositionChange = useCallback<NonNullable<BottomSheetModalProps['onChange']>>((index) => {
const isBottomSheetVisible = index >= 0
if (isBottomSheetVisible && !backHandlerSubscriptionRef.current) {
// setup the back handler if the bottom sheet is right in front of the user
backHandlerSubscriptionRef.current = BackHandler.addEventListener('hardwareBackPress', () => {
bottomSheetRef.current?.dismiss()
return true
})
} else if (!isBottomSheetVisible) {
backHandlerSubscriptionRef.current?.remove()
backHandlerSubscriptionRef.current = null
}
}, [bottomSheetRef, backHandlerSubscriptionRef])
return { handleSheetPositionChange }
} Then somewhere in your code pass the returned handler to ...
const { handleSheetPositionChange } = useBottomSheetBackHandler(bottomSheetRef)
...
return (
<BottomSheetModal
ref={bottomSheetRef}
onChange={handleSheetPositionChange}>
</BottomSheetModal>
) |
@Hesowcharov thank you so much. It's work!!!!!! |
@gorhom is it possible to add this functionality to the modal base code, and control it by props? like: <BottomSheetModal
ref={bottomSheetRef}
...
dismissOnBackAndroid={false}>
</BottomSheetModal> |
awesome solution @Hesowcharov ; For example I have a :
So there is no ref associated to it. BUT, randomly, the back button seems to be broken when using it and once the bottomsheet is closed; |
...
const ref = useRef<BottomSheet>(null);
const [currentIndex, setCurrentIndex] = useState<number>(-1);
...
const onBackPress = () => {
if (ref !== null) {
ref.current?.close();
return true;
}
};
useEffect(() => {
if (currentIndex !== -1) {
BackHandler.addEventListener('hardwareBackPress', onBackPress);
return () => BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}
}, [currentIndex]);
return (
<BottomSheet
ref={ref}
onChange={(index) => {
setCurrentIndex(index);
}
...
>
</BottomSheet>
) |
Ask your Question
Hello, The bottom sheet does not seem to close when the physical back button is pressed in android. I have tried it on the issue template too and the same issue appears. Upon pressing the back button, the window behind the bottom sheet pops and the app quits on the starting screen. Should this happen and does anyone know of a fix for this issue?
The text was updated successfully, but these errors were encountered: