modal-system 0.1.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +9 -0
- package/dist/cli/init.d.ts +1 -0
- package/dist/cli/init.js +52 -0
- package/dist/core/createModalSystem.d.ts +24 -0
- package/dist/core/createModalSystem.js +36 -0
- package/dist/index.d.ts +1 -29
- package/dist/index.js +1 -42
- package/dist/templates/daisy.d.ts +1 -0
- package/dist/templates/daisy.js +23 -0
- package/dist/templates/shadcn.d.ts +1 -0
- package/dist/templates/shadcn.js +21 -0
- package/package.json +12 -12
- package/dist/cli.js +0 -94
- package/dist/index.cjs +0 -71
- package/dist/index.d.cts +0 -29
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function init(): Promise<void>;
|
package/dist/cli/init.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import inquirer from "inquirer";
|
|
4
|
+
import { shadcnTemplate } from "../templates/shadcn";
|
|
5
|
+
import { daisyTemplate } from "../templates/daisy";
|
|
6
|
+
export async function init() {
|
|
7
|
+
const { ui } = await inquirer.prompt([
|
|
8
|
+
{
|
|
9
|
+
type: "list",
|
|
10
|
+
name: "ui",
|
|
11
|
+
message: "Choose UI library:",
|
|
12
|
+
choices: ["shadcn", "daisy"],
|
|
13
|
+
},
|
|
14
|
+
]);
|
|
15
|
+
const baseDir = path.resolve(process.cwd(), "modals");
|
|
16
|
+
if (!fs.existsSync(baseDir)) {
|
|
17
|
+
fs.mkdirSync(baseDir);
|
|
18
|
+
}
|
|
19
|
+
// modals.ts
|
|
20
|
+
fs.writeFileSync(path.join(baseDir, "modals.ts"), `import { ExampleModal } from "./ExampleModal";
|
|
21
|
+
|
|
22
|
+
export const MODALS = [
|
|
23
|
+
{
|
|
24
|
+
id: "example",
|
|
25
|
+
component: ExampleModal,
|
|
26
|
+
},
|
|
27
|
+
] as const;
|
|
28
|
+
`);
|
|
29
|
+
// types.ts
|
|
30
|
+
fs.writeFileSync(path.join(baseDir, "types.ts"), `import { ReactNode } from "react";
|
|
31
|
+
|
|
32
|
+
export enum ModalType {
|
|
33
|
+
EXAMPLE = "example",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type ModalDataMap = {
|
|
37
|
+
[ModalType.EXAMPLE]: {
|
|
38
|
+
title?: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type ModalProps<T extends ModalType = ModalType> = {
|
|
43
|
+
id: T;
|
|
44
|
+
data?: ModalDataMap[T];
|
|
45
|
+
isOpened: boolean;
|
|
46
|
+
onClose: () => void;
|
|
47
|
+
};
|
|
48
|
+
`);
|
|
49
|
+
// modal example
|
|
50
|
+
fs.writeFileSync(path.join(baseDir, "ExampleModal.tsx"), ui === "shadcn" ? shadcnTemplate : daisyTemplate);
|
|
51
|
+
console.log("✅ Modal system initialized!");
|
|
52
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React, { ReactNode } from "react";
|
|
2
|
+
export type ModalDefinition<T extends string, D> = {
|
|
3
|
+
id: T;
|
|
4
|
+
component: React.FC<any>;
|
|
5
|
+
};
|
|
6
|
+
export declare function createModalSystem<const TModals extends readonly ModalDefinition<string, any>[]>(modals: TModals): {
|
|
7
|
+
ModalProvider: ({ children }: {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
useModal: () => {
|
|
11
|
+
isOpened: boolean;
|
|
12
|
+
activeModal?: keyof { [K in TModals[number] as K["id"]]: Extract<TModals[number], {
|
|
13
|
+
id: K["id"];
|
|
14
|
+
}> extends ModalDefinition<any, infer D> ? D : never; };
|
|
15
|
+
data?: any;
|
|
16
|
+
} & {
|
|
17
|
+
openModal: <K_1 extends keyof { [K in TModals[number] as K["id"]]: Extract<TModals[number], {
|
|
18
|
+
id: K["id"];
|
|
19
|
+
}> extends ModalDefinition<any, infer D> ? D : never; }>(id: K_1, data?: { [K in TModals[number] as K["id"]]: Extract<TModals[number], {
|
|
20
|
+
id: K["id"];
|
|
21
|
+
}> extends ModalDefinition<any, infer D> ? D : never; }[K_1]) => void;
|
|
22
|
+
closeModal: () => void;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useState } from "react";
|
|
3
|
+
export function createModalSystem(modals) {
|
|
4
|
+
const ModalContext = createContext(null);
|
|
5
|
+
const ModalProvider = ({ children }) => {
|
|
6
|
+
const [state, setState] = useState({
|
|
7
|
+
isOpened: false,
|
|
8
|
+
});
|
|
9
|
+
const openModal = (id, data) => {
|
|
10
|
+
setState({
|
|
11
|
+
isOpened: true,
|
|
12
|
+
activeModal: id,
|
|
13
|
+
data,
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
const closeModal = () => {
|
|
17
|
+
setState({
|
|
18
|
+
isOpened: false,
|
|
19
|
+
activeModal: undefined,
|
|
20
|
+
data: undefined,
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
return (_jsxs(ModalContext.Provider, { value: { ...state, openModal, closeModal }, children: [children, modals.map((modal) => {
|
|
24
|
+
const Component = modal.component;
|
|
25
|
+
return (_jsx(Component, { isOpened: state.activeModal === modal.id, data: state.data, onClose: closeModal }, modal.id));
|
|
26
|
+
})] }));
|
|
27
|
+
};
|
|
28
|
+
const useModal = () => {
|
|
29
|
+
const ctx = useContext(ModalContext);
|
|
30
|
+
if (!ctx) {
|
|
31
|
+
throw new Error("useModal must be used inside ModalProvider");
|
|
32
|
+
}
|
|
33
|
+
return ctx;
|
|
34
|
+
};
|
|
35
|
+
return { ModalProvider, useModal };
|
|
36
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,29 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import React from 'react';
|
|
3
|
-
|
|
4
|
-
type ModalComponentProps<T = any> = {
|
|
5
|
-
isOpen: boolean;
|
|
6
|
-
onClose: () => void;
|
|
7
|
-
data: T;
|
|
8
|
-
};
|
|
9
|
-
type ModalsRegistry = Record<string, React.ComponentType<ModalComponentProps<any>>>;
|
|
10
|
-
type InferModalData<T> = T extends React.ComponentType<ModalComponentProps<infer D>> ? D : never;
|
|
11
|
-
interface Register {
|
|
12
|
-
}
|
|
13
|
-
type AnyModalsRegistry = Register extends {
|
|
14
|
-
modals: infer M;
|
|
15
|
-
} ? M extends ModalsRegistry ? M : ModalsRegistry : ModalsRegistry;
|
|
16
|
-
|
|
17
|
-
interface ModalContextType<T extends ModalsRegistry = AnyModalsRegistry> {
|
|
18
|
-
openModal: <K extends keyof T>(name: K, ...args: InferModalData<T[K]> extends undefined ? [data?: undefined] : [data: InferModalData<T[K]>]) => void;
|
|
19
|
-
closeModal: () => void;
|
|
20
|
-
}
|
|
21
|
-
declare const ModalContext: React.Context<ModalContextType<any> | null>;
|
|
22
|
-
declare const ModalProvider: <T extends ModalsRegistry = AnyModalsRegistry>({ children, modals, }: {
|
|
23
|
-
children: React.ReactNode;
|
|
24
|
-
modals: T;
|
|
25
|
-
}) => react_jsx_runtime.JSX.Element;
|
|
26
|
-
|
|
27
|
-
declare const useModal: () => ModalContextType<ModalsRegistry>;
|
|
28
|
-
|
|
29
|
-
export { type AnyModalsRegistry, type InferModalData, type ModalComponentProps, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, type Register, useModal };
|
|
1
|
+
export { createModalSystem } from "./core/createModalSystem";
|
package/dist/index.js
CHANGED
|
@@ -1,42 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { createContext, useState, useCallback, useMemo } from "react";
|
|
3
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
-
var ModalContext = createContext(null);
|
|
5
|
-
var ModalProvider = ({
|
|
6
|
-
children,
|
|
7
|
-
modals
|
|
8
|
-
}) => {
|
|
9
|
-
const [activeModal, setActiveModal] = useState(null);
|
|
10
|
-
const openModal = useCallback((name, data) => {
|
|
11
|
-
setActiveModal({ name, data });
|
|
12
|
-
}, []);
|
|
13
|
-
const closeModal = useCallback(() => {
|
|
14
|
-
setActiveModal(null);
|
|
15
|
-
}, []);
|
|
16
|
-
const value = useMemo(() => ({ openModal, closeModal }), [openModal, closeModal]);
|
|
17
|
-
const Component = activeModal ? modals[activeModal.name] : null;
|
|
18
|
-
return /* @__PURE__ */ jsxs(ModalContext.Provider, { value, children: [
|
|
19
|
-
children,
|
|
20
|
-
Component && /* @__PURE__ */ jsx(
|
|
21
|
-
Component,
|
|
22
|
-
{
|
|
23
|
-
isOpen: true,
|
|
24
|
-
onClose: closeModal,
|
|
25
|
-
data: activeModal?.data
|
|
26
|
-
}
|
|
27
|
-
)
|
|
28
|
-
] });
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
// src/hooks/use-modal.ts
|
|
32
|
-
import { useContext } from "react";
|
|
33
|
-
var useModal = () => {
|
|
34
|
-
const ctx = useContext(ModalContext);
|
|
35
|
-
if (!ctx) throw new Error("useModal must be used inside ModalProvider");
|
|
36
|
-
return ctx;
|
|
37
|
-
};
|
|
38
|
-
export {
|
|
39
|
-
ModalContext,
|
|
40
|
-
ModalProvider,
|
|
41
|
-
useModal
|
|
42
|
-
};
|
|
1
|
+
export { createModalSystem } from "./core/createModalSystem";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const daisyTemplate = "\nimport { ModalProps } from \"./types\";\n\nexport const ExampleModal = ({\n isOpened,\n onClose,\n data\n}: ModalProps) => {\n return (\n <dialog className={\"modal \" + (isOpened ? \"modal-open\" : \"\")}>\n <div className=\"modal-box\">\n <h3>{data?.title}</h3>\n\n <div className=\"modal-action\">\n <button className=\"btn\" onClick={onClose}>\n Close\n </button>\n </div>\n </div>\n </dialog>\n );\n};\n";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const daisyTemplate = `
|
|
2
|
+
import { ModalProps } from "./types";
|
|
3
|
+
|
|
4
|
+
export const ExampleModal = ({
|
|
5
|
+
isOpened,
|
|
6
|
+
onClose,
|
|
7
|
+
data
|
|
8
|
+
}: ModalProps) => {
|
|
9
|
+
return (
|
|
10
|
+
<dialog className={"modal " + (isOpened ? "modal-open" : "")}>
|
|
11
|
+
<div className="modal-box">
|
|
12
|
+
<h3>{data?.title}</h3>
|
|
13
|
+
|
|
14
|
+
<div className="modal-action">
|
|
15
|
+
<button className="btn" onClick={onClose}>
|
|
16
|
+
Close
|
|
17
|
+
</button>
|
|
18
|
+
</div>
|
|
19
|
+
</div>
|
|
20
|
+
</dialog>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const shadcnTemplate = "\nimport { ModalProps } from \"./types\";\n\nexport const ExampleModal = ({\n isOpened,\n onClose,\n data\n}: ModalProps) => {\n if (!isOpened) return null;\n\n return (\n <div className=\"fixed inset-0 flex items-center justify-center bg-black/50\">\n <div className=\"bg-white p-6 rounded-xl\">\n <h2>{data?.title}</h2>\n\n <button onClick={onClose}>Close</button>\n </div>\n </div>\n );\n};\n";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const shadcnTemplate = `
|
|
2
|
+
import { ModalProps } from "./types";
|
|
3
|
+
|
|
4
|
+
export const ExampleModal = ({
|
|
5
|
+
isOpened,
|
|
6
|
+
onClose,
|
|
7
|
+
data
|
|
8
|
+
}: ModalProps) => {
|
|
9
|
+
if (!isOpened) return null;
|
|
10
|
+
|
|
11
|
+
return (
|
|
12
|
+
<div className="fixed inset-0 flex items-center justify-center bg-black/50">
|
|
13
|
+
<div className="bg-white p-6 rounded-xl">
|
|
14
|
+
<h2>{data?.title}</h2>
|
|
15
|
+
|
|
16
|
+
<button onClick={onClose}>Close</button>
|
|
17
|
+
</div>
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
`;
|
package/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "modal-system",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Flexible modal system with CLI and type-safe API",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
6
8
|
"types": "dist/index.d.ts",
|
|
7
9
|
"bin": {
|
|
8
|
-
"modal-system": "dist/cli.js"
|
|
10
|
+
"modal-system": "dist/cli/index.js"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"dev": "tsc -w"
|
|
9
15
|
},
|
|
10
16
|
"files": [
|
|
11
17
|
"dist"
|
|
12
18
|
],
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
},
|
|
17
|
-
"peerDependencies": {
|
|
18
|
-
"react": "^18 || ^19"
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"inquirer": "^13.3.2",
|
|
21
|
+
"react": "^18.0.0"
|
|
19
22
|
},
|
|
20
23
|
"devDependencies": {
|
|
24
|
+
"@types/node": "^20.0.0",
|
|
21
25
|
"@types/react": "^18.0.0",
|
|
22
|
-
"tsup": "^8.0.0",
|
|
23
26
|
"typescript": "^5.0.0"
|
|
24
|
-
},
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"prompts": "^2.4.2"
|
|
27
27
|
}
|
|
28
28
|
}
|
package/dist/cli.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// bin/cli.ts
|
|
4
|
-
import prompts from "prompts";
|
|
5
|
-
import fs from "fs";
|
|
6
|
-
import path from "path";
|
|
7
|
-
async function init() {
|
|
8
|
-
const { ui } = await prompts({
|
|
9
|
-
type: "select",
|
|
10
|
-
name: "ui",
|
|
11
|
-
message: "\u0412\u044B\u0431\u0435\u0440\u0438 UI \u0431\u0438\u0431\u043B\u0438\u043E\u0442\u0435\u043A\u0443 \u0434\u043B\u044F \u0433\u0435\u043D\u0435\u0440\u0430\u0446\u0438\u0438 \u043C\u043E\u0434\u0430\u043B\u043E\u043A:",
|
|
12
|
-
choices: [
|
|
13
|
-
{ title: "shadcn", value: "shadcn" },
|
|
14
|
-
{ title: "daisyUI", value: "daisy" }
|
|
15
|
-
]
|
|
16
|
-
});
|
|
17
|
-
const targetDir = path.join(process.cwd(), "modals");
|
|
18
|
-
if (!fs.existsSync(targetDir)) {
|
|
19
|
-
fs.mkdirSync(targetDir);
|
|
20
|
-
}
|
|
21
|
-
fs.writeFileSync(
|
|
22
|
-
path.join(targetDir, "confirm-modal.tsx"),
|
|
23
|
-
getModalTemplate(ui)
|
|
24
|
-
);
|
|
25
|
-
fs.writeFileSync(
|
|
26
|
-
path.join(targetDir, "index.ts"),
|
|
27
|
-
getModalsIndexFile()
|
|
28
|
-
);
|
|
29
|
-
console.log("");
|
|
30
|
-
console.log("\u2705 \u041F\u0430\u043F\u043A\u0430 'modals' \u0443\u0441\u043F\u0435\u0448\u043D\u043E \u0441\u043E\u0437\u0434\u0430\u043D\u0430!");
|
|
31
|
-
console.log("\u{1F680} \u0422\u0435\u043F\u0435\u0440\u044C:");
|
|
32
|
-
console.log("1. \u041E\u0431\u0435\u0440\u043D\u0438 \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \u0432 <ModalProvider modals={MODALS}> (\u0438\u043C\u043F\u043E\u0440\u0442 \u0438\u0437 ./modals)");
|
|
33
|
-
console.log("2. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439 const { openModal } = useModal() (\u0438\u043C\u043F\u043E\u0440\u0442 \u0438\u0437 modal-system)");
|
|
34
|
-
}
|
|
35
|
-
function getModalTemplate(ui) {
|
|
36
|
-
const isShadcn = ui === "shadcn";
|
|
37
|
-
const containerClass = isShadcn ? "fixed inset-0 bg-black/50 flex items-center justify-center z-50" : "modal modal-open";
|
|
38
|
-
const boxClass = isShadcn ? "bg-white rounded-xl p-6 max-w-sm w-full shadow-lg" : "modal-box";
|
|
39
|
-
const buttonClass = isShadcn ? "px-4 py-2 rounded border" : "btn";
|
|
40
|
-
const primaryClass = isShadcn ? "px-4 py-2 rounded bg-blue-600 text-white" : "btn btn-primary";
|
|
41
|
-
return `import { ModalComponentProps } from "modal-system";
|
|
42
|
-
|
|
43
|
-
// \u041E\u043F\u0438\u0441\u044B\u0432\u0430\u0435\u043C \u0442\u0438\u043F\u044B \u0434\u0430\u043D\u043D\u044B\u0445, \u043A\u043E\u0442\u043E\u0440\u044B\u0435 \u043D\u0443\u0436\u043D\u044B \u044D\u0442\u043E\u0439 \u043C\u043E\u0434\u0430\u043B\u043A\u0435
|
|
44
|
-
export interface ConfirmData {
|
|
45
|
-
title: string;
|
|
46
|
-
onConfirm: () => void;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const ConfirmModal = ({ isOpen, onClose, data }: ModalComponentProps<ConfirmData>) => {
|
|
50
|
-
if (!isOpen) return null;
|
|
51
|
-
|
|
52
|
-
return (
|
|
53
|
-
<div className="${containerClass}">
|
|
54
|
-
<div className="${boxClass}">
|
|
55
|
-
<h3 className="font-bold text-lg">{data.title}</h3>
|
|
56
|
-
|
|
57
|
-
<div className="flex justify-end gap-2 mt-6">
|
|
58
|
-
<button className="${buttonClass}" onClick={onClose}>
|
|
59
|
-
\u041E\u0442\u043C\u0435\u043D\u0430
|
|
60
|
-
</button>
|
|
61
|
-
<button
|
|
62
|
-
className="${primaryClass}"
|
|
63
|
-
onClick={() => {
|
|
64
|
-
data.onConfirm();
|
|
65
|
-
onClose();
|
|
66
|
-
}}
|
|
67
|
-
>
|
|
68
|
-
\u041F\u043E\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u0442\u044C
|
|
69
|
-
</button>
|
|
70
|
-
</div>
|
|
71
|
-
</div>
|
|
72
|
-
</div>
|
|
73
|
-
);
|
|
74
|
-
};
|
|
75
|
-
`;
|
|
76
|
-
}
|
|
77
|
-
function getModalsIndexFile() {
|
|
78
|
-
return `import { ConfirmModal } from "./confirm-modal";
|
|
79
|
-
|
|
80
|
-
// 1. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043C \u0432\u0441\u0435 \u043C\u043E\u0434\u0430\u043B\u043A\u0438 \u0437\u0434\u0435\u0441\u044C
|
|
81
|
-
export const MODALS = {
|
|
82
|
-
confirm: ConfirmModal,
|
|
83
|
-
// \u0421\u044E\u0434\u0430 \u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u044C \u0431\u0443\u0434\u0435\u0442 \u0434\u043E\u0431\u0430\u0432\u043B\u044F\u0442\u044C \u043D\u043E\u0432\u044B\u0435 \u043C\u043E\u0434\u0430\u043B\u043A\u0438
|
|
84
|
-
} as const;
|
|
85
|
-
|
|
86
|
-
// 2. \u0414\u043E\u0431\u0430\u0432\u043B\u044F\u0435\u043C \u0433\u043B\u043E\u0431\u0430\u043B\u044C\u043D\u0443\u044E \u0442\u0438\u043F\u0438\u0437\u0430\u0446\u0438\u044E \u0434\u043B\u044F \u0430\u0432\u0442\u043E\u043A\u043E\u043C\u043F\u043B\u0438\u0442\u0430
|
|
87
|
-
declare module "modal-system" {
|
|
88
|
-
interface Register {
|
|
89
|
-
modals: typeof MODALS;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
`;
|
|
93
|
-
}
|
|
94
|
-
init();
|
package/dist/index.cjs
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
ModalContext: () => ModalContext,
|
|
24
|
-
ModalProvider: () => ModalProvider,
|
|
25
|
-
useModal: () => useModal
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(index_exports);
|
|
28
|
-
|
|
29
|
-
// src/provider/ModalProvider.tsx
|
|
30
|
-
var import_react = require("react");
|
|
31
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
32
|
-
var ModalContext = (0, import_react.createContext)(null);
|
|
33
|
-
var ModalProvider = ({
|
|
34
|
-
children,
|
|
35
|
-
modals
|
|
36
|
-
}) => {
|
|
37
|
-
const [activeModal, setActiveModal] = (0, import_react.useState)(null);
|
|
38
|
-
const openModal = (0, import_react.useCallback)((name, data) => {
|
|
39
|
-
setActiveModal({ name, data });
|
|
40
|
-
}, []);
|
|
41
|
-
const closeModal = (0, import_react.useCallback)(() => {
|
|
42
|
-
setActiveModal(null);
|
|
43
|
-
}, []);
|
|
44
|
-
const value = (0, import_react.useMemo)(() => ({ openModal, closeModal }), [openModal, closeModal]);
|
|
45
|
-
const Component = activeModal ? modals[activeModal.name] : null;
|
|
46
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ModalContext.Provider, { value, children: [
|
|
47
|
-
children,
|
|
48
|
-
Component && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
49
|
-
Component,
|
|
50
|
-
{
|
|
51
|
-
isOpen: true,
|
|
52
|
-
onClose: closeModal,
|
|
53
|
-
data: activeModal?.data
|
|
54
|
-
}
|
|
55
|
-
)
|
|
56
|
-
] });
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// src/hooks/use-modal.ts
|
|
60
|
-
var import_react2 = require("react");
|
|
61
|
-
var useModal = () => {
|
|
62
|
-
const ctx = (0, import_react2.useContext)(ModalContext);
|
|
63
|
-
if (!ctx) throw new Error("useModal must be used inside ModalProvider");
|
|
64
|
-
return ctx;
|
|
65
|
-
};
|
|
66
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
67
|
-
0 && (module.exports = {
|
|
68
|
-
ModalContext,
|
|
69
|
-
ModalProvider,
|
|
70
|
-
useModal
|
|
71
|
-
});
|
package/dist/index.d.cts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
|
|
4
|
-
type ModalComponentProps<T = any> = {
|
|
5
|
-
isOpen: boolean;
|
|
6
|
-
onClose: () => void;
|
|
7
|
-
data: T;
|
|
8
|
-
};
|
|
9
|
-
type ModalsRegistry = Record<string, React.ComponentType<ModalComponentProps<any>>>;
|
|
10
|
-
type InferModalData<T> = T extends React.ComponentType<ModalComponentProps<infer D>> ? D : never;
|
|
11
|
-
interface Register {
|
|
12
|
-
}
|
|
13
|
-
type AnyModalsRegistry = Register extends {
|
|
14
|
-
modals: infer M;
|
|
15
|
-
} ? M extends ModalsRegistry ? M : ModalsRegistry : ModalsRegistry;
|
|
16
|
-
|
|
17
|
-
interface ModalContextType<T extends ModalsRegistry = AnyModalsRegistry> {
|
|
18
|
-
openModal: <K extends keyof T>(name: K, ...args: InferModalData<T[K]> extends undefined ? [data?: undefined] : [data: InferModalData<T[K]>]) => void;
|
|
19
|
-
closeModal: () => void;
|
|
20
|
-
}
|
|
21
|
-
declare const ModalContext: React.Context<ModalContextType<any> | null>;
|
|
22
|
-
declare const ModalProvider: <T extends ModalsRegistry = AnyModalsRegistry>({ children, modals, }: {
|
|
23
|
-
children: React.ReactNode;
|
|
24
|
-
modals: T;
|
|
25
|
-
}) => react_jsx_runtime.JSX.Element;
|
|
26
|
-
|
|
27
|
-
declare const useModal: () => ModalContextType<ModalsRegistry>;
|
|
28
|
-
|
|
29
|
-
export { type AnyModalsRegistry, type InferModalData, type ModalComponentProps, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, type Register, useModal };
|