@4i/modal-manager 1.0.115 → 1.0.117
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 +121 -4
package/package.json
CHANGED
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,108 @@ 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
|
-
|
70
|
-
|
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
|
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);
|
74
191
|
```
|
75
192
|
|
76
193
|
### index.js
|