@ivex0002/stack-modal 1.0.0 → 1.0.3
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/package.json +6 -6
- package/dist/index.d.mts +0 -24
- package/dist/index.mjs +0 -83
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ivex0002/stack-modal",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "A flexible and customizable modal stack management library for React",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/index.
|
|
7
|
-
"module": "./dist/index.
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.
|
|
13
|
-
"require": "./dist/index.
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"typescript": "^5.0.0"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
|
-
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
57
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
58
58
|
"pack:test": "npm run build && npm pack"
|
|
59
59
|
}
|
|
60
60
|
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
type ModalRegistry = {
|
|
2
|
-
[key: string]: (props?: unknown) => React.ReactNode;
|
|
3
|
-
};
|
|
4
|
-
type AnyFn = (...args: unknown[]) => unknown;
|
|
5
|
-
type PushFn<F extends AnyFn> = Parameters<F> extends [] ? () => void : (props?: Parameters<F>[0]) => void;
|
|
6
|
-
type ModalLayout = {
|
|
7
|
-
Background: React.ComponentType<{
|
|
8
|
-
children: React.ReactNode;
|
|
9
|
-
onClose: () => void;
|
|
10
|
-
}>;
|
|
11
|
-
ModalWrap: React.ComponentType<{
|
|
12
|
-
children: React.ReactNode;
|
|
13
|
-
depth: number;
|
|
14
|
-
isTop: boolean;
|
|
15
|
-
}>;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
declare function createModalStack<M extends ModalRegistry = ModalRegistry>(modals: M, modalLayout: ModalLayout): { [K in Extract<keyof M, string>]: {
|
|
19
|
-
push: PushFn<M[K]>;
|
|
20
|
-
}; } & {
|
|
21
|
-
pop: () => void;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export { type ModalLayout, createModalStack };
|
package/dist/index.mjs
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
// src/ensureRoot.ts
|
|
2
|
-
import { createRoot } from "react-dom/client";
|
|
3
|
-
function ensureRoot(render) {
|
|
4
|
-
const container = document.createElement("div");
|
|
5
|
-
container.setAttribute("data-modal-root", "true");
|
|
6
|
-
document.body.appendChild(container);
|
|
7
|
-
const root = createRoot(container);
|
|
8
|
-
root.render(render());
|
|
9
|
-
return {
|
|
10
|
-
destroy: () => {
|
|
11
|
-
root.unmount();
|
|
12
|
-
container.remove();
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// src/ModalRender.tsx
|
|
18
|
-
import React from "react";
|
|
19
|
-
import { jsx } from "react/jsx-runtime";
|
|
20
|
-
function ModalRender({
|
|
21
|
-
modals,
|
|
22
|
-
modalLayout,
|
|
23
|
-
store
|
|
24
|
-
}) {
|
|
25
|
-
const stack = React.useSyncExternalStore(
|
|
26
|
-
store.subscribe,
|
|
27
|
-
store.get,
|
|
28
|
-
store.get
|
|
29
|
-
);
|
|
30
|
-
if (stack.length === 0) return null;
|
|
31
|
-
return /* @__PURE__ */ jsx(modalLayout.Background, { onClose: () => store.pop(), children: stack.map((item, index) => {
|
|
32
|
-
const depth = stack.length - 1 - index;
|
|
33
|
-
return /* @__PURE__ */ jsx(
|
|
34
|
-
modalLayout.ModalWrap,
|
|
35
|
-
{
|
|
36
|
-
depth,
|
|
37
|
-
isTop: index === stack.length - 1,
|
|
38
|
-
children: modals[item.key](item.props)
|
|
39
|
-
},
|
|
40
|
-
index
|
|
41
|
-
);
|
|
42
|
-
}) });
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// src/createStackStore.ts
|
|
46
|
-
var createStackStore = () => {
|
|
47
|
-
let stack = [];
|
|
48
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
49
|
-
return {
|
|
50
|
-
push(item) {
|
|
51
|
-
stack = [...stack, item];
|
|
52
|
-
listeners.forEach((l) => l());
|
|
53
|
-
},
|
|
54
|
-
pop() {
|
|
55
|
-
stack = stack.slice(0, -1);
|
|
56
|
-
listeners.forEach((l) => l());
|
|
57
|
-
},
|
|
58
|
-
get() {
|
|
59
|
-
return stack;
|
|
60
|
-
},
|
|
61
|
-
subscribe(fn) {
|
|
62
|
-
listeners.add(fn);
|
|
63
|
-
return () => listeners.delete(fn);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
// src/createModalStack.tsx
|
|
69
|
-
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
70
|
-
function createModalStack(modals, modalLayout) {
|
|
71
|
-
const store = createStackStore();
|
|
72
|
-
ensureRoot(() => /* @__PURE__ */ jsx2(ModalRender, { modals, modalLayout, store }));
|
|
73
|
-
const modal = {};
|
|
74
|
-
Object.keys(modals).forEach((key) => {
|
|
75
|
-
modal[key] = {
|
|
76
|
-
push: (props) => store.push({ key, props })
|
|
77
|
-
};
|
|
78
|
-
});
|
|
79
|
-
return { ...modal, pop: () => store.pop() };
|
|
80
|
-
}
|
|
81
|
-
export {
|
|
82
|
-
createModalStack
|
|
83
|
-
};
|