modal-system 0.1.3 → 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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ import { init } from "./init";
3
+ const command = process.argv[2];
4
+ if (command === "init") {
5
+ init();
6
+ }
7
+ else {
8
+ console.log("Usage: modal-system init");
9
+ }
@@ -0,0 +1 @@
1
+ export declare function init(): Promise<void>;
@@ -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
- 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 };
1
+ export { createModalSystem } from "./core/createModalSystem";
package/dist/index.js CHANGED
@@ -1,42 +1 @@
1
- // src/provider/ModalProvider.tsx
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.1.3",
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
- "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
+ "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,92 +0,0 @@
1
- // bin/cli.ts
2
- import prompts from "prompts";
3
- import fs from "fs";
4
- import path from "path";
5
- async function init() {
6
- const { ui } = await prompts({
7
- type: "select",
8
- name: "ui",
9
- 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:",
10
- choices: [
11
- { title: "shadcn", value: "shadcn" },
12
- { title: "daisyUI", value: "daisy" }
13
- ]
14
- });
15
- const targetDir = path.join(process.cwd(), "modals");
16
- if (!fs.existsSync(targetDir)) {
17
- fs.mkdirSync(targetDir);
18
- }
19
- fs.writeFileSync(
20
- path.join(targetDir, "confirm-modal.tsx"),
21
- getModalTemplate(ui)
22
- );
23
- fs.writeFileSync(
24
- path.join(targetDir, "index.ts"),
25
- getModalsIndexFile()
26
- );
27
- console.log("");
28
- console.log("\u2705 \u041F\u0430\u043F\u043A\u0430 'modals' \u0443\u0441\u043F\u0435\u0448\u043D\u043E \u0441\u043E\u0437\u0434\u0430\u043D\u0430!");
29
- console.log("\u{1F680} \u0422\u0435\u043F\u0435\u0440\u044C:");
30
- 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)");
31
- 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)");
32
- }
33
- function getModalTemplate(ui) {
34
- const isShadcn = ui === "shadcn";
35
- const containerClass = isShadcn ? "fixed inset-0 bg-black/50 flex items-center justify-center z-50" : "modal modal-open";
36
- const boxClass = isShadcn ? "bg-white rounded-xl p-6 max-w-sm w-full shadow-lg" : "modal-box";
37
- const buttonClass = isShadcn ? "px-4 py-2 rounded border" : "btn";
38
- const primaryClass = isShadcn ? "px-4 py-2 rounded bg-blue-600 text-white" : "btn btn-primary";
39
- return `import { ModalComponentProps } from "modal-system";
40
-
41
- // \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
42
- export interface ConfirmData {
43
- title: string;
44
- onConfirm: () => void;
45
- }
46
-
47
- export const ConfirmModal = ({ isOpen, onClose, data }: ModalComponentProps<ConfirmData>) => {
48
- if (!isOpen) return null;
49
-
50
- return (
51
- <div className="${containerClass}">
52
- <div className="${boxClass}">
53
- <h3 className="font-bold text-lg">{data.title}</h3>
54
-
55
- <div className="flex justify-end gap-2 mt-6">
56
- <button className="${buttonClass}" onClick={onClose}>
57
- \u041E\u0442\u043C\u0435\u043D\u0430
58
- </button>
59
- <button
60
- className="${primaryClass}"
61
- onClick={() => {
62
- data.onConfirm();
63
- onClose();
64
- }}
65
- >
66
- \u041F\u043E\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u0442\u044C
67
- </button>
68
- </div>
69
- </div>
70
- </div>
71
- );
72
- };
73
- `;
74
- }
75
- function getModalsIndexFile() {
76
- return `import { ConfirmModal } from "./confirm-modal";
77
-
78
- // 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
79
- export const MODALS = {
80
- confirm: ConfirmModal,
81
- // \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
82
- } as const;
83
-
84
- // 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
85
- declare module "modal-system" {
86
- interface Register {
87
- modals: typeof MODALS;
88
- }
89
- }
90
- `;
91
- }
92
- 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 };