@4i/modal-manager 1.0.115 → 1.0.116

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/readme.md +111 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4i/modal-manager",
3
- "version": "1.0.115",
3
+ "version": "1.0.116",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
package/readme.md CHANGED
@@ -21,10 +21,30 @@ npm install @4i/modal-manager
21
21
 
22
22
  Call a modal by its action name and pass props to it.
23
23
 
24
- ##### .close()
24
+ ##### .close('all')
25
25
 
26
26
  Close all modals.
27
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
+
28
48
  #### Define Modal Actions:
29
49
 
30
50
  In your project, define modal actions as keys in the modalAction object. Each key represents a specific modal or UI element.
@@ -66,11 +86,98 @@ If desired, you can inherit from the Manager class to create your own classes fo
66
86
  ```javascript
67
87
  import { Manager } from "@4i/modal-manager";
68
88
 
69
- class CustomManager extends Manager {
70
- // 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
+ }
71
120
  }
72
121
 
73
- const customManager = new CustomManager();
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
+
133
+ const BottomModalProvider = ({ modalList }) => {
134
+ const [data, setData] = useState({
135
+ data: null,
136
+ });
137
+ const [name, setName] = useState < string > null;
138
+ const modalRef = useRef < HTMLDivElement > null;
139
+
140
+ useEffect(() => {
141
+ function handleOpenModal(name, data) {
142
+ console.log("LOG", name);
143
+ setName(name);
144
+ setData(data);
145
+ }
146
+
147
+ function handleClose() {
148
+ setName(null);
149
+ setData(null);
150
+ }
151
+
152
+ bottomModal.addEventListener(constants.OPEN, handleOpenModal);
153
+ bottomModal.addEventListener(constants.CLOSE, handleClose);
154
+
155
+ return () => {
156
+ bottomModal.removeEventListener(constants.OPEN, handleOpenModal);
157
+ bottomModal.removeEventListener(constants.CLOSE, handleClose);
158
+ };
159
+ }, []);
160
+
161
+ const handleCloseModal = (e: any) => {
162
+ if (modalRef.current && !modalRef.current.contains(e.target)) {
163
+ bottomModal.close();
164
+ }
165
+ };
166
+
167
+ const Widget = modalList[name];
168
+
169
+ return (
170
+ <div>
171
+ {name && Widget && (
172
+ <div className="fixed z-[1000] left-0 right-0 bottom-0 animate-fromBottom">
173
+ <Widget {...data.data} />
174
+ </div>
175
+ )}
176
+ </div>
177
+ );
178
+ };
179
+
180
+ export default memo(BottomModalProvider);
74
181
  ```
75
182
 
76
183
  ### index.js