@4i/modal-manager 1.0.10 → 1.0.12

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4i/modal-manager",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
@@ -14,7 +14,9 @@
14
14
  },
15
15
  "keywords": [
16
16
  "modal",
17
- "manager"
17
+ "manager",
18
+ "react",
19
+ "typescript"
18
20
  ],
19
21
  "author": "Toni",
20
22
  "license": "ISC",
@@ -22,10 +24,7 @@
22
24
  "url": "https://github.com/tonichiga/modal-manager/issues"
23
25
  },
24
26
  "homepage": "https://github.com/tonichiga/modal-manager#readme",
25
- "dependencies": {
26
- "react": "^18.2.0",
27
- "uuid": "^9.0.1"
28
- },
27
+ "dependencies": {},
29
28
  "devDependencies": {
30
29
  "@babel/cli": "^7.23.4",
31
30
  "@babel/core": "^7.23.5",
package/readme.md CHANGED
@@ -15,6 +15,36 @@ npm install @4i/modal-manager
15
15
 
16
16
  ## Usage
17
17
 
18
+ #### Instance methods:
19
+
20
+ ##### .call(action, props)
21
+
22
+ Call a modal by its action name and pass props to it.
23
+
24
+ ##### .close('all')
25
+
26
+ Close all modals.
27
+
28
+ ##### .close(-1)
29
+
30
+ Close last modals.
31
+
32
+ ##### .close(0)
33
+
34
+ Close first modals.
35
+
36
+ ##### .close() (default)
37
+
38
+ Close last modals.
39
+
40
+ ##### .addEventListener(event, callback)
41
+
42
+ Add an event listener to the modal manager.
43
+
44
+ ##### .removeEventListener(event, callback)
45
+
46
+ Remove an event listener from the modal manager.
47
+
18
48
  #### Define Modal Actions:
19
49
 
20
50
  In your project, define modal actions as keys in the modalAction object. Each key represents a specific modal or UI element.
@@ -53,17 +83,112 @@ modal.call(modalAction.MODAL_PROMPT, {
53
83
 
54
84
  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
85
 
56
- ````javascript
57
-
58
86
  ```javascript
59
87
  import { Manager } from "@4i/modal-manager";
60
88
 
61
- class CustomManager extends Manager {
62
- // Custom methods and logic here
89
+ import { Manager } from "@4i/modal-manager";
90
+ import { v4 as uuidv4 } from "uuid";
91
+
92
+ export const constants = {
93
+ OPEN: "open-bottom-modal",
94
+ CLOSE: "close-bottom-modal",
95
+ };
96
+
97
+ class CustomModalManager extends Manager {
98
+ name: string;
99
+ data: any;
100
+
101
+ constructor() {
102
+ super();
103
+ this.name = "";
104
+ this.data = {};
105
+ }
106
+
107
+ create(name: string, data) {
108
+ this.name = name;
109
+ this.data = data;
110
+ this.emitter.emit(constants.OPEN, this.name, this.data);
111
+ }
112
+
113
+ call(name: string, data: any = {}) {
114
+ this.create(name, { modalId: uuidv4(), data });
115
+ }
116
+
117
+ close(position?: number | string) {
118
+ this.emitter.emit(constants.CLOSE, position);
119
+ }
63
120
  }
64
121
 
65
- const customManager = new CustomManager();
66
- ````
122
+ const customModalManager = new CustomModalManager();
123
+
124
+ export default customModalManager;
125
+ ```
126
+
127
+ #### Custom Provider
128
+
129
+ ```javascript
130
+ import { memo, useEffect, useRef, useState } from "react";
131
+ import bottomModal, { constants } from "../../service/BottomModal";
132
+ import widgets from "../../config/modal/modal-list";
133
+
134
+ const initialData = {
135
+ data: null,
136
+ };
137
+
138
+ const BottomModalProvider = () => {
139
+ const [data, setData] = useState(initialData);
140
+ const [name, setName] = useState < string > null;
141
+ const modalRef = useRef < HTMLDivElement > null;
142
+
143
+ useEffect(() => {
144
+ function handleOpenModal(name, data) {
145
+ console.log("LOG", name);
146
+ setName(name);
147
+ setData(data);
148
+ }
149
+
150
+ function handleClose() {
151
+ console.log("LOG", "close");
152
+ setName(null);
153
+ setData(initialData);
154
+ }
155
+
156
+ bottomModal.addEventListener(constants.OPEN, handleOpenModal);
157
+ bottomModal.addEventListener(constants.CLOSE, handleClose);
158
+
159
+ return () => {
160
+ bottomModal.removeEventListener(constants.OPEN, handleOpenModal);
161
+ bottomModal.removeEventListener(constants.CLOSE, handleClose);
162
+ };
163
+ }, []);
164
+
165
+ const handleCloseModal = (e: any) => {
166
+ if (modalRef.current && !modalRef.current.contains(e.target)) {
167
+ console.log("LOG", "close");
168
+ bottomModal.close();
169
+ }
170
+ };
171
+
172
+ const Widget = widgets[name];
173
+
174
+ return (
175
+ <>
176
+ {name && Widget && (
177
+ <div
178
+ onClick={handleCloseModal}
179
+ className="animate-backdrop fixed z-[1000] h-full w-full overflow-hidden flex items-end"
180
+ >
181
+ <div className="animate-fromBottom w-full" ref={modalRef}>
182
+ <Widget {...data.data} />
183
+ </div>
184
+ </div>
185
+ )}
186
+ </>
187
+ );
188
+ };
189
+
190
+ export default memo(BottomModalProvider);
191
+ ```
67
192
 
68
193
  ### index.js
69
194
 
@@ -133,14 +258,19 @@ export default App;
133
258
 
134
259
  ```javascript
135
260
  import React from "react";
261
+ import { modal } from "@4i/modal-manager";
136
262
 
137
263
  // Get props
138
264
  const ModalPrompts = ({ title, content }) => {
265
+ const handleClose = () => {
266
+ modal.close();
267
+ };
268
+
139
269
  return (
140
270
  <div className="w-[400px] h-[300px] bg-slate-50 p-[24px] flex flex-col justify-center items-center">
141
271
  <h1>{title}</h1>
142
272
  <p>{content}</p>
143
- <button>Close</button>
273
+ <button onClick={handleClose}>Close</button>
144
274
  </div>
145
275
  );
146
276
  };
@@ -1,10 +1,9 @@
1
1
  import React from "react";
2
- type ModalList = {
2
+ export type ModalList = {
3
3
  [key: string]: React.ComponentType;
4
4
  };
5
5
  interface ModalProviderProps {
6
- CustomComponent?: React.ComponentType;
7
- modalList: ModalList;
6
+ modalList: any;
8
7
  }
9
- declare const ModalProvider: ({ CustomComponent, modalList }: ModalProviderProps) => false | React.JSX.Element[];
8
+ declare const ModalProvider: ({ modalList }: ModalProviderProps) => false | React.JSX.Element[];
10
9
  export default ModalProvider;
@@ -50,11 +50,10 @@ var react_1 = __importStar(require("react"));
50
50
  var ModalManager_1 = __importDefault(require("../utils/ModalManager"));
51
51
  var ModalManager_2 = __importDefault(require("../utils/ModalManager"));
52
52
  var ModalProvider = function (_a) {
53
- var CustomComponent = _a.CustomComponent, modalList = _a.modalList;
53
+ var modalList = _a.modalList;
54
54
  var _b = (0, react_1.useState)([]), data = _b[0], setData = _b[1];
55
55
  var _c = (0, react_1.useState)([]), names = _c[0], setNames = _c[1];
56
56
  var modalRef = (0, react_1.useRef)([]);
57
- console.log("OPEN MODAL", names, data, modalList);
58
57
  (0, react_1.useEffect)(function () {
59
58
  var handleOpenModal = function (name, data) {
60
59
  setData(function (prev) { return __spreadArray(__spreadArray([], prev, true), [data], false); });
@@ -111,17 +110,17 @@ var ModalProvider = function (_a) {
111
110
  var refReducer = function (index, value) {
112
111
  modalRef.current[index] = value;
113
112
  };
114
- return (activeModals.length !== 0 &&
115
- activeModals.map(function (Component, i) {
116
- var Modal = Component;
117
- return (react_1.default.createElement("div", { key: i, onClick: function (e) {
113
+ return (data.length !== 0 &&
114
+ data.map(function (item, i) {
115
+ var Modal = activeModals[i] || (function () { return react_1.default.createElement(react_1.default.Fragment, null); });
116
+ return (react_1.default.createElement("div", { key: item.modalId, onClick: function (e) {
118
117
  handleCloseModal(i, e);
119
118
  } },
120
119
  react_1.default.createElement("div", { className: "backdrop_modal_manager" },
121
120
  react_1.default.createElement("div", { ref: function (ref) {
122
121
  refReducer(i, ref);
123
122
  } },
124
- react_1.default.createElement(Modal, __assign({ key: i }, data[i]))))));
123
+ react_1.default.createElement(Modal, __assign({}, item.data))))));
125
124
  }));
126
125
  };
127
126
  exports.default = ModalProvider;
@@ -2,21 +2,19 @@ import React, { useEffect, useRef, useState } from "react";
2
2
  import manager from "../utils/ModalManager";
3
3
  import modal from "../utils/ModalManager";
4
4
 
5
- type ModalList = { [key: string]: React.ComponentType };
5
+ export type ModalList = { [key: string]: React.ComponentType };
6
6
 
7
7
  interface ModalProviderProps {
8
- CustomComponent?: React.ComponentType;
9
- modalList: ModalList;
8
+ modalList: any;
10
9
  }
11
10
 
12
11
  type TData = { [key: string]: any };
13
12
 
14
- const ModalProvider = ({ CustomComponent, modalList }: ModalProviderProps) => {
13
+ const ModalProvider = ({ modalList }: ModalProviderProps) => {
15
14
  const [data, setData] = useState<TData[]>([]);
16
15
  const [names, setNames] = useState<string[]>([]);
17
16
  const modalRef = useRef<any[]>([]);
18
17
 
19
- console.log("OPEN MODAL", names, data, modalList);
20
18
  useEffect(() => {
21
19
  const handleOpenModal = (name: string, data: TData) => {
22
20
  setData((prev: TData[]) => [...prev, data]);
@@ -85,13 +83,13 @@ const ModalProvider = ({ CustomComponent, modalList }: ModalProviderProps) => {
85
83
  };
86
84
 
87
85
  return (
88
- activeModals.length !== 0 &&
89
- activeModals.map((Component, i) => {
90
- const Modal = Component;
86
+ data.length !== 0 &&
87
+ data.map((item, i) => {
88
+ const Modal = activeModals[i] || (() => <></>);
91
89
 
92
90
  return (
93
91
  <div
94
- key={i}
92
+ key={item.modalId}
95
93
  onClick={(e) => {
96
94
  handleCloseModal(i, e);
97
95
  }}
@@ -102,7 +100,7 @@ const ModalProvider = ({ CustomComponent, modalList }: ModalProviderProps) => {
102
100
  refReducer(i, ref);
103
101
  }}
104
102
  >
105
- <Modal key={i} {...data[i]} />
103
+ <Modal {...item.data} />
106
104
  </div>
107
105
  </div>
108
106
  </div>
@@ -1,10 +1,11 @@
1
1
  import Manager from "./Manager";
2
2
  declare class ModalManager extends Manager {
3
3
  constructor();
4
- create(name: string, data: {
5
- [key: string]: any;
4
+ create<T>(name: string, payload: {
5
+ modalId: number;
6
+ data?: T;
6
7
  }): void;
7
- call(name: string, data?: any): void;
8
+ call<T>(name: string, data?: T): void;
8
9
  close<T>(position?: T): void;
9
10
  }
10
11
  declare const modal: ModalManager;
@@ -19,7 +19,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  var Manager_1 = __importDefault(require("./Manager"));
22
- var uuid_1 = require("uuid");
22
+ function uniqueID() {
23
+ return Math.floor(Math.random() * Date.now());
24
+ }
23
25
  var constants = {
24
26
  CHANGE: "change",
25
27
  CLOSE: "close",
@@ -29,14 +31,13 @@ var ModalManager = /** @class */ (function (_super) {
29
31
  function ModalManager() {
30
32
  return _super.call(this) || this;
31
33
  }
32
- ModalManager.prototype.create = function (name, data) {
34
+ ModalManager.prototype.create = function (name, payload) {
33
35
  this.name = name;
34
- this.data = data;
36
+ this.data = payload;
35
37
  this.emitter.emit(constants.CHANGE, this.name, this.data);
36
38
  };
37
39
  ModalManager.prototype.call = function (name, data) {
38
- if (data === void 0) { data = {}; }
39
- this.create(name, { modalId: (0, uuid_1.v4)(), data: data });
40
+ this.create(name, { modalId: uniqueID(), data: data });
40
41
  };
41
42
  ModalManager.prototype.close = function (position) {
42
43
  this.emitter.emit(constants.CLOSE, position);
@@ -1,5 +1,8 @@
1
1
  import Manager from "./Manager";
2
- import { v4 as uuidv4 } from "uuid";
2
+
3
+ function uniqueID() {
4
+ return Math.floor(Math.random() * Date.now());
5
+ }
3
6
 
4
7
  const constants = {
5
8
  CHANGE: "change",
@@ -11,14 +14,14 @@ class ModalManager extends Manager {
11
14
  super();
12
15
  }
13
16
 
14
- create(name: string, data: { [key: string]: any }) {
17
+ create<T>(name: string, payload: { modalId: number; data?: T }) {
15
18
  this.name = name;
16
- this.data = data;
19
+ this.data = payload;
17
20
  this.emitter.emit(constants.CHANGE, this.name, this.data);
18
21
  }
19
22
 
20
- call(name: string, data: any = {}) {
21
- this.create(name, { modalId: uuidv4(), data });
23
+ call<T>(name: string, data?: T) {
24
+ this.create<T>(name, { modalId: uniqueID(), data });
22
25
  }
23
26
 
24
27
  close<T>(position?: T) {