modal-system 0.1.2 → 0.1.4

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 CHANGED
@@ -1,3 +1,5 @@
1
+ #!/usr/bin/env node
2
+
1
3
  // bin/cli.ts
2
4
  import prompts from "prompts";
3
5
  import fs from "fs";
@@ -28,7 +30,7 @@ async function init() {
28
30
  console.log("\u2705 \u041F\u0430\u043F\u043A\u0430 'modals' \u0443\u0441\u043F\u0435\u0448\u043D\u043E \u0441\u043E\u0437\u0434\u0430\u043D\u0430!");
29
31
  console.log("\u{1F680} \u0422\u0435\u043F\u0435\u0440\u044C:");
30
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)");
31
- console.log("2. \u0418\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439 const { openModal } = useModal() (\u0438\u043C\u043F\u043E\u0440\u0442 \u0438\u0437 ./modals \u0434\u043B\u044F \u0430\u0432\u0442\u043E\u043A\u043E\u043C\u043F\u043B\u0438\u0442\u0430)");
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)");
32
34
  }
33
35
  function getModalTemplate(ui) {
34
36
  const isShadcn = ui === "shadcn";
@@ -36,7 +38,7 @@ function getModalTemplate(ui) {
36
38
  const boxClass = isShadcn ? "bg-white rounded-xl p-6 max-w-sm w-full shadow-lg" : "modal-box";
37
39
  const buttonClass = isShadcn ? "px-4 py-2 rounded border" : "btn";
38
40
  const primaryClass = isShadcn ? "px-4 py-2 rounded bg-blue-600 text-white" : "btn btn-primary";
39
- return `import { ModalComponentProps } from "your-modal-library";
41
+ return `import { ModalComponentProps } from "modal-system";
40
42
 
41
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
42
44
  export interface ConfirmData {
@@ -73,23 +75,20 @@ export const ConfirmModal = ({ isOpen, onClose, data }: ModalComponentProps<Conf
73
75
  `;
74
76
  }
75
77
  function getModalsIndexFile() {
76
- return `import { useModal as useBaseModal } from "your-modal-library";
77
- import { ConfirmModal } from "./confirm-modal";
78
+ return `import { ConfirmModal } from "./confirm-modal";
78
79
 
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
80
81
  export const MODALS = {
81
- confirm: {
82
- component: ConfirmModal,
83
- },
82
+ confirm: ConfirmModal,
84
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
85
84
  } as const;
86
85
 
87
- // 2. \u0421\u043E\u0437\u0434\u0430\u0435\u043C \u0442\u0438\u043F \u043D\u0430 \u043E\u0441\u043D\u043E\u0432\u0435 \u043A\u043E\u043D\u0444\u0438\u0433\u0430
88
- export type AppModals = typeof MODALS;
89
-
90
- // 3. \u042D\u043A\u0441\u043F\u043E\u0440\u0442\u0438\u0440\u0443\u0435\u043C \u0442\u0438\u043F\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043D\u044B\u0439 \u0445\u0443\u043A \u0434\u043B\u044F \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u043D\u0438\u044F \u0432 \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0438
91
- // \u0422\u0435\u043F\u0435\u0440\u044C openModal \u0431\u0443\u0434\u0435\u0442 \u0437\u043D\u0430\u0442\u044C \u0432\u0441\u0435 \u043A\u043B\u044E\u0447\u0438 \u0438 \u0442\u0438\u043F\u044B \u0434\u0430\u043D\u043D\u044B\u0445 \u0430\u0432\u0442\u043E\u043C\u0430\u0442\u0438\u0447\u0435\u0441\u043A\u0438
92
- export const useModal = () => useBaseModal<AppModals>();
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
+ }
93
92
  `;
94
93
  }
95
94
  init();
package/dist/index.cjs CHANGED
@@ -22,7 +22,7 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  ModalContext: () => ModalContext,
24
24
  ModalProvider: () => ModalProvider,
25
- useBaseModal: () => useBaseModal
25
+ useModal: () => useModal
26
26
  });
27
27
  module.exports = __toCommonJS(index_exports);
28
28
 
@@ -42,8 +42,7 @@ var ModalProvider = ({
42
42
  setActiveModal(null);
43
43
  }, []);
44
44
  const value = (0, import_react.useMemo)(() => ({ openModal, closeModal }), [openModal, closeModal]);
45
- const currentConfig = activeModal ? modals[activeModal.name] : null;
46
- const Component = currentConfig?.component;
45
+ const Component = activeModal ? modals[activeModal.name] : null;
47
46
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ModalContext.Provider, { value, children: [
48
47
  children,
49
48
  Component && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
@@ -59,7 +58,7 @@ var ModalProvider = ({
59
58
 
60
59
  // src/hooks/use-modal.ts
61
60
  var import_react2 = require("react");
62
- var useBaseModal = () => {
61
+ var useModal = () => {
63
62
  const ctx = (0, import_react2.useContext)(ModalContext);
64
63
  if (!ctx) throw new Error("useModal must be used inside ModalProvider");
65
64
  return ctx;
@@ -68,5 +67,5 @@ var useBaseModal = () => {
68
67
  0 && (module.exports = {
69
68
  ModalContext,
70
69
  ModalProvider,
71
- useBaseModal
70
+ useModal
72
71
  });
package/dist/index.d.cts CHANGED
@@ -6,22 +6,24 @@ type ModalComponentProps<T = any> = {
6
6
  onClose: () => void;
7
7
  data: T;
8
8
  };
9
- type ModalConfig<T = any> = {
10
- component: React.ComponentType<ModalComponentProps<T>>;
11
- };
12
- type ModalsRegistry = Record<string, ModalConfig<any>>;
13
- type InferModalData<T> = T extends ModalConfig<infer D> ? D : never;
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;
14
16
 
15
- interface ModalContextType<T extends ModalsRegistry = any> {
16
- openModal: <K extends keyof T>(name: K, data: InferModalData<T[K]>) => void;
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;
17
19
  closeModal: () => void;
18
20
  }
19
21
  declare const ModalContext: React.Context<ModalContextType<any> | null>;
20
- declare const ModalProvider: <T extends ModalsRegistry>({ children, modals, }: {
22
+ declare const ModalProvider: <T extends ModalsRegistry = AnyModalsRegistry>({ children, modals, }: {
21
23
  children: React.ReactNode;
22
24
  modals: T;
23
25
  }) => react_jsx_runtime.JSX.Element;
24
26
 
25
- declare const useBaseModal: <T extends ModalsRegistry>() => ModalContextType<T>;
27
+ declare const useModal: () => ModalContextType<ModalsRegistry>;
26
28
 
27
- export { type InferModalData, type ModalComponentProps, type ModalConfig, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, useBaseModal };
29
+ export { type AnyModalsRegistry, type InferModalData, type ModalComponentProps, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, type Register, useModal };
package/dist/index.d.ts CHANGED
@@ -6,22 +6,24 @@ type ModalComponentProps<T = any> = {
6
6
  onClose: () => void;
7
7
  data: T;
8
8
  };
9
- type ModalConfig<T = any> = {
10
- component: React.ComponentType<ModalComponentProps<T>>;
11
- };
12
- type ModalsRegistry = Record<string, ModalConfig<any>>;
13
- type InferModalData<T> = T extends ModalConfig<infer D> ? D : never;
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;
14
16
 
15
- interface ModalContextType<T extends ModalsRegistry = any> {
16
- openModal: <K extends keyof T>(name: K, data: InferModalData<T[K]>) => void;
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;
17
19
  closeModal: () => void;
18
20
  }
19
21
  declare const ModalContext: React.Context<ModalContextType<any> | null>;
20
- declare const ModalProvider: <T extends ModalsRegistry>({ children, modals, }: {
22
+ declare const ModalProvider: <T extends ModalsRegistry = AnyModalsRegistry>({ children, modals, }: {
21
23
  children: React.ReactNode;
22
24
  modals: T;
23
25
  }) => react_jsx_runtime.JSX.Element;
24
26
 
25
- declare const useBaseModal: <T extends ModalsRegistry>() => ModalContextType<T>;
27
+ declare const useModal: () => ModalContextType<ModalsRegistry>;
26
28
 
27
- export { type InferModalData, type ModalComponentProps, type ModalConfig, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, useBaseModal };
29
+ export { type AnyModalsRegistry, type InferModalData, type ModalComponentProps, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, type Register, useModal };
package/dist/index.js CHANGED
@@ -14,8 +14,7 @@ var ModalProvider = ({
14
14
  setActiveModal(null);
15
15
  }, []);
16
16
  const value = useMemo(() => ({ openModal, closeModal }), [openModal, closeModal]);
17
- const currentConfig = activeModal ? modals[activeModal.name] : null;
18
- const Component = currentConfig?.component;
17
+ const Component = activeModal ? modals[activeModal.name] : null;
19
18
  return /* @__PURE__ */ jsxs(ModalContext.Provider, { value, children: [
20
19
  children,
21
20
  Component && /* @__PURE__ */ jsx(
@@ -31,7 +30,7 @@ var ModalProvider = ({
31
30
 
32
31
  // src/hooks/use-modal.ts
33
32
  import { useContext } from "react";
34
- var useBaseModal = () => {
33
+ var useModal = () => {
35
34
  const ctx = useContext(ModalContext);
36
35
  if (!ctx) throw new Error("useModal must be used inside ModalProvider");
37
36
  return ctx;
@@ -39,5 +38,5 @@ var useBaseModal = () => {
39
38
  export {
40
39
  ModalContext,
41
40
  ModalProvider,
42
- useBaseModal
41
+ useModal
43
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modal-system",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",