modal-system 0.1.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.js +81 -0
- package/dist/index.cjs +72 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +43 -0
- package/package.json +28 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
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:",
|
|
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
|
+
ui === "shadcn" ? getShadcnModal() : getDaisyModal()
|
|
24
|
+
);
|
|
25
|
+
fs.writeFileSync(
|
|
26
|
+
path.join(targetDir, "modals.ts"),
|
|
27
|
+
getModalsFile()
|
|
28
|
+
);
|
|
29
|
+
console.log("\u2705 modals folder created");
|
|
30
|
+
}
|
|
31
|
+
function getModalsFile() {
|
|
32
|
+
return `import { ConfirmModal } from "./confirm-modal";
|
|
33
|
+
|
|
34
|
+
export const MODALS = {
|
|
35
|
+
confirm: {
|
|
36
|
+
component: ConfirmModal,
|
|
37
|
+
},
|
|
38
|
+
} as const;
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
function getShadcnModal() {
|
|
42
|
+
return `export const ConfirmModal = ({ isOpen, onClose, data }) => {
|
|
43
|
+
if (!isOpen) return null;
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<div className="fixed inset-0 bg-black/50 flex items-center justify-center">
|
|
47
|
+
<div className="bg-white rounded-xl p-6">
|
|
48
|
+
<h2 className="text-lg font-bold">{data?.title}</h2>
|
|
49
|
+
|
|
50
|
+
<div className="flex gap-2 mt-4">
|
|
51
|
+
<button onClick={onClose}>Cancel</button>
|
|
52
|
+
<button onClick={data?.onConfirm}>Confirm</button>
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
`;
|
|
59
|
+
}
|
|
60
|
+
function getDaisyModal() {
|
|
61
|
+
return `export const ConfirmModal = ({ isOpen, onClose, data }) => {
|
|
62
|
+
if (!isOpen) return null;
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<div className="modal modal-open">
|
|
66
|
+
<div className="modal-box">
|
|
67
|
+
<h3 className="font-bold text-lg">{data?.title}</h3>
|
|
68
|
+
|
|
69
|
+
<div className="modal-action">
|
|
70
|
+
<button className="btn" onClick={onClose}>Cancel</button>
|
|
71
|
+
<button className="btn btn-primary" onClick={data?.onConfirm}>
|
|
72
|
+
Confirm
|
|
73
|
+
</button>
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
`;
|
|
80
|
+
}
|
|
81
|
+
init();
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
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 = (name, data) => {
|
|
39
|
+
setActiveModal({ name, data });
|
|
40
|
+
};
|
|
41
|
+
const closeModal = () => {
|
|
42
|
+
setActiveModal(null);
|
|
43
|
+
};
|
|
44
|
+
const CurrentModal = activeModal && modals[activeModal.name]?.component;
|
|
45
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ModalContext.Provider, { value: { openModal, closeModal }, children: [
|
|
46
|
+
children,
|
|
47
|
+
CurrentModal && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
48
|
+
CurrentModal,
|
|
49
|
+
{
|
|
50
|
+
isOpen: true,
|
|
51
|
+
data: activeModal.data,
|
|
52
|
+
onClose: closeModal
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
] });
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/hooks/use-modal.ts
|
|
59
|
+
var import_react2 = require("react");
|
|
60
|
+
var useModal = () => {
|
|
61
|
+
const ctx = (0, import_react2.useContext)(ModalContext);
|
|
62
|
+
if (!ctx) {
|
|
63
|
+
throw new Error("useModal must be used inside ModalProvider");
|
|
64
|
+
}
|
|
65
|
+
return ctx;
|
|
66
|
+
};
|
|
67
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
+
0 && (module.exports = {
|
|
69
|
+
ModalContext,
|
|
70
|
+
ModalProvider,
|
|
71
|
+
useModal
|
|
72
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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 ModalConfig = {
|
|
10
|
+
component: React.FC<any>;
|
|
11
|
+
};
|
|
12
|
+
type ModalsMap = Record<string, ModalConfig>;
|
|
13
|
+
|
|
14
|
+
declare const ModalContext: React.Context<{
|
|
15
|
+
openModal: (name: string, data?: any) => void;
|
|
16
|
+
closeModal: () => void;
|
|
17
|
+
} | null>;
|
|
18
|
+
declare const ModalProvider: ({ children, modals, }: {
|
|
19
|
+
children: React.ReactNode;
|
|
20
|
+
modals: ModalsMap;
|
|
21
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
22
|
+
|
|
23
|
+
declare const useModal: () => {
|
|
24
|
+
openModal: (name: string, data?: any) => void;
|
|
25
|
+
closeModal: () => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { type ModalComponentProps, type ModalConfig, ModalContext, ModalProvider, type ModalsMap, useModal };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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 ModalConfig = {
|
|
10
|
+
component: React.FC<any>;
|
|
11
|
+
};
|
|
12
|
+
type ModalsMap = Record<string, ModalConfig>;
|
|
13
|
+
|
|
14
|
+
declare const ModalContext: React.Context<{
|
|
15
|
+
openModal: (name: string, data?: any) => void;
|
|
16
|
+
closeModal: () => void;
|
|
17
|
+
} | null>;
|
|
18
|
+
declare const ModalProvider: ({ children, modals, }: {
|
|
19
|
+
children: React.ReactNode;
|
|
20
|
+
modals: ModalsMap;
|
|
21
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
22
|
+
|
|
23
|
+
declare const useModal: () => {
|
|
24
|
+
openModal: (name: string, data?: any) => void;
|
|
25
|
+
closeModal: () => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { type ModalComponentProps, type ModalConfig, ModalContext, ModalProvider, type ModalsMap, useModal };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/provider/ModalProvider.tsx
|
|
2
|
+
import { createContext, useState } 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 = (name, data) => {
|
|
11
|
+
setActiveModal({ name, data });
|
|
12
|
+
};
|
|
13
|
+
const closeModal = () => {
|
|
14
|
+
setActiveModal(null);
|
|
15
|
+
};
|
|
16
|
+
const CurrentModal = activeModal && modals[activeModal.name]?.component;
|
|
17
|
+
return /* @__PURE__ */ jsxs(ModalContext.Provider, { value: { openModal, closeModal }, children: [
|
|
18
|
+
children,
|
|
19
|
+
CurrentModal && /* @__PURE__ */ jsx(
|
|
20
|
+
CurrentModal,
|
|
21
|
+
{
|
|
22
|
+
isOpen: true,
|
|
23
|
+
data: activeModal.data,
|
|
24
|
+
onClose: closeModal
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
] });
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// src/hooks/use-modal.ts
|
|
31
|
+
import { useContext } from "react";
|
|
32
|
+
var useModal = () => {
|
|
33
|
+
const ctx = useContext(ModalContext);
|
|
34
|
+
if (!ctx) {
|
|
35
|
+
throw new Error("useModal must be used inside ModalProvider");
|
|
36
|
+
}
|
|
37
|
+
return ctx;
|
|
38
|
+
};
|
|
39
|
+
export {
|
|
40
|
+
ModalContext,
|
|
41
|
+
ModalProvider,
|
|
42
|
+
useModal
|
|
43
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "modal-system",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"modal-genius": "dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsup src/index.ts --format esm,cjs --dts && tsup bin/cli.ts --format esm --out-dir dist",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"react": "^18 || ^19"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/react": "^18.0.0",
|
|
22
|
+
"tsup": "^8.0.0",
|
|
23
|
+
"typescript": "^5.0.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"prompts": "^2.4.2"
|
|
27
|
+
}
|
|
28
|
+
}
|