react-flow-modal 0.5.0 → 0.6.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/index.cjs +36 -38
- package/dist/index.d.cts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +37 -39
- package/package.json +5 -2
- package/src/ModalHost.tsx +8 -4
- package/src/ModalProvider.tsx +46 -6
- package/src/useModal.tsx +10 -11
- package/src/modalContext.ts +0 -9
- package/src/useModalContext.ts +0 -12
package/dist/index.cjs
CHANGED
|
@@ -27,66 +27,64 @@ __export(index_exports, {
|
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
|
|
29
29
|
// src/ModalProvider.tsx
|
|
30
|
-
var import_react2 = require("react");
|
|
31
|
-
|
|
32
|
-
// src/modalContext.ts
|
|
33
30
|
var import_react = require("react");
|
|
34
|
-
var
|
|
35
|
-
stack: [],
|
|
36
|
-
setStack: () => []
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// src/ModalProvider.tsx
|
|
31
|
+
var import_zustand = require("zustand");
|
|
40
32
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
41
|
-
var
|
|
42
|
-
|
|
43
|
-
return
|
|
33
|
+
var createModalStore = (initialState) => (0, import_zustand.createStore)((set) => {
|
|
34
|
+
var _a;
|
|
35
|
+
return {
|
|
36
|
+
stack: (_a = initialState == null ? void 0 : initialState.stack) != null ? _a : /* @__PURE__ */ new Map(),
|
|
37
|
+
appendStack: (key, element) => set((state) => ({ stack: new Map(state.stack).set(key, element) })),
|
|
38
|
+
removeStack: (key) => set((state) => {
|
|
39
|
+
const newStack = new Map(state.stack);
|
|
40
|
+
newStack.delete(key);
|
|
41
|
+
return { stack: newStack };
|
|
42
|
+
})
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
var ModalStoreContext = (0, import_react.createContext)(null);
|
|
46
|
+
var ModalProvider = ({ children, initialState }) => {
|
|
47
|
+
const [store] = (0, import_react.useState)(createModalStore(initialState));
|
|
48
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ModalStoreContext.Provider, { value: store, children });
|
|
44
49
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// src/useModalContext.ts
|
|
50
|
-
var import_react3 = require("react");
|
|
51
|
-
var useModalContext = () => {
|
|
52
|
-
const context = (0, import_react3.useContext)(ModalContext);
|
|
53
|
-
if (!context) {
|
|
54
|
-
throw new Error("useModalContext must be used within a ModalProvider");
|
|
55
|
-
}
|
|
56
|
-
return context;
|
|
50
|
+
var useModalStore = (selector) => {
|
|
51
|
+
const store = (0, import_react.useContext)(ModalStoreContext);
|
|
52
|
+
if (!store) throw new Error("DashboardStoreProvider missing");
|
|
53
|
+
return (0, import_zustand.useStore)(store, selector);
|
|
57
54
|
};
|
|
58
55
|
|
|
59
56
|
// src/useModal.tsx
|
|
57
|
+
var import_react2 = require("react");
|
|
60
58
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
61
59
|
var useModal = () => {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
};
|
|
60
|
+
const appendStack = useModalStore(
|
|
61
|
+
(state) => state.appendStack
|
|
62
|
+
);
|
|
63
|
+
const removeStack = useModalStore(
|
|
64
|
+
(state) => state.removeStack
|
|
65
|
+
);
|
|
69
66
|
const open = (key, render) => {
|
|
70
67
|
return new Promise((resolve, reject) => {
|
|
71
68
|
const element = render((value) => {
|
|
72
69
|
resolve(value);
|
|
73
|
-
|
|
70
|
+
removeStack(key);
|
|
74
71
|
}, (reason) => {
|
|
75
72
|
reject(reason);
|
|
76
|
-
|
|
73
|
+
removeStack(key);
|
|
77
74
|
});
|
|
78
|
-
|
|
75
|
+
appendStack(key, /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react2.Fragment, { children: element }, `modal-${key}`));
|
|
79
76
|
});
|
|
80
77
|
};
|
|
81
78
|
return { open };
|
|
82
79
|
};
|
|
83
80
|
|
|
84
81
|
// src/ModalHost.tsx
|
|
85
|
-
var import_react5 = require("react");
|
|
86
82
|
var ModalHost = ({ children }) => {
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
83
|
+
const stack = useModalStore(
|
|
84
|
+
(state) => state.stack
|
|
85
|
+
);
|
|
86
|
+
if (!children) return Array.from(stack.values());
|
|
87
|
+
return children(Array.from(stack.values()));
|
|
90
88
|
};
|
|
91
89
|
// Annotate the CommonJS export names for ESM import in node:
|
|
92
90
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import React$1, {
|
|
1
|
+
import React$1, { PropsWithChildren, ReactElement, FC } from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type ModalState = {
|
|
4
|
+
stack: Map<string, ReactElement>;
|
|
5
|
+
appendStack: (key: string, element: ReactElement) => void;
|
|
6
|
+
removeStack: (key: string) => void;
|
|
7
|
+
};
|
|
8
|
+
type ProviderProps = {
|
|
9
|
+
initialState?: Partial<ModalState>;
|
|
10
|
+
};
|
|
11
|
+
declare const ModalProvider: React$1.FC<PropsWithChildren<ProviderProps>>;
|
|
4
12
|
|
|
5
13
|
declare const useModal: () => {
|
|
6
14
|
open: <T>(key: string, render: (resolve: (value: T) => void, reject: (reason?: unknown) => void) => React$1.ReactNode) => Promise<T>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import React$1, {
|
|
1
|
+
import React$1, { PropsWithChildren, ReactElement, FC } from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type ModalState = {
|
|
4
|
+
stack: Map<string, ReactElement>;
|
|
5
|
+
appendStack: (key: string, element: ReactElement) => void;
|
|
6
|
+
removeStack: (key: string) => void;
|
|
7
|
+
};
|
|
8
|
+
type ProviderProps = {
|
|
9
|
+
initialState?: Partial<ModalState>;
|
|
10
|
+
};
|
|
11
|
+
declare const ModalProvider: React$1.FC<PropsWithChildren<ProviderProps>>;
|
|
4
12
|
|
|
5
13
|
declare const useModal: () => {
|
|
6
14
|
open: <T>(key: string, render: (resolve: (value: T) => void, reject: (reason?: unknown) => void) => React$1.ReactNode) => Promise<T>;
|
package/dist/index.js
CHANGED
|
@@ -1,64 +1,62 @@
|
|
|
1
1
|
// src/ModalProvider.tsx
|
|
2
|
-
import { useState } from "react";
|
|
3
|
-
|
|
4
|
-
// src/modalContext.ts
|
|
5
|
-
import { createContext } from "react";
|
|
6
|
-
var ModalContext = createContext({
|
|
7
|
-
stack: [],
|
|
8
|
-
setStack: () => []
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
// src/ModalProvider.tsx
|
|
2
|
+
import { createContext, useContext, useState } from "react";
|
|
3
|
+
import { createStore, useStore } from "zustand";
|
|
12
4
|
import { jsx } from "react/jsx-runtime";
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
return
|
|
5
|
+
var createModalStore = (initialState) => createStore((set) => {
|
|
6
|
+
var _a;
|
|
7
|
+
return {
|
|
8
|
+
stack: (_a = initialState == null ? void 0 : initialState.stack) != null ? _a : /* @__PURE__ */ new Map(),
|
|
9
|
+
appendStack: (key, element) => set((state) => ({ stack: new Map(state.stack).set(key, element) })),
|
|
10
|
+
removeStack: (key) => set((state) => {
|
|
11
|
+
const newStack = new Map(state.stack);
|
|
12
|
+
newStack.delete(key);
|
|
13
|
+
return { stack: newStack };
|
|
14
|
+
})
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
var ModalStoreContext = createContext(null);
|
|
18
|
+
var ModalProvider = ({ children, initialState }) => {
|
|
19
|
+
const [store] = useState(createModalStore(initialState));
|
|
20
|
+
return /* @__PURE__ */ jsx(ModalStoreContext.Provider, { value: store, children });
|
|
16
21
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// src/useModalContext.ts
|
|
22
|
-
import { useContext } from "react";
|
|
23
|
-
var useModalContext = () => {
|
|
24
|
-
const context = useContext(ModalContext);
|
|
25
|
-
if (!context) {
|
|
26
|
-
throw new Error("useModalContext must be used within a ModalProvider");
|
|
27
|
-
}
|
|
28
|
-
return context;
|
|
22
|
+
var useModalStore = (selector) => {
|
|
23
|
+
const store = useContext(ModalStoreContext);
|
|
24
|
+
if (!store) throw new Error("DashboardStoreProvider missing");
|
|
25
|
+
return useStore(store, selector);
|
|
29
26
|
};
|
|
30
27
|
|
|
31
28
|
// src/useModal.tsx
|
|
29
|
+
import { Fragment } from "react";
|
|
32
30
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
33
31
|
var useModal = () => {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
};
|
|
32
|
+
const appendStack = useModalStore(
|
|
33
|
+
(state) => state.appendStack
|
|
34
|
+
);
|
|
35
|
+
const removeStack = useModalStore(
|
|
36
|
+
(state) => state.removeStack
|
|
37
|
+
);
|
|
41
38
|
const open = (key, render) => {
|
|
42
39
|
return new Promise((resolve, reject) => {
|
|
43
40
|
const element = render((value) => {
|
|
44
41
|
resolve(value);
|
|
45
|
-
|
|
42
|
+
removeStack(key);
|
|
46
43
|
}, (reason) => {
|
|
47
44
|
reject(reason);
|
|
48
|
-
|
|
45
|
+
removeStack(key);
|
|
49
46
|
});
|
|
50
|
-
|
|
47
|
+
appendStack(key, /* @__PURE__ */ jsx2(Fragment, { children: element }, `modal-${key}`));
|
|
51
48
|
});
|
|
52
49
|
};
|
|
53
50
|
return { open };
|
|
54
51
|
};
|
|
55
52
|
|
|
56
53
|
// src/ModalHost.tsx
|
|
57
|
-
import { isValidElement } from "react";
|
|
58
54
|
var ModalHost = ({ children }) => {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
const stack = useModalStore(
|
|
56
|
+
(state) => state.stack
|
|
57
|
+
);
|
|
58
|
+
if (!children) return Array.from(stack.values());
|
|
59
|
+
return children(Array.from(stack.values()));
|
|
62
60
|
};
|
|
63
61
|
export {
|
|
64
62
|
ModalHost,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-flow-modal",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Promise-based modal flows for React",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -39,5 +39,8 @@
|
|
|
39
39
|
"tsup": "^8.5.1",
|
|
40
40
|
"typescript": "^5.9.3"
|
|
41
41
|
},
|
|
42
|
-
"packageManager": "pnpm@10.27.0"
|
|
42
|
+
"packageManager": "pnpm@10.27.0",
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"zustand": "^5.0.11"
|
|
45
|
+
}
|
|
43
46
|
}
|
package/src/ModalHost.tsx
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { FC, isValidElement, ReactElement } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { useModalStore } from './ModalProvider';
|
|
3
|
+
|
|
3
4
|
|
|
4
5
|
export const ModalHost: FC<{ children?: (modals: ReactElement[]) => React.ReactNode }> = ({ children }) => {
|
|
5
|
-
const
|
|
6
|
+
const stack = useModalStore(
|
|
7
|
+
(state) => state.stack
|
|
8
|
+
);
|
|
9
|
+
|
|
6
10
|
|
|
7
|
-
if (!children) return stack.
|
|
8
|
-
return children(stack.
|
|
11
|
+
if (!children) return Array.from(stack.values());
|
|
12
|
+
return children(Array.from(stack.values()));
|
|
9
13
|
};
|
package/src/ModalProvider.tsx
CHANGED
|
@@ -1,12 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
import { ModalContext } from "./modalContext";
|
|
1
|
+
'use client';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
|
|
4
|
+
import React, { createContext, PropsWithChildren, ReactElement, useContext, useState } from 'react';
|
|
5
|
+
import { createStore, ExtractState, StoreApi, useStore } from 'zustand';
|
|
6
|
+
|
|
7
|
+
type ModalState = {
|
|
8
|
+
stack: Map<string, ReactElement>;
|
|
9
|
+
appendStack: (key: string, element: ReactElement) => void;
|
|
10
|
+
removeStack: (key: string) => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const createModalStore = (initialState?: Partial<ModalState>) => createStore<ModalState>((set) => {
|
|
16
|
+
return {
|
|
17
|
+
stack: initialState?.stack ?? new Map<string, ReactElement>(),
|
|
18
|
+
|
|
19
|
+
appendStack: (key: string, element: ReactElement) => set((state) => ({ stack: new Map(state.stack).set(key, element) })),
|
|
20
|
+
|
|
21
|
+
removeStack: (key: string) => set((state) => {
|
|
22
|
+
const newStack = new Map(state.stack);
|
|
23
|
+
newStack.delete(key);
|
|
24
|
+
return { stack: newStack };
|
|
25
|
+
}),
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const ModalStoreContext = createContext<StoreApi<ModalState> | null>(null);
|
|
30
|
+
|
|
31
|
+
type ProviderProps = {
|
|
32
|
+
initialState?: Partial<ModalState>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export const ModalProvider: React.FC<PropsWithChildren<ProviderProps>> = ({ children, initialState }) => {
|
|
37
|
+
// 여러번 호출되어도 store가 하나만 생성되도록 함
|
|
38
|
+
const [store] = useState(createModalStore(initialState));
|
|
6
39
|
|
|
7
40
|
return (
|
|
8
|
-
<
|
|
41
|
+
<ModalStoreContext.Provider value={store}>
|
|
9
42
|
{children}
|
|
10
|
-
</
|
|
43
|
+
</ModalStoreContext.Provider>
|
|
11
44
|
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
export const useModalStore = <T,>(selector: (state: ExtractState<StoreApi<ModalState>>) => T) => {
|
|
49
|
+
const store = useContext(ModalStoreContext);
|
|
50
|
+
if (!store) throw new Error('DashboardStoreProvider missing');
|
|
51
|
+
return useStore(store, selector);
|
|
12
52
|
};
|
package/src/useModal.tsx
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import React, { Fragment } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { useModalStore } from "./ModalProvider";
|
|
3
3
|
|
|
4
4
|
export const useModal = () => {
|
|
5
|
-
const
|
|
5
|
+
const appendStack = useModalStore(
|
|
6
|
+
(state) => state.appendStack
|
|
7
|
+
);
|
|
6
8
|
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
const removeStack = useModalStore(
|
|
10
|
+
(state) => state.removeStack
|
|
11
|
+
);
|
|
10
12
|
|
|
11
|
-
const push = (element: React.ReactNode) => {
|
|
12
|
-
setStack((prev) => [...prev, element]);
|
|
13
|
-
};
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* 모달을 열고 닫는 함수
|
|
@@ -24,12 +23,12 @@ export const useModal = () => {
|
|
|
24
23
|
return new Promise<T>((resolve, reject) => {
|
|
25
24
|
const element = render((value) => {
|
|
26
25
|
resolve(value);
|
|
27
|
-
|
|
26
|
+
removeStack(key);
|
|
28
27
|
}, (reason) => {
|
|
29
28
|
reject(reason);
|
|
30
|
-
|
|
29
|
+
removeStack(key);
|
|
31
30
|
});
|
|
32
|
-
|
|
31
|
+
appendStack(key, <Fragment key={`modal-${key}`}>{element}</Fragment>);
|
|
33
32
|
});
|
|
34
33
|
};
|
|
35
34
|
|
package/src/modalContext.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { createContext, Dispatch, SetStateAction } from "react";
|
|
2
|
-
|
|
3
|
-
export const ModalContext = createContext<{
|
|
4
|
-
stack: React.ReactNode[];
|
|
5
|
-
setStack: Dispatch<SetStateAction<React.ReactNode[]>>
|
|
6
|
-
}>({
|
|
7
|
-
stack: [],
|
|
8
|
-
setStack: () => [] as React.ReactNode[],
|
|
9
|
-
});
|
package/src/useModalContext.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
import { ModalContext } from "./modalContext";
|
|
3
|
-
|
|
4
|
-
export const useModalContext = () => {
|
|
5
|
-
const context = useContext(ModalContext);
|
|
6
|
-
|
|
7
|
-
if (!context) {
|
|
8
|
-
throw new Error('useModalContext must be used within a ModalProvider');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
return context;
|
|
12
|
-
};
|