@4i/modal-manager 1.0.10 → 1.0.13

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