UNPKG

@worktif/purei

Version:

Work TIF Material UI Theme Provider and Customization Suite for React applications with dark mode support and dynamic color schemes

173 lines 7.76 kB
"use strict"; /* * Work TIF, Raman Marozau. * All rights reserved. * Copyright(c), 2025-present. * * Business Source License 1.1 * * Copyright (C) 2025 Raman Marozau, raman@worktif.com * Use of this software is governed by the Business Source License included in the LICENSE file and at www.mariadb.com/bsl11. * * Change Date: Never * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. * Additional Use Grant: Free for personal and non-commercial research use only. * * * SPDX-License-Identifier: BUSL-1.1 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.useDialog = useDialog; const React = __importStar(require("react")); const react_1 = require("react"); const material_1 = require("@mui/material"); const context_1 = require("../../context"); const components_1 = require("../../components"); /** * Hook that manages the state and behavior of dialog components in an application. * It provides functionality to open, close, toggle dialogs, and manage dialog messages and actions. * * @param {DialogMessageInstance} [messages] Optional messages mapping for the dialogs. * Each dialog type can be associated with a specific message or component. * * @return {UseDialogValue<T>} An object containing methods and state to manage dialogs, such as: * - `dialogContextType`: The type of the dialog currently in the context. * - `dialogType`: The type of the current dialog being managed by the hook. * - `dialogOpen`: A mapping of dialog types to their open state. * - `isDialogOpened`: A method to check if a specific dialog type is open. * - `openDialog`: A method to open a dialog with the specified type, title, and other properties. * - `closeDialog`: A method to close a specific dialog type or all dialogs. * - `toggleDialog`: A method to toggle the open state of a specific dialog. * - `deliverDialogMessage`: A method to deliver or set a specific dialog message. * - `deliverDialogActions`: A method to set actions for a specific dialog type. * - `actions`: The actions associated with the current dialog. * - `dialogMessage`: The message or component being rendered in the current dialog. * - `setDialogProps`: A method to set additional properties for the dialog. * - `dialogProps`: The current properties of the dialog. */ function useDialog(messages) { const context = (0, react_1.useContext)(context_1.DialogContext); if (!context) { throw new Error("useDialog must be used within a DialogProvider"); } const { open, type: dialogContextType, setDialogMessage, setActions, setDialogProps, dialogProps, updateOpen, actions, dialogMessage, setLoading, dialogComponent, setDialogComponent, setDialogTitle, } = context; const [dialogOpen, setDialogOpen] = (0, react_1.useState)({}); const [dialogType, setDialogType] = (0, react_1.useState)(void 0); const [dialogMessageList, deliverDialogMessageList] = (0, react_1.useState)(messages !== null && messages !== void 0 ? messages : {}); const [dialogActionList, deliverDialogActionList] = (0, react_1.useState)(void 0); const isDialogOpened = (type) => { return type && dialogOpen[type] !== void 0 ? dialogOpen[type] : false; }; (0, react_1.useEffect)(() => { if (dialogType && dialogOpen[dialogType]) { updateOpen(dialogOpen[dialogType], context_1.DialogType.Dialog); } }, [dialogOpen, dialogType]); const openDialog = (type, title, notificationProps, dialogMessageProps = {}, dialogActions = void 0) => { if (!dialogMessageList[type]) { console.error(`Dialog message ${String(type)} has no message.`); return; } if (notificationProps === null || notificationProps === void 0 ? void 0 : notificationProps.dialogComponent) { setDialogComponent(notificationProps === null || notificationProps === void 0 ? void 0 : notificationProps.dialogComponent); } else { setDialogComponent(void 0); } setDialogTitle(title); setDialogType(type); setActions(dialogActions || (dialogActionList ? dialogActionList[type] : void 0)); const DialogMessageComponent = dialogMessageList[type]; setDialogMessage(dialogMessageProps ? React.cloneElement(DialogMessageComponent, Object.assign({}, dialogMessageProps)) : React.cloneElement(DialogMessageComponent)); // if (dialogOpen) { setDialogOpen({ [type]: !dialogOpen[type] }); // } }; const closeDialog = (type) => { if (!type) { updateOpen(false, context_1.DialogType.Dialog); setDialogMessage(void 0); setActions(void 0); } else { setDialogType(type); setActions(void 0); setDialogMessage(React.createElement(material_1.Container, { maxWidth: "md", sx: { textAlign: "center", pl: { xs: 2 }, pr: { xs: 2 }, } }, React.createElement(material_1.Box, { sx: { mb: 4 } }, React.createElement(components_1.CustomLinearLoader, null)))); setDialogOpen({ [type]: false }); } }; const toggleDialog = (type) => { setDialogType(type); setDialogOpen((prevDialogOpen) => { if (!(prevDialogOpen[type] && dialogMessageList[type])) { console.error(`Dialog message ${String(type)} has no message.`); return prevDialogOpen; } return Object.assign(Object.assign({}, prevDialogOpen), { [type]: !prevDialogOpen[type] }); }); }; const deliverDialogMessage = (type, messageSection) => { deliverDialogMessageList({ [type]: messageSection }); }; const deliverDialogActions = (type, dialogActions) => { deliverDialogActionList({ [type]: dialogActions }); }; return { dialogContextType, dialogType, dialogOpen, isDialogOpened, openDialog, closeDialog, toggleDialog, deliverDialogMessage, deliverDialogActions, actions, dialogMessage, setDialogProps, dialogProps, }; } ; //# sourceMappingURL=index.js.map