@4i/modal-manager 1.0.8 → 1.0.10
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 +1 -1
- package/readme.md +151 -0
- package/src/components/modal-provider.d.ts +5 -1
- package/src/components/modal-provider.js +12 -10
- package/src/components/modal-provider.tsx +20 -21
- package/src/index.d.ts +3 -5
- package/src/index.js +3 -7
- package/src/index.ts +3 -5
- package/src/utils/{service/Manager.d.ts → Manager.d.ts} +0 -3
- package/src/utils/{service/Manager.js → Manager.js} +0 -19
- package/src/utils/{service/Manager.ts → Manager.ts} +0 -16
- package/src/utils/{service/ModalManager.d.ts → ModalManager.d.ts} +1 -1
- package/src/utils/{service/ModalManager.js → ModalManager.js} +2 -2
- package/src/utils/{service/ModalManager.ts → ModalManager.ts} +2 -2
- package/src/utils/config/modal-actions.d.ts +0 -6
- package/src/utils/config/modal-actions.js +0 -20
- package/src/utils/config/modal-actions.ts +0 -11
- package/src/utils/config/modal-list.d.ts +0 -6
- package/src/utils/config/modal-list.js +0 -20
- package/src/utils/config/modal-list.ts +0 -11
package/package.json
CHANGED
package/readme.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# Modal manager
|
2
|
+
|
3
|
+
[](https://badge.fury.io/js/%404i%2Fmodal-manager)
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
5
|
+
|
6
|
+
Usage in Managing Multiple Modals, Popups, Notifications
|
7
|
+
|
8
|
+
This package simplifies the management of multiple modals, popups, notifications, and similar UI elements within a project. It achieves this by leveraging events, maintaining a centralized list of modals, and assigning actions to them.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
```bash
|
13
|
+
npm install @4i/modal-manager
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
#### Define Modal Actions:
|
19
|
+
|
20
|
+
In your project, define modal actions as keys in the modalAction object. Each key represents a specific modal or UI element.
|
21
|
+
|
22
|
+
```javascript
|
23
|
+
export const modalAction = {
|
24
|
+
MODAL_PROMPT: "modal-prompt",
|
25
|
+
// Additional modal actions...
|
26
|
+
};
|
27
|
+
```
|
28
|
+
|
29
|
+
#### Create Modal List:
|
30
|
+
|
31
|
+
Create a list of modals in the list object, where keys are modal actions, and values are the corresponding modal components.
|
32
|
+
|
33
|
+
```javascript
|
34
|
+
const list = {
|
35
|
+
[modalAction.MODAL_PROMPT]: ModalPrompts,
|
36
|
+
// Additional modals...
|
37
|
+
};
|
38
|
+
```
|
39
|
+
|
40
|
+
#### Call modals
|
41
|
+
|
42
|
+
Call modals in your components by invoking modal.call with the desired modal action.
|
43
|
+
|
44
|
+
```javascript
|
45
|
+
modal.call(modalAction.MODAL_PROMPT, {
|
46
|
+
// Recieve props in your modal component
|
47
|
+
title: "Modal title",
|
48
|
+
content: "Modal content",
|
49
|
+
});
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Extensibility
|
53
|
+
|
54
|
+
If desired, you can inherit from the Manager class to create your own classes for handling custom notifications, popups, and more. And then pass your custom class to the CustomProvider component using ModalProvider as an example
|
55
|
+
|
56
|
+
````javascript
|
57
|
+
|
58
|
+
```javascript
|
59
|
+
import { Manager } from "@4i/modal-manager";
|
60
|
+
|
61
|
+
class CustomManager extends Manager {
|
62
|
+
// Custom methods and logic here
|
63
|
+
}
|
64
|
+
|
65
|
+
const customManager = new CustomManager();
|
66
|
+
````
|
67
|
+
|
68
|
+
### index.js
|
69
|
+
|
70
|
+
```javascript
|
71
|
+
import React from "react";
|
72
|
+
import ReactDOM from "react-dom/client";
|
73
|
+
import "./index.css";
|
74
|
+
import App from "./App";
|
75
|
+
import ModalPrompts from "./modals/prompt";
|
76
|
+
import { ModalProvider } from "@4i/modal-manager";
|
77
|
+
import "@4i/modal-manager/src/styles.css";
|
78
|
+
|
79
|
+
const root = ReactDOM.createRoot(document.getElementById("root"));
|
80
|
+
|
81
|
+
// Define your modal actions here
|
82
|
+
export const modalAction = {
|
83
|
+
MODAL_PROMPT: "modal-prompt",
|
84
|
+
};
|
85
|
+
|
86
|
+
// Your modal list should be an object with modal names
|
87
|
+
// as keys and modal components as values.
|
88
|
+
const list = {
|
89
|
+
[modalAction.MODAL_PROMPT]: ModalPrompts,
|
90
|
+
};
|
91
|
+
|
92
|
+
root.render(
|
93
|
+
<React.StrictMode>
|
94
|
+
<App />
|
95
|
+
<ModalProvider modalList={list} />
|
96
|
+
</React.StrictMode>
|
97
|
+
);
|
98
|
+
```
|
99
|
+
|
100
|
+
### App.js
|
101
|
+
|
102
|
+
```javascript
|
103
|
+
import { modalAction } from ".";
|
104
|
+
import "./App.css";
|
105
|
+
import { modal } from "@4i/modal-manager";
|
106
|
+
|
107
|
+
const App = () => {
|
108
|
+
const handleClick = () => {
|
109
|
+
// Call the modal by its action name
|
110
|
+
modal.call(modalAction.MODAL_PROMPT, {
|
111
|
+
// You can pass any props to your modal component
|
112
|
+
title: "Modal title",
|
113
|
+
content: "Modal content",
|
114
|
+
});
|
115
|
+
};
|
116
|
+
|
117
|
+
return (
|
118
|
+
<div className="app">
|
119
|
+
<button
|
120
|
+
onClick={handleClick}
|
121
|
+
className="w-[200px] h-[80px] mx-auto bg-teal-800 text-white"
|
122
|
+
>
|
123
|
+
Click to call modal
|
124
|
+
</button>
|
125
|
+
</div>
|
126
|
+
);
|
127
|
+
};
|
128
|
+
|
129
|
+
export default App;
|
130
|
+
```
|
131
|
+
|
132
|
+
### ModalPrompts.js
|
133
|
+
|
134
|
+
```javascript
|
135
|
+
import React from "react";
|
136
|
+
|
137
|
+
// Get props
|
138
|
+
const ModalPrompts = ({ title, content }) => {
|
139
|
+
return (
|
140
|
+
<div className="w-[400px] h-[300px] bg-slate-50 p-[24px] flex flex-col justify-center items-center">
|
141
|
+
<h1>{title}</h1>
|
142
|
+
<p>{content}</p>
|
143
|
+
<button>Close</button>
|
144
|
+
</div>
|
145
|
+
);
|
146
|
+
};
|
147
|
+
|
148
|
+
export default ModalPrompts;
|
149
|
+
```
|
150
|
+
|
151
|
+
Feel free to tailor this documentation to better fit your package's specific features and capabilities.
|
@@ -1,6 +1,10 @@
|
|
1
1
|
import React from "react";
|
2
|
+
type ModalList = {
|
3
|
+
[key: string]: React.ComponentType;
|
4
|
+
};
|
2
5
|
interface ModalProviderProps {
|
3
6
|
CustomComponent?: React.ComponentType;
|
7
|
+
modalList: ModalList;
|
4
8
|
}
|
5
|
-
declare const ModalProvider: ({ CustomComponent }: ModalProviderProps) => false | React.JSX.Element[];
|
9
|
+
declare const ModalProvider: ({ CustomComponent, modalList }: ModalProviderProps) => false | React.JSX.Element[];
|
6
10
|
export default ModalProvider;
|
@@ -47,14 +47,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
47
47
|
};
|
48
48
|
Object.defineProperty(exports, "__esModule", { value: true });
|
49
49
|
var react_1 = __importStar(require("react"));
|
50
|
-
var ModalManager_1 = __importDefault(require("../utils/
|
51
|
-
var ModalManager_2 = __importDefault(require("../utils/
|
52
|
-
var modal_list_1 = __importDefault(require("../utils/config/modal-list"));
|
50
|
+
var ModalManager_1 = __importDefault(require("../utils/ModalManager"));
|
51
|
+
var ModalManager_2 = __importDefault(require("../utils/ModalManager"));
|
53
52
|
var ModalProvider = function (_a) {
|
54
|
-
var CustomComponent = _a.CustomComponent;
|
53
|
+
var CustomComponent = _a.CustomComponent, modalList = _a.modalList;
|
55
54
|
var _b = (0, react_1.useState)([]), data = _b[0], setData = _b[1];
|
56
55
|
var _c = (0, react_1.useState)([]), names = _c[0], setNames = _c[1];
|
57
56
|
var modalRef = (0, react_1.useRef)([]);
|
57
|
+
console.log("OPEN MODAL", names, data, modalList);
|
58
58
|
(0, react_1.useEffect)(function () {
|
59
59
|
var handleOpenModal = function (name, data) {
|
60
60
|
setData(function (prev) { return __spreadArray(__spreadArray([], prev, true), [data], false); });
|
@@ -98,10 +98,11 @@ var ModalProvider = function (_a) {
|
|
98
98
|
};
|
99
99
|
}, []);
|
100
100
|
var activeModals = names.map(function (name) {
|
101
|
-
var Component =
|
101
|
+
var Component = modalList[name] || (function () { return react_1.default.createElement(react_1.default.Fragment, null); });
|
102
102
|
return Component;
|
103
103
|
});
|
104
104
|
var handleCloseModal = function (index, e) {
|
105
|
+
console.log(index);
|
105
106
|
if (modalRef.current[index] &&
|
106
107
|
!modalRef.current[index].contains(e.target)) {
|
107
108
|
ModalManager_2.default.close(index);
|
@@ -113,13 +114,14 @@ var ModalProvider = function (_a) {
|
|
113
114
|
return (activeModals.length !== 0 &&
|
114
115
|
activeModals.map(function (Component, i) {
|
115
116
|
var Modal = Component;
|
116
|
-
return (react_1.default.createElement(
|
117
|
+
return (react_1.default.createElement("div", { key: i, onClick: function (e) {
|
117
118
|
handleCloseModal(i, e);
|
118
119
|
} },
|
119
|
-
react_1.default.createElement("div", {
|
120
|
-
|
121
|
-
|
122
|
-
|
120
|
+
react_1.default.createElement("div", { className: "backdrop_modal_manager" },
|
121
|
+
react_1.default.createElement("div", { ref: function (ref) {
|
122
|
+
refReducer(i, ref);
|
123
|
+
} },
|
124
|
+
react_1.default.createElement(Modal, __assign({ key: i }, data[i]))))));
|
123
125
|
}));
|
124
126
|
};
|
125
127
|
exports.default = ModalProvider;
|
@@ -1,19 +1,22 @@
|
|
1
1
|
import React, { useEffect, useRef, useState } from "react";
|
2
|
-
import manager from "../utils/
|
3
|
-
import modal from "../utils/
|
4
|
-
|
2
|
+
import manager from "../utils/ModalManager";
|
3
|
+
import modal from "../utils/ModalManager";
|
4
|
+
|
5
|
+
type ModalList = { [key: string]: React.ComponentType };
|
5
6
|
|
6
7
|
interface ModalProviderProps {
|
7
8
|
CustomComponent?: React.ComponentType;
|
9
|
+
modalList: ModalList;
|
8
10
|
}
|
9
11
|
|
10
12
|
type TData = { [key: string]: any };
|
11
13
|
|
12
|
-
const ModalProvider = ({ CustomComponent }: ModalProviderProps) => {
|
14
|
+
const ModalProvider = ({ CustomComponent, modalList }: ModalProviderProps) => {
|
13
15
|
const [data, setData] = useState<TData[]>([]);
|
14
16
|
const [names, setNames] = useState<string[]>([]);
|
15
17
|
const modalRef = useRef<any[]>([]);
|
16
18
|
|
19
|
+
console.log("OPEN MODAL", names, data, modalList);
|
17
20
|
useEffect(() => {
|
18
21
|
const handleOpenModal = (name: string, data: TData) => {
|
19
22
|
setData((prev: TData[]) => [...prev, data]);
|
@@ -68,6 +71,7 @@ const ModalProvider = ({ CustomComponent }: ModalProviderProps) => {
|
|
68
71
|
});
|
69
72
|
|
70
73
|
const handleCloseModal = (index: number, e: any) => {
|
74
|
+
console.log(index);
|
71
75
|
if (
|
72
76
|
modalRef.current[index] &&
|
73
77
|
!modalRef.current[index].contains(e.target)
|
@@ -86,27 +90,22 @@ const ModalProvider = ({ CustomComponent }: ModalProviderProps) => {
|
|
86
90
|
const Modal = Component;
|
87
91
|
|
88
92
|
return (
|
89
|
-
|
90
|
-
{
|
91
|
-
|
92
|
-
|
93
|
+
<div
|
94
|
+
key={i}
|
95
|
+
onClick={(e) => {
|
96
|
+
handleCloseModal(i, e);
|
97
|
+
}}
|
98
|
+
>
|
99
|
+
<div className="backdrop_modal_manager">
|
93
100
|
<div
|
94
|
-
|
95
|
-
|
96
|
-
onClick={(e) => {
|
97
|
-
handleCloseModal(i, e);
|
101
|
+
ref={(ref) => {
|
102
|
+
refReducer(i, ref);
|
98
103
|
}}
|
99
104
|
>
|
100
|
-
<
|
101
|
-
ref={(ref) => {
|
102
|
-
refReducer(i, ref);
|
103
|
-
}}
|
104
|
-
>
|
105
|
-
<Modal key={i} {...data[i]} />
|
106
|
-
</div>
|
105
|
+
<Modal key={i} {...data[i]} />
|
107
106
|
</div>
|
108
|
-
|
109
|
-
|
107
|
+
</div>
|
108
|
+
</div>
|
110
109
|
);
|
111
110
|
})
|
112
111
|
);
|
package/src/index.d.ts
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
import Manager from "./utils/
|
2
|
-
import modal from "./utils/
|
1
|
+
import Manager from "./utils/Manager";
|
2
|
+
import modal from "./utils/ModalManager";
|
3
3
|
import ModalProvider from "./components/modal-provider";
|
4
|
-
|
5
|
-
import { setModalActions } from "./utils/config/modal-actions";
|
6
|
-
export { Manager, modal, ModalProvider, setModalList, setModalActions };
|
4
|
+
export { Manager, modal, ModalProvider };
|
package/src/index.js
CHANGED
@@ -3,14 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.
|
7
|
-
var Manager_1 = __importDefault(require("./utils/
|
6
|
+
exports.ModalProvider = exports.modal = exports.Manager = void 0;
|
7
|
+
var Manager_1 = __importDefault(require("./utils/Manager"));
|
8
8
|
exports.Manager = Manager_1.default;
|
9
|
-
var ModalManager_1 = __importDefault(require("./utils/
|
9
|
+
var ModalManager_1 = __importDefault(require("./utils/ModalManager"));
|
10
10
|
exports.modal = ModalManager_1.default;
|
11
11
|
var modal_provider_1 = __importDefault(require("./components/modal-provider"));
|
12
12
|
exports.ModalProvider = modal_provider_1.default;
|
13
|
-
var modal_list_1 = require("./utils/config/modal-list");
|
14
|
-
Object.defineProperty(exports, "setModalList", { enumerable: true, get: function () { return modal_list_1.setModalList; } });
|
15
|
-
var modal_actions_1 = require("./utils/config/modal-actions");
|
16
|
-
Object.defineProperty(exports, "setModalActions", { enumerable: true, get: function () { return modal_actions_1.setModalActions; } });
|
package/src/index.ts
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
import Manager from "./utils/
|
2
|
-
import modal from "./utils/
|
1
|
+
import Manager from "./utils/Manager";
|
2
|
+
import modal from "./utils/ModalManager";
|
3
3
|
import ModalProvider from "./components/modal-provider";
|
4
|
-
import { setModalList } from "./utils/config/modal-list";
|
5
|
-
import { setModalActions } from "./utils/config/modal-actions";
|
6
4
|
|
7
|
-
export { Manager, modal, ModalProvider
|
5
|
+
export { Manager, modal, ModalProvider };
|
@@ -9,8 +9,5 @@ declare class Manager {
|
|
9
9
|
constructor();
|
10
10
|
addEventListener(event: string, listener: (...args: any[]) => void): void;
|
11
11
|
removeEventListener(event: string, listener: (...args: any[]) => void): void;
|
12
|
-
emitChange(): void;
|
13
|
-
close(...closeList: number[]): void;
|
14
|
-
emitClose<T>(closeList?: T): void;
|
15
12
|
}
|
16
13
|
export default Manager;
|
@@ -1,10 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
var events_1 = require("events");
|
4
|
-
var constants = {
|
5
|
-
CHANGE: "change",
|
6
|
-
CLOSE: "close",
|
7
|
-
};
|
8
4
|
var Manager = /** @class */ (function () {
|
9
5
|
function Manager() {
|
10
6
|
this.name = "";
|
@@ -17,21 +13,6 @@ var Manager = /** @class */ (function () {
|
|
17
13
|
Manager.prototype.removeEventListener = function (event, listener) {
|
18
14
|
this.emitter.removeListener(event, listener);
|
19
15
|
};
|
20
|
-
Manager.prototype.emitChange = function () {
|
21
|
-
this.emitter.emit(constants.CHANGE, this.name, this.data);
|
22
|
-
};
|
23
|
-
Manager.prototype.close = function () {
|
24
|
-
var closeList = [];
|
25
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
26
|
-
closeList[_i] = arguments[_i];
|
27
|
-
}
|
28
|
-
this.name = null;
|
29
|
-
this.data = {};
|
30
|
-
this.emitClose(closeList);
|
31
|
-
};
|
32
|
-
Manager.prototype.emitClose = function (closeList) {
|
33
|
-
this.emitter.emit(constants.CLOSE, closeList);
|
34
|
-
};
|
35
16
|
return Manager;
|
36
17
|
}());
|
37
18
|
exports.default = Manager;
|
@@ -1,10 +1,5 @@
|
|
1
1
|
import { EventEmitter } from "events";
|
2
2
|
|
3
|
-
const constants = {
|
4
|
-
CHANGE: "change",
|
5
|
-
CLOSE: "close",
|
6
|
-
};
|
7
|
-
|
8
3
|
type TData = {
|
9
4
|
[key: string]: any;
|
10
5
|
};
|
@@ -27,17 +22,6 @@ class Manager {
|
|
27
22
|
removeEventListener(event: string, listener: (...args: any[]) => void) {
|
28
23
|
this.emitter.removeListener(event, listener);
|
29
24
|
}
|
30
|
-
emitChange() {
|
31
|
-
this.emitter.emit(constants.CHANGE, this.name, this.data);
|
32
|
-
}
|
33
|
-
close(...closeList: number[]) {
|
34
|
-
this.name = null;
|
35
|
-
this.data = {};
|
36
|
-
this.emitClose(closeList);
|
37
|
-
}
|
38
|
-
emitClose<T>(closeList?: T) {
|
39
|
-
this.emitter.emit(constants.CLOSE, closeList);
|
40
|
-
}
|
41
25
|
}
|
42
26
|
|
43
27
|
export default Manager;
|
@@ -32,13 +32,13 @@ var ModalManager = /** @class */ (function (_super) {
|
|
32
32
|
ModalManager.prototype.create = function (name, data) {
|
33
33
|
this.name = name;
|
34
34
|
this.data = data;
|
35
|
-
this.
|
35
|
+
this.emitter.emit(constants.CHANGE, this.name, this.data);
|
36
36
|
};
|
37
37
|
ModalManager.prototype.call = function (name, data) {
|
38
38
|
if (data === void 0) { data = {}; }
|
39
39
|
this.create(name, { modalId: (0, uuid_1.v4)(), data: data });
|
40
40
|
};
|
41
|
-
ModalManager.prototype.
|
41
|
+
ModalManager.prototype.close = function (position) {
|
42
42
|
this.emitter.emit(constants.CLOSE, position);
|
43
43
|
};
|
44
44
|
return ModalManager;
|
@@ -14,14 +14,14 @@ class ModalManager extends Manager {
|
|
14
14
|
create(name: string, data: { [key: string]: any }) {
|
15
15
|
this.name = name;
|
16
16
|
this.data = data;
|
17
|
-
this.
|
17
|
+
this.emitter.emit(constants.CHANGE, this.name, this.data);
|
18
18
|
}
|
19
19
|
|
20
20
|
call(name: string, data: any = {}) {
|
21
21
|
this.create(name, { modalId: uuidv4(), data });
|
22
22
|
}
|
23
23
|
|
24
|
-
|
24
|
+
close<T>(position?: T) {
|
25
25
|
this.emitter.emit(constants.CLOSE, position);
|
26
26
|
}
|
27
27
|
}
|
@@ -1,20 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
3
|
-
__assign = Object.assign || function(t) {
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5
|
-
s = arguments[i];
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7
|
-
t[p] = s[p];
|
8
|
-
}
|
9
|
-
return t;
|
10
|
-
};
|
11
|
-
return __assign.apply(this, arguments);
|
12
|
-
};
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
14
|
-
exports.setModalActions = void 0;
|
15
|
-
var modalActions = {};
|
16
|
-
var setModalActions = function (actionList) {
|
17
|
-
modalActions = __assign(__assign({}, modalActions), actionList);
|
18
|
-
};
|
19
|
-
exports.setModalActions = setModalActions;
|
20
|
-
exports.default = modalActions;
|
@@ -1,20 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
3
|
-
__assign = Object.assign || function(t) {
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5
|
-
s = arguments[i];
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7
|
-
t[p] = s[p];
|
8
|
-
}
|
9
|
-
return t;
|
10
|
-
};
|
11
|
-
return __assign.apply(this, arguments);
|
12
|
-
};
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
14
|
-
exports.setModalList = void 0;
|
15
|
-
var modalList = {};
|
16
|
-
var setModalList = function (_modalList) {
|
17
|
-
modalList = __assign(__assign({}, modalList), _modalList);
|
18
|
-
};
|
19
|
-
exports.setModalList = setModalList;
|
20
|
-
exports.default = modalList;
|