react-native-bottom-sheets
Version:
A tiny wrapper around @gorhom/bottom-sheet to make it easier to use in react-native projects.
53 lines (52 loc) • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var BottomSheetManager_1 = require("../utils/BottomSheetManager");
/**
* Custom hook for managing bottom sheet state and operations.
* @returns An object containing bottom sheet state and control functions.
*/
var useBottomSheets = function () {
var _a = (0, react_1.useState)(null), name = _a[0], setName = _a[1];
var _b = (0, react_1.useState)(false), open = _b[0], setOpen = _b[1];
var _c = (0, react_1.useState)(null), params = _c[0], setParams = _c[1];
/**
* Opens a bottom sheet.
* @param name - The name of the bottom sheet to open.
* @param params - Parameters to pass to the bottom sheet.
*/
var openBottomSheet = function (name, params, callback) {
if (params === void 0) { params = null; }
BottomSheetManager_1.default.open(name, params, callback);
};
/**
* Closes a bottom sheet.
* @param callback - Optional callback to be called after closing the bottom sheet.
*/
var closeBottomSheet = function (callback) {
BottomSheetManager_1.default.close(callback);
};
(0, react_1.useEffect)(function () {
var handleOpen = function (name, params, callback) {
if (params === void 0) { params = {}; }
setOpen(true);
setName(name);
setParams(params);
callback === null || callback === void 0 ? void 0 : callback();
};
var handleClose = function (callback) {
setName(null);
setOpen(false);
setParams(null);
callback === null || callback === void 0 ? void 0 : callback();
};
BottomSheetManager_1.default.on("open", handleOpen);
BottomSheetManager_1.default.on("close", handleClose);
return function () {
BottomSheetManager_1.default.off("open", handleOpen);
BottomSheetManager_1.default.off("close", handleClose);
};
}, []);
return { name: name, open: open, params: params, openBottomSheet: openBottomSheet, closeBottomSheet: closeBottomSheet };
};
exports.default = useBottomSheets;