@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
51 lines • 2.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useModalContext = exports.ModalContext = void 0;
const react_1 = __importDefault(require("react"));
/** Can be provided in the application to react to modal related changes. */
exports.ModalContext = react_1.default.createContext({
setModalOpen: () => { },
openModalStack: () => [],
});
/** Default implementation for modal context props.
* Tracks open modals in a stack representation.
**/
const useModalContext = () => {
// A stack of modal IDs. These should reflect a stacked opening of modals on top of each other.
const currentOpenModalStack = react_1.default.useRef([]);
const setOpenModalStack = ((stackUpdateFunction) => {
currentOpenModalStack.current = stackUpdateFunction([...currentOpenModalStack.current]);
});
const setModalOpen = react_1.default.useCallback((modalId, isOpen) => {
setOpenModalStack(old => {
if (isOpen) {
return [...old, modalId];
}
else {
const idx = old.findIndex((id) => modalId === id);
switch (idx) {
case -1:
// Trying to close modal that has not been registered as open!
return old;
case old.length - 1:
return old.slice(0, idx);
default:
// Modal in between is closed. Consider all modals after it also as closed.
return old.slice(0, idx);
}
}
});
}, []);
const openModalStack = react_1.default.useCallback(() => {
return currentOpenModalStack.current.length ? [...currentOpenModalStack.current] : undefined;
}, []);
return {
openModalStack,
setModalOpen,
};
};
exports.useModalContext = useModalContext;
//# sourceMappingURL=ModalContext.js.map