@devvie/bottom-sheet
Version:
The 😎smart , 📦tiny , and 🎗flexible bottom sheet your app craves 🚀
32 lines (31 loc) • 1.27 kB
JavaScript
import { useEffect, useRef } from 'react';
import { BackHandler } from 'react-native';
/**
* Handles closing sheet when back button is pressed on
* android and sheet is opened
*
* @param {boolean} shouldClose Whether to close sheet when back button is pressed
* @param {boolean} closeSheet Function to call to close the sheet
* @param {boolean} sheetOpen Determines the visibility of the sheet
*/
const useHandleAndroidBackButtonClose = function () {
let shouldClose = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
let closeSheet = arguments.length > 1 ? arguments[1] : undefined;
let sheetOpen = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
const handler = useRef();
useEffect(() => {
handler.current = BackHandler.addEventListener('hardwareBackPress', () => {
if (sheetOpen) {
if (shouldClose) {
closeSheet?.();
}
return true; // prevent back press event bubbling as long as sheet is open
} else return false; // when sheet is closed allow bubbling
});
return () => {
handler.current?.remove?.();
};
}, [shouldClose, closeSheet, sheetOpen]);
};
export default useHandleAndroidBackButtonClose;
//# sourceMappingURL=index.js.map