@4i/modal-manager 1.0.42 → 1.0.50
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
@@ -6,10 +6,14 @@ interface ModalProviderProps {
|
|
6
6
|
modalList: any;
|
7
7
|
isOverflow?: boolean;
|
8
8
|
className?: string;
|
9
|
+
isHaveBackdrop?: boolean;
|
10
|
+
isCloseOnBackdropClick?: boolean;
|
11
|
+
onModalClose?: (modalName: string | string[]) => void;
|
12
|
+
onModalOpen?: (modalName: string) => void;
|
9
13
|
onModalStateChange?: (modalState: boolean, data: TData[], names: string[]) => void;
|
10
14
|
}
|
11
15
|
type TData = {
|
12
16
|
[key: string]: any;
|
13
17
|
};
|
14
|
-
declare const ModalProvider: ({ modalList, isOverflow, className, onModalStateChange, }: ModalProviderProps) => false | React.JSX.Element[];
|
18
|
+
declare const ModalProvider: ({ modalList, isOverflow, className, onModalStateChange, onModalClose, onModalOpen, isHaveBackdrop, isCloseOnBackdropClick, }: ModalProviderProps) => false | React.JSX.Element[];
|
15
19
|
export default ModalProvider;
|
@@ -50,9 +50,9 @@ 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 modalList = _a.modalList, isOverflow = _a.isOverflow, className = _a.className, onModalStateChange = _a.onModalStateChange;
|
54
|
-
var
|
55
|
-
var
|
53
|
+
var modalList = _a.modalList, isOverflow = _a.isOverflow, className = _a.className, onModalStateChange = _a.onModalStateChange, onModalClose = _a.onModalClose, onModalOpen = _a.onModalOpen, _b = _a.isHaveBackdrop, isHaveBackdrop = _b === void 0 ? true : _b, _c = _a.isCloseOnBackdropClick, isCloseOnBackdropClick = _c === void 0 ? true : _c;
|
54
|
+
var _d = (0, react_1.useState)([]), data = _d[0], setData = _d[1];
|
55
|
+
var _e = (0, react_1.useState)([]), names = _e[0], setNames = _e[1];
|
56
56
|
var modalRef = (0, react_1.useRef)([]);
|
57
57
|
(0, react_1.useEffect)(function () {
|
58
58
|
if (!onModalStateChange)
|
@@ -64,6 +64,9 @@ var ModalProvider = function (_a) {
|
|
64
64
|
var handleOpenModal = function (name, data) {
|
65
65
|
setData(function (prev) { return __spreadArray(__spreadArray([], prev, true), [data], false); });
|
66
66
|
setNames(function (prev) { return __spreadArray(__spreadArray([], prev, true), [name], false); });
|
67
|
+
if (onModalOpen) {
|
68
|
+
onModalOpen(name);
|
69
|
+
}
|
67
70
|
if (isOverflow) {
|
68
71
|
if (typeof document === "undefined")
|
69
72
|
return;
|
@@ -77,12 +80,18 @@ var ModalProvider = function (_a) {
|
|
77
80
|
}
|
78
81
|
}
|
79
82
|
if (position === "all") {
|
83
|
+
if (onModalClose) {
|
84
|
+
onModalClose("all");
|
85
|
+
}
|
80
86
|
setData([]);
|
81
87
|
setNames([]);
|
82
88
|
return;
|
83
89
|
}
|
84
90
|
if (position === -1) {
|
85
91
|
// remove last
|
92
|
+
if (onModalClose) {
|
93
|
+
onModalClose(names[names.length - 1]);
|
94
|
+
}
|
86
95
|
setData(function (prev) {
|
87
96
|
return prev.filter(function (_, index) { return index !== prev.length - 1; });
|
88
97
|
});
|
@@ -93,6 +102,9 @@ var ModalProvider = function (_a) {
|
|
93
102
|
}
|
94
103
|
if (position === 0) {
|
95
104
|
// remove first
|
105
|
+
if (onModalClose) {
|
106
|
+
onModalClose(names[0]);
|
107
|
+
}
|
96
108
|
setData(function (prev) { return prev.filter(function (_, index) { return index !== 0; }); });
|
97
109
|
setNames(function (prev) { return prev.filter(function (_, index) { return index !== 0; }); });
|
98
110
|
return;
|
@@ -117,6 +129,8 @@ var ModalProvider = function (_a) {
|
|
117
129
|
return Component;
|
118
130
|
});
|
119
131
|
var handleCloseModal = function (index, e) {
|
132
|
+
if (!isCloseOnBackdropClick)
|
133
|
+
return;
|
120
134
|
if (modalRef.current[index] &&
|
121
135
|
!modalRef.current[index].contains(e.target)) {
|
122
136
|
ModalManager_2.default.close(index);
|
@@ -128,10 +142,10 @@ var ModalProvider = function (_a) {
|
|
128
142
|
return (data.length !== 0 &&
|
129
143
|
data.map(function (item, i) {
|
130
144
|
var Modal = activeModals[i] || (function () { return react_1.default.createElement(react_1.default.Fragment, null); });
|
131
|
-
return (react_1.default.createElement("div", { key: item.modalId,
|
132
|
-
handleCloseModal(i, e);
|
145
|
+
return (react_1.default.createElement("div", { key: item.modalId, onMouseDown: function (e) {
|
146
|
+
isCloseOnBackdropClick && handleCloseModal(i, e);
|
133
147
|
} },
|
134
|
-
react_1.default.createElement("div", { className: "".concat(className, " backdrop_modal_manager") },
|
148
|
+
react_1.default.createElement("div", { className: "".concat(className, " backdrop_modal_manager ").concat(isHaveBackdrop && isCloseOnBackdropClick && "backdrop") },
|
135
149
|
react_1.default.createElement("div", { ref: function (ref) {
|
136
150
|
refReducer(i, ref);
|
137
151
|
} },
|
@@ -8,6 +8,10 @@ interface ModalProviderProps {
|
|
8
8
|
modalList: any;
|
9
9
|
isOverflow?: boolean;
|
10
10
|
className?: string;
|
11
|
+
isHaveBackdrop?: boolean;
|
12
|
+
isCloseOnBackdropClick?: boolean;
|
13
|
+
onModalClose?: (modalName: string | string[]) => void;
|
14
|
+
onModalOpen?: (modalName: string) => void;
|
11
15
|
onModalStateChange?: (
|
12
16
|
modalState: boolean,
|
13
17
|
data: TData[],
|
@@ -22,6 +26,10 @@ const ModalProvider = ({
|
|
22
26
|
isOverflow,
|
23
27
|
className,
|
24
28
|
onModalStateChange,
|
29
|
+
onModalClose,
|
30
|
+
onModalOpen,
|
31
|
+
isHaveBackdrop = true,
|
32
|
+
isCloseOnBackdropClick = true,
|
25
33
|
}: ModalProviderProps) => {
|
26
34
|
const [data, setData] = useState<TData[]>([]);
|
27
35
|
const [names, setNames] = useState<string[]>([]);
|
@@ -37,6 +45,9 @@ const ModalProvider = ({
|
|
37
45
|
const handleOpenModal = (name: string, data: TData) => {
|
38
46
|
setData((prev: TData[]) => [...prev, data]);
|
39
47
|
setNames((prev: string[]) => [...prev, name]);
|
48
|
+
if (onModalOpen) {
|
49
|
+
onModalOpen(name);
|
50
|
+
}
|
40
51
|
|
41
52
|
if (isOverflow) {
|
42
53
|
if (typeof document === "undefined") return;
|
@@ -52,6 +63,9 @@ const ModalProvider = ({
|
|
52
63
|
}
|
53
64
|
|
54
65
|
if (position === "all") {
|
66
|
+
if (onModalClose) {
|
67
|
+
onModalClose("all");
|
68
|
+
}
|
55
69
|
setData([]);
|
56
70
|
setNames([]);
|
57
71
|
return;
|
@@ -59,6 +73,9 @@ const ModalProvider = ({
|
|
59
73
|
|
60
74
|
if (position === -1) {
|
61
75
|
// remove last
|
76
|
+
if (onModalClose) {
|
77
|
+
onModalClose(names[names.length - 1]);
|
78
|
+
}
|
62
79
|
setData((prev: TData[]) =>
|
63
80
|
prev.filter((_, index) => index !== prev.length - 1)
|
64
81
|
);
|
@@ -70,6 +87,9 @@ const ModalProvider = ({
|
|
70
87
|
|
71
88
|
if (position === 0) {
|
72
89
|
// remove first
|
90
|
+
if (onModalClose) {
|
91
|
+
onModalClose(names[0]);
|
92
|
+
}
|
73
93
|
setData((prev: TData[]) => prev.filter((_, index) => index !== 0));
|
74
94
|
setNames((prev: string[]) => prev.filter((_, index) => index !== 0));
|
75
95
|
return;
|
@@ -98,6 +118,7 @@ const ModalProvider = ({
|
|
98
118
|
});
|
99
119
|
|
100
120
|
const handleCloseModal = (index: number, e: any) => {
|
121
|
+
if (!isCloseOnBackdropClick) return;
|
101
122
|
if (
|
102
123
|
modalRef.current[index] &&
|
103
124
|
!modalRef.current[index].contains(e.target)
|
@@ -118,11 +139,15 @@ const ModalProvider = ({
|
|
118
139
|
return (
|
119
140
|
<div
|
120
141
|
key={item.modalId}
|
121
|
-
|
122
|
-
handleCloseModal(i, e);
|
142
|
+
onMouseDown={(e) => {
|
143
|
+
isCloseOnBackdropClick && handleCloseModal(i, e);
|
123
144
|
}}
|
124
145
|
>
|
125
|
-
<div
|
146
|
+
<div
|
147
|
+
className={`${className} backdrop_modal_manager ${
|
148
|
+
isHaveBackdrop && isCloseOnBackdropClick && "backdrop"
|
149
|
+
}`}
|
150
|
+
>
|
126
151
|
<div
|
127
152
|
ref={(ref) => {
|
128
153
|
refReducer(i, ref);
|
package/src/styles.css
CHANGED
@@ -27,13 +27,16 @@
|
|
27
27
|
bottom: 0;
|
28
28
|
width: 100%;
|
29
29
|
height: 100%;
|
30
|
-
background-color: rgba(0, 0, 0, 0.5);
|
31
30
|
position: fixed;
|
32
31
|
z-index: 1000;
|
33
|
-
opacity: 0;
|
34
32
|
animation: bg_opacity 150ms ease-in-out forwards;
|
35
33
|
}
|
36
34
|
|
35
|
+
.backdrop {
|
36
|
+
background-color: rgba(0, 0, 0, 0.5);
|
37
|
+
opacity: 0;
|
38
|
+
}
|
39
|
+
|
37
40
|
.backdrop_modal_manager > div {
|
38
41
|
animation: bg_opacity_scale 150ms ease-in-out forwards;
|
39
42
|
}
|