@devvie/bottom-sheet
Version:
The 😎smart , 📦tiny , and 🎗flexible bottom sheet your app craves 🚀
38 lines (36 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("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 = (0, _react.useRef)();
(0, _react.useEffect)(() => {
handler.current = _reactNative.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]);
};
var _default = exports.default = useHandleAndroidBackButtonClose;
//# sourceMappingURL=index.js.map