@4i/modal-manager 1.1.8 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4i/modal-manager",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
@@ -1,5 +1,4 @@
1
1
  import React from "react";
2
- import { ModalManager } from "../utils/ModalManager";
3
2
  export type ModalList = {
4
3
  [key: string]: React.ComponentType;
5
4
  };
@@ -8,11 +7,10 @@ interface ModalProviderProps {
8
7
  isOverflow?: boolean;
9
8
  className?: string;
10
9
  backdropClassName?: string;
11
- modalManager?: ModalManager;
12
10
  onModalStateChange?: (modalState: boolean, data: TData[], names: string[]) => void;
13
11
  }
14
12
  type TData = {
15
13
  [key: string]: any;
16
14
  };
17
- declare const ModalProvider: ({ modalList, isOverflow, className, backdropClassName, modalManager, onModalStateChange, }: ModalProviderProps) => React.JSX.Element;
15
+ declare const ModalProvider: ({ modalList, isOverflow, className, backdropClassName, onModalStateChange, }: ModalProviderProps) => React.JSX.Element;
18
16
  export default ModalProvider;
@@ -34,6 +34,42 @@ var __importStar = (this && this.__importStar) || function (mod) {
34
34
  __setModuleDefault(result, mod);
35
35
  return result;
36
36
  };
37
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
38
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
39
+ return new (P || (P = Promise))(function (resolve, reject) {
40
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
41
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
42
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
43
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
44
+ });
45
+ };
46
+ var __generator = (this && this.__generator) || function (thisArg, body) {
47
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
48
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
49
+ function verb(n) { return function (v) { return step([n, v]); }; }
50
+ function step(op) {
51
+ if (f) throw new TypeError("Generator is already executing.");
52
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
53
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
54
+ if (y = 0, t) op = [op[0] & 2, t.value];
55
+ switch (op[0]) {
56
+ case 0: case 1: t = op; break;
57
+ case 4: _.label++; return { value: op[1], done: false };
58
+ case 5: _.label++; y = op[1]; op = [0]; continue;
59
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
60
+ default:
61
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
62
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
63
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
64
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
65
+ if (t[2]) _.ops.pop();
66
+ _.trys.pop(); continue;
67
+ }
68
+ op = body.call(thisArg, _);
69
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
70
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
71
+ }
72
+ };
37
73
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
38
74
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
39
75
  if (ar || !(i in from)) {
@@ -47,11 +83,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
47
83
  var react_1 = __importStar(require("react"));
48
84
  var ModalManager_1 = __importStar(require("../utils/ModalManager"));
49
85
  var ModalProvider = function (_a) {
50
- var modalList = _a.modalList, isOverflow = _a.isOverflow, className = _a.className, backdropClassName = _a.backdropClassName, modalManager = _a.modalManager, onModalStateChange = _a.onModalStateChange;
86
+ var modalList = _a.modalList, isOverflow = _a.isOverflow, className = _a.className, backdropClassName = _a.backdropClassName, onModalStateChange = _a.onModalStateChange;
51
87
  var _b = (0, react_1.useState)([]), data = _b[0], setData = _b[1];
52
88
  var _c = (0, react_1.useState)([]), names = _c[0], setNames = _c[1];
53
89
  var modalRef = (0, react_1.useRef)([]);
54
- var m = modalManager !== null && modalManager !== void 0 ? modalManager : ModalManager_1.default;
90
+ var applyCloseStyles = function (index) {
91
+ return new Promise(function (resolve) {
92
+ var modal = document.querySelector("[data-index=\"".concat(index, "\"]"));
93
+ if (!modal)
94
+ return;
95
+ modal.classList.add("closing");
96
+ setTimeout(function () {
97
+ resolve(true);
98
+ }, 150);
99
+ });
100
+ };
55
101
  (0, react_1.useEffect)(function () {
56
102
  if (!onModalStateChange)
57
103
  return;
@@ -69,46 +115,54 @@ var ModalProvider = function (_a) {
69
115
  document.body.style.overflow = "hidden";
70
116
  }
71
117
  };
72
- var handleClose = function (position) {
73
- if (isOverflow) {
74
- if (typeof document !== "undefined") {
75
- document.body.style.overflow = "";
118
+ var handleClose = function (position) { return __awaiter(void 0, void 0, void 0, function () {
119
+ return __generator(this, function (_a) {
120
+ switch (_a.label) {
121
+ case 0: return [4 /*yield*/, applyCloseStyles(position)];
122
+ case 1:
123
+ _a.sent();
124
+ if (isOverflow) {
125
+ if (typeof document !== "undefined") {
126
+ document.body.style.overflow = "";
127
+ }
128
+ }
129
+ if (position === "all") {
130
+ setData([]);
131
+ setNames([]);
132
+ return [2 /*return*/];
133
+ }
134
+ if (position === -1) {
135
+ // remove last
136
+ setData(function (prev) {
137
+ return prev.filter(function (_, index) { return index !== prev.length - 1; });
138
+ });
139
+ setNames(function (prev) {
140
+ return prev.filter(function (_, index) { return index !== prev.length - 1; });
141
+ });
142
+ return [2 /*return*/];
143
+ }
144
+ if (position === 0) {
145
+ // remove first
146
+ setData(function (prev) { return prev.filter(function (_, index) { return index !== 0; }); });
147
+ setNames(function (prev) { return prev.filter(function (_, index) { return index !== 0; }); });
148
+ return [2 /*return*/];
149
+ }
150
+ // remove position index
151
+ setData(function (prev) {
152
+ return prev.filter(function (_, index) { return index !== prev.length - 1; });
153
+ });
154
+ setNames(function (prev) {
155
+ return prev.filter(function (_, index) { return index !== prev.length - 1; });
156
+ });
157
+ return [2 /*return*/];
76
158
  }
77
- }
78
- if (position === "all") {
79
- setData([]);
80
- setNames([]);
81
- return;
82
- }
83
- if (position === -1) {
84
- // remove last
85
- setData(function (prev) {
86
- return prev.filter(function (_, index) { return index !== prev.length - 1; });
87
- });
88
- setNames(function (prev) {
89
- return prev.filter(function (_, index) { return index !== prev.length - 1; });
90
- });
91
- return;
92
- }
93
- if (position === 0) {
94
- // remove first
95
- setData(function (prev) { return prev.filter(function (_, index) { return index !== 0; }); });
96
- setNames(function (prev) { return prev.filter(function (_, index) { return index !== 0; }); });
97
- return;
98
- }
99
- // remove position index
100
- setData(function (prev) {
101
- return prev.filter(function (_, index) { return index !== prev.length - 1; });
102
159
  });
103
- setNames(function (prev) {
104
- return prev.filter(function (_, index) { return index !== prev.length - 1; });
105
- });
106
- };
107
- m.addEventListener(ModalManager_1.constants.CHANGE, handleOpenModal);
108
- m.addEventListener(ModalManager_1.constants.CLOSE, handleClose);
160
+ }); };
161
+ ModalManager_1.default.addEventListener(ModalManager_1.constants.CHANGE, handleOpenModal);
162
+ ModalManager_1.default.addEventListener(ModalManager_1.constants.CLOSE, handleClose);
109
163
  return function () {
110
- m.removeEventListener(ModalManager_1.constants.CHANGE, handleOpenModal);
111
- m.removeEventListener(ModalManager_1.constants.CLOSE, handleClose);
164
+ ModalManager_1.default.removeEventListener(ModalManager_1.constants.CHANGE, handleOpenModal);
165
+ ModalManager_1.default.removeEventListener(ModalManager_1.constants.CLOSE, handleClose);
112
166
  };
113
167
  // eslint-disable-next-line react-hooks/exhaustive-deps
114
168
  }, []);
@@ -117,7 +171,7 @@ var ModalProvider = function (_a) {
117
171
  return Component;
118
172
  });
119
173
  var handleCloseModal = function (index) {
120
- m.close(index);
174
+ ModalManager_1.default.close(index);
121
175
  };
122
176
  var refReducer = function (index, value) {
123
177
  modalRef.current[index] = value;
@@ -125,7 +179,7 @@ var ModalProvider = function (_a) {
125
179
  return (react_1.default.createElement(react_1.default.Fragment, null, data.length !== 0 &&
126
180
  data.map(function (item, i) {
127
181
  var Modal = activeModals[i] || (function () { return react_1.default.createElement(react_1.default.Fragment, null); });
128
- return (react_1.default.createElement("div", { key: item.modalId, className: "modal-manager backdrop_modal_manager ".concat(backdropClassName) },
182
+ return (react_1.default.createElement("div", { "data-index": i, key: item.modalId, className: "modal-manager backdrop_modal_manager ".concat(backdropClassName) },
129
183
  react_1.default.createElement("div", { onClick: function (e) {
130
184
  e.stopPropagation();
131
185
  handleCloseModal(i);
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
 
3
3
  import React, { useEffect, useRef, useState } from "react";
4
- import modal, { constants, ModalManager } from "../utils/ModalManager";
4
+ import modal, { constants } from "../utils/ModalManager";
5
5
 
6
6
  export type ModalList = { [key: string]: React.ComponentType };
7
7
 
@@ -10,7 +10,6 @@ interface ModalProviderProps {
10
10
  isOverflow?: boolean;
11
11
  className?: string;
12
12
  backdropClassName?: string;
13
- modalManager?: ModalManager;
14
13
  onModalStateChange?: (
15
14
  modalState: boolean,
16
15
  data: TData[],
@@ -25,13 +24,24 @@ const ModalProvider = ({
25
24
  isOverflow,
26
25
  className,
27
26
  backdropClassName,
28
- modalManager,
29
27
  onModalStateChange,
30
28
  }: ModalProviderProps) => {
31
29
  const [data, setData] = useState<TData[]>([]);
32
30
  const [names, setNames] = useState<string[]>([]);
33
31
  const modalRef = useRef<any[]>([]);
34
- const m = modalManager ?? modal;
32
+
33
+ const applyCloseStyles = (index: number) => {
34
+ return new Promise((resolve) => {
35
+ const modal = document.querySelector(
36
+ `[data-index="${index}"]`
37
+ ) as HTMLElement;
38
+ if (!modal) return;
39
+ modal.classList.add("closing");
40
+ setTimeout(() => {
41
+ resolve(true);
42
+ }, 150);
43
+ });
44
+ };
35
45
 
36
46
  useEffect(() => {
37
47
  if (!onModalStateChange) return;
@@ -51,7 +61,8 @@ const ModalProvider = ({
51
61
  }
52
62
  };
53
63
 
54
- const handleClose = (position: number | string) => {
64
+ const handleClose = async (position: number | string) => {
65
+ await applyCloseStyles(position as number);
55
66
  if (isOverflow) {
56
67
  if (typeof document !== "undefined") {
57
68
  document.body.style.overflow = "";
@@ -91,11 +102,11 @@ const ModalProvider = ({
91
102
  );
92
103
  };
93
104
 
94
- m.addEventListener(constants.CHANGE, handleOpenModal);
95
- m.addEventListener(constants.CLOSE, handleClose);
105
+ modal.addEventListener(constants.CHANGE, handleOpenModal);
106
+ modal.addEventListener(constants.CLOSE, handleClose);
96
107
  return () => {
97
- m.removeEventListener(constants.CHANGE, handleOpenModal);
98
- m.removeEventListener(constants.CLOSE, handleClose);
108
+ modal.removeEventListener(constants.CHANGE, handleOpenModal);
109
+ modal.removeEventListener(constants.CLOSE, handleClose);
99
110
  };
100
111
  // eslint-disable-next-line react-hooks/exhaustive-deps
101
112
  }, []);
@@ -106,7 +117,7 @@ const ModalProvider = ({
106
117
  });
107
118
 
108
119
  const handleCloseModal = (index: number) => {
109
- m.close(index);
120
+ modal.close(index);
110
121
  };
111
122
 
112
123
  const refReducer = (index: number, value: any) => {
@@ -121,6 +132,7 @@ const ModalProvider = ({
121
132
 
122
133
  return (
123
134
  <div
135
+ data-index={i}
124
136
  key={item.modalId}
125
137
  className={`modal-manager backdrop_modal_manager ${backdropClassName}`}
126
138
  >
package/src/styles.css CHANGED
@@ -16,6 +16,24 @@
16
16
  }
17
17
  }
18
18
 
19
+ @keyframes bg_opacity_out {
20
+ from {
21
+ opacity: 1;
22
+ }
23
+ to {
24
+ opacity: 0;
25
+ }
26
+ }
27
+
28
+ @keyframes bg_scale_out {
29
+ from {
30
+ transform: scale(1);
31
+ }
32
+ to {
33
+ transform: scale(0.8);
34
+ }
35
+ }
36
+
19
37
  .modal-manager.backdrop_modal_manager {
20
38
  display: flex;
21
39
  justify-content: center;
@@ -47,3 +65,11 @@
47
65
  animation: bg_opacity_scale 150ms ease-in-out forwards;
48
66
  width: 100%;
49
67
  }
68
+
69
+ .modal-manager.closing .backdrop {
70
+ animation: bg_opacity_out 150ms ease-in-out forwards;
71
+ }
72
+
73
+ .modal-manager.closing .modal_paper {
74
+ animation: bg_scale_out 150ms ease-in-out forwards;
75
+ }