modal-system 0.1.0 → 0.1.1
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/index.cjs +16 -16
- package/dist/index.d.cts +13 -14
- package/dist/index.d.ts +13 -14
- package/dist/index.js +16 -16
- package/package.json +3 -3
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
|
-
|
|
25
|
+
useBaseModal: () => useBaseModal
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
|
|
@@ -35,21 +35,23 @@ var ModalProvider = ({
|
|
|
35
35
|
modals
|
|
36
36
|
}) => {
|
|
37
37
|
const [activeModal, setActiveModal] = (0, import_react.useState)(null);
|
|
38
|
-
const openModal = (name, data) => {
|
|
38
|
+
const openModal = (0, import_react.useCallback)((name, data) => {
|
|
39
39
|
setActiveModal({ name, data });
|
|
40
|
-
};
|
|
41
|
-
const closeModal = () => {
|
|
40
|
+
}, []);
|
|
41
|
+
const closeModal = (0, import_react.useCallback)(() => {
|
|
42
42
|
setActiveModal(null);
|
|
43
|
-
};
|
|
44
|
-
const
|
|
45
|
-
|
|
43
|
+
}, []);
|
|
44
|
+
const value = (0, import_react.useMemo)(() => ({ openModal, closeModal }), [openModal, closeModal]);
|
|
45
|
+
const currentConfig = activeModal ? modals[activeModal.name] : null;
|
|
46
|
+
const Component = currentConfig?.component;
|
|
47
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ModalContext.Provider, { value, children: [
|
|
46
48
|
children,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
Component && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
50
|
+
Component,
|
|
49
51
|
{
|
|
50
52
|
isOpen: true,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
onClose: closeModal,
|
|
54
|
+
data: activeModal?.data
|
|
53
55
|
}
|
|
54
56
|
)
|
|
55
57
|
] });
|
|
@@ -57,16 +59,14 @@ var ModalProvider = ({
|
|
|
57
59
|
|
|
58
60
|
// src/hooks/use-modal.ts
|
|
59
61
|
var import_react2 = require("react");
|
|
60
|
-
var
|
|
62
|
+
var useBaseModal = () => {
|
|
61
63
|
const ctx = (0, import_react2.useContext)(ModalContext);
|
|
62
|
-
if (!ctx)
|
|
63
|
-
throw new Error("useModal must be used inside ModalProvider");
|
|
64
|
-
}
|
|
64
|
+
if (!ctx) throw new Error("useModal must be used inside ModalProvider");
|
|
65
65
|
return ctx;
|
|
66
66
|
};
|
|
67
67
|
// Annotate the CommonJS export names for ESM import in node:
|
|
68
68
|
0 && (module.exports = {
|
|
69
69
|
ModalContext,
|
|
70
70
|
ModalProvider,
|
|
71
|
-
|
|
71
|
+
useBaseModal
|
|
72
72
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -4,25 +4,24 @@ import React from 'react';
|
|
|
4
4
|
type ModalComponentProps<T = any> = {
|
|
5
5
|
isOpen: boolean;
|
|
6
6
|
onClose: () => void;
|
|
7
|
-
data
|
|
7
|
+
data: T;
|
|
8
8
|
};
|
|
9
|
-
type ModalConfig = {
|
|
10
|
-
component: React.
|
|
9
|
+
type ModalConfig<T = any> = {
|
|
10
|
+
component: React.ComponentType<ModalComponentProps<T>>;
|
|
11
11
|
};
|
|
12
|
-
type
|
|
12
|
+
type ModalsRegistry = Record<string, ModalConfig<any>>;
|
|
13
|
+
type InferModalData<T> = T extends ModalConfig<infer D> ? D : never;
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
openModal: (name:
|
|
15
|
+
interface ModalContextType<T extends ModalsRegistry = any> {
|
|
16
|
+
openModal: <K extends keyof T>(name: K, data: InferModalData<T[K]>) => void;
|
|
16
17
|
closeModal: () => void;
|
|
17
|
-
}
|
|
18
|
-
declare const
|
|
18
|
+
}
|
|
19
|
+
declare const ModalContext: React.Context<ModalContextType<any> | null>;
|
|
20
|
+
declare const ModalProvider: <T extends ModalsRegistry>({ children, modals, }: {
|
|
19
21
|
children: React.ReactNode;
|
|
20
|
-
modals:
|
|
22
|
+
modals: T;
|
|
21
23
|
}) => react_jsx_runtime.JSX.Element;
|
|
22
24
|
|
|
23
|
-
declare const
|
|
24
|
-
openModal: (name: string, data?: any) => void;
|
|
25
|
-
closeModal: () => void;
|
|
26
|
-
};
|
|
25
|
+
declare const useBaseModal: <T extends ModalsRegistry>() => ModalContextType<T>;
|
|
27
26
|
|
|
28
|
-
export { type ModalComponentProps, type ModalConfig, ModalContext, ModalProvider, type
|
|
27
|
+
export { type InferModalData, type ModalComponentProps, type ModalConfig, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, useBaseModal };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,25 +4,24 @@ import React from 'react';
|
|
|
4
4
|
type ModalComponentProps<T = any> = {
|
|
5
5
|
isOpen: boolean;
|
|
6
6
|
onClose: () => void;
|
|
7
|
-
data
|
|
7
|
+
data: T;
|
|
8
8
|
};
|
|
9
|
-
type ModalConfig = {
|
|
10
|
-
component: React.
|
|
9
|
+
type ModalConfig<T = any> = {
|
|
10
|
+
component: React.ComponentType<ModalComponentProps<T>>;
|
|
11
11
|
};
|
|
12
|
-
type
|
|
12
|
+
type ModalsRegistry = Record<string, ModalConfig<any>>;
|
|
13
|
+
type InferModalData<T> = T extends ModalConfig<infer D> ? D : never;
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
openModal: (name:
|
|
15
|
+
interface ModalContextType<T extends ModalsRegistry = any> {
|
|
16
|
+
openModal: <K extends keyof T>(name: K, data: InferModalData<T[K]>) => void;
|
|
16
17
|
closeModal: () => void;
|
|
17
|
-
}
|
|
18
|
-
declare const
|
|
18
|
+
}
|
|
19
|
+
declare const ModalContext: React.Context<ModalContextType<any> | null>;
|
|
20
|
+
declare const ModalProvider: <T extends ModalsRegistry>({ children, modals, }: {
|
|
19
21
|
children: React.ReactNode;
|
|
20
|
-
modals:
|
|
22
|
+
modals: T;
|
|
21
23
|
}) => react_jsx_runtime.JSX.Element;
|
|
22
24
|
|
|
23
|
-
declare const
|
|
24
|
-
openModal: (name: string, data?: any) => void;
|
|
25
|
-
closeModal: () => void;
|
|
26
|
-
};
|
|
25
|
+
declare const useBaseModal: <T extends ModalsRegistry>() => ModalContextType<T>;
|
|
27
26
|
|
|
28
|
-
export { type ModalComponentProps, type ModalConfig, ModalContext, ModalProvider, type
|
|
27
|
+
export { type InferModalData, type ModalComponentProps, type ModalConfig, ModalContext, type ModalContextType, ModalProvider, type ModalsRegistry, useBaseModal };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/provider/ModalProvider.tsx
|
|
2
|
-
import { createContext, useState } from "react";
|
|
2
|
+
import { createContext, useState, useCallback, useMemo } from "react";
|
|
3
3
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
var ModalContext = createContext(null);
|
|
5
5
|
var ModalProvider = ({
|
|
@@ -7,21 +7,23 @@ var ModalProvider = ({
|
|
|
7
7
|
modals
|
|
8
8
|
}) => {
|
|
9
9
|
const [activeModal, setActiveModal] = useState(null);
|
|
10
|
-
const openModal = (name, data) => {
|
|
10
|
+
const openModal = useCallback((name, data) => {
|
|
11
11
|
setActiveModal({ name, data });
|
|
12
|
-
};
|
|
13
|
-
const closeModal = () => {
|
|
12
|
+
}, []);
|
|
13
|
+
const closeModal = useCallback(() => {
|
|
14
14
|
setActiveModal(null);
|
|
15
|
-
};
|
|
16
|
-
const
|
|
17
|
-
|
|
15
|
+
}, []);
|
|
16
|
+
const value = useMemo(() => ({ openModal, closeModal }), [openModal, closeModal]);
|
|
17
|
+
const currentConfig = activeModal ? modals[activeModal.name] : null;
|
|
18
|
+
const Component = currentConfig?.component;
|
|
19
|
+
return /* @__PURE__ */ jsxs(ModalContext.Provider, { value, children: [
|
|
18
20
|
children,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
Component && /* @__PURE__ */ jsx(
|
|
22
|
+
Component,
|
|
21
23
|
{
|
|
22
24
|
isOpen: true,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
onClose: closeModal,
|
|
26
|
+
data: activeModal?.data
|
|
25
27
|
}
|
|
26
28
|
)
|
|
27
29
|
] });
|
|
@@ -29,15 +31,13 @@ var ModalProvider = ({
|
|
|
29
31
|
|
|
30
32
|
// src/hooks/use-modal.ts
|
|
31
33
|
import { useContext } from "react";
|
|
32
|
-
var
|
|
34
|
+
var useBaseModal = () => {
|
|
33
35
|
const ctx = useContext(ModalContext);
|
|
34
|
-
if (!ctx)
|
|
35
|
-
throw new Error("useModal must be used inside ModalProvider");
|
|
36
|
-
}
|
|
36
|
+
if (!ctx) throw new Error("useModal must be used inside ModalProvider");
|
|
37
37
|
return ctx;
|
|
38
38
|
};
|
|
39
39
|
export {
|
|
40
40
|
ModalContext,
|
|
41
41
|
ModalProvider,
|
|
42
|
-
|
|
42
|
+
useBaseModal
|
|
43
43
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "modal-system",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"bin": {
|
|
8
|
-
"modal-
|
|
8
|
+
"modal-system": "dist/cli.js"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
11
|
"dist"
|
|
@@ -25,4 +25,4 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"prompts": "^2.4.2"
|
|
27
27
|
}
|
|
28
|
-
}
|
|
28
|
+
}
|