react-simpler-modal
Version:
a lightweight, hook-based modal management library for React. It leverages React Context and custom hooks to offer an intuitive, type-safe API for handling multiple modals—making it easy to show, hide, update, and persist modal state across your app.
114 lines (111 loc) • 4.41 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.tsx
var index_exports = {};
__export(index_exports, {
simplerModalFactory: () => simplerModalFactory
});
module.exports = __toCommonJS(index_exports);
// src/lib/simpler-modal.tsx
var import_react = require("react");
var import_jsx_runtime = require("react/jsx-runtime");
var modalReducer = (state, action) => {
switch (action.type) {
case "SHOW_MODAL": {
const { mergeData } = action.options || {};
const currentData = state[action.modalType]?.data;
const newData = mergeData && typeof currentData === "object" && typeof action.data === "object" ? { ...currentData, ...action.data } : action.data;
return {
...state,
[action.modalType]: { isShowing: true, data: newData }
};
}
case "HIDE_MODAL": {
const { persistData } = action.options || {};
return {
...state,
[action.modalType]: {
isShowing: false,
data: persistData ? state[action.modalType]?.data : void 0
}
};
}
case "TOGGLE_MODAL": {
const isCurrentlyShowing = state[action.modalType]?.isShowing || false;
const { mergeData } = action.options || {};
const currentData = state[action.modalType]?.data;
const newData = !isCurrentlyShowing && mergeData && typeof currentData === "object" && typeof action.data === "object" ? { ...currentData, ...action.data } : !isCurrentlyShowing ? action.data : void 0;
return {
...state,
[action.modalType]: {
isShowing: !isCurrentlyShowing,
data: newData
}
};
}
case "UPDATE_MODAL_DATA": {
const { mergeData } = action.options || {};
const currentData = state[action.modalType]?.data;
const newData = mergeData && typeof currentData === "object" && typeof action.data === "object" ? { ...currentData, ...action.data } : action.data;
return {
...state,
[action.modalType]: {
isShowing: state[action.modalType]?.isShowing || false,
data: newData
}
};
}
default:
return state;
}
};
function simplerModalFactory(initialState) {
const ModalContext = (0, import_react.createContext)(null);
const ModalProvider = ({ children }) => {
const [state, dispatch] = (0, import_react.useReducer)(modalReducer, initialState);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ModalContext.Provider, { value: { state, dispatch }, children });
};
const useModal = () => {
const context = (0, import_react.useContext)(ModalContext);
if (!context) {
throw new Error("useModal must be used within its corresponding ModalProvider");
}
const { state, dispatch } = context;
const showModal = (modalType, data, options) => {
dispatch({ type: "SHOW_MODAL", modalType, data, options });
};
const hideModal = (modalType, options) => {
dispatch({ type: "HIDE_MODAL", modalType, options });
};
const toggleModal = (modalType, data, options) => {
dispatch({ type: "TOGGLE_MODAL", modalType, data, options });
};
const updateModalData = (modalType, data, options) => {
dispatch({ type: "UPDATE_MODAL_DATA", modalType, data, options });
};
const isModalOpen = (modalType) => !!state[modalType]?.isShowing;
const getModalData = (modalType) => state[modalType]?.data;
return { showModal, hideModal, toggleModal, updateModalData, isModalOpen, getModalData };
};
return { ModalProvider, useModal };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
simplerModalFactory
});