1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | exports.__esModule = true;
|
8 | exports["default"] = void 0;
|
9 |
|
10 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
11 |
|
12 | var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
13 |
|
14 | var _activeElement = _interopRequireDefault(require("dom-helpers/activeElement"));
|
15 |
|
16 | var _contains = _interopRequireDefault(require("dom-helpers/contains"));
|
17 |
|
18 | var _canUseDOM = _interopRequireDefault(require("dom-helpers/canUseDOM"));
|
19 |
|
20 | var _listen = _interopRequireDefault(require("dom-helpers/listen"));
|
21 |
|
22 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
23 |
|
24 | var _react = _interopRequireWildcard(require("react"));
|
25 |
|
26 | var _reactDom = _interopRequireDefault(require("react-dom"));
|
27 |
|
28 | var _useMounted = _interopRequireDefault(require("@restart/hooks/useMounted"));
|
29 |
|
30 | var _useWillUnmount = _interopRequireDefault(require("@restart/hooks/useWillUnmount"));
|
31 |
|
32 | var _usePrevious = _interopRequireDefault(require("@restart/hooks/usePrevious"));
|
33 |
|
34 | var _useEventCallback = _interopRequireDefault(require("@restart/hooks/useEventCallback"));
|
35 |
|
36 | var _ModalManager = _interopRequireDefault(require("./ModalManager"));
|
37 |
|
38 | var _useWaitForDOMRef = _interopRequireDefault(require("./useWaitForDOMRef"));
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | var manager;
|
44 |
|
45 | function getManager() {
|
46 | if (!manager) manager = new _ModalManager["default"]();
|
47 | return manager;
|
48 | }
|
49 |
|
50 | function useModalManager(provided) {
|
51 | var modalManager = provided || getManager();
|
52 | var modal = (0, _react.useRef)({
|
53 | dialog: null,
|
54 | backdrop: null
|
55 | });
|
56 | return Object.assign(modal.current, {
|
57 | add: function add(container, className) {
|
58 | return modalManager.add(modal.current, container, className);
|
59 | },
|
60 | remove: function remove() {
|
61 | return modalManager.remove(modal.current);
|
62 | },
|
63 | isTopModal: function isTopModal() {
|
64 | return modalManager.isTopModal(modal.current);
|
65 | },
|
66 | setDialogRef: (0, _react.useCallback)(function (ref) {
|
67 | modal.current.dialog = ref;
|
68 | }, []),
|
69 | setBackdropRef: (0, _react.useCallback)(function (ref) {
|
70 | modal.current.backdrop = ref;
|
71 | }, [])
|
72 | });
|
73 | }
|
74 |
|
75 | var Modal = (0, _react.forwardRef)(function (_ref, ref) {
|
76 | var _ref$show = _ref.show,
|
77 | show = _ref$show === void 0 ? false : _ref$show,
|
78 | _ref$role = _ref.role,
|
79 | role = _ref$role === void 0 ? 'dialog' : _ref$role,
|
80 | className = _ref.className,
|
81 | style = _ref.style,
|
82 | children = _ref.children,
|
83 | _ref$backdrop = _ref.backdrop,
|
84 | backdrop = _ref$backdrop === void 0 ? true : _ref$backdrop,
|
85 | _ref$keyboard = _ref.keyboard,
|
86 | keyboard = _ref$keyboard === void 0 ? true : _ref$keyboard,
|
87 | onBackdropClick = _ref.onBackdropClick,
|
88 | onEscapeKeyDown = _ref.onEscapeKeyDown,
|
89 | transition = _ref.transition,
|
90 | backdropTransition = _ref.backdropTransition,
|
91 | _ref$autoFocus = _ref.autoFocus,
|
92 | autoFocus = _ref$autoFocus === void 0 ? true : _ref$autoFocus,
|
93 | _ref$enforceFocus = _ref.enforceFocus,
|
94 | enforceFocus = _ref$enforceFocus === void 0 ? true : _ref$enforceFocus,
|
95 | _ref$restoreFocus = _ref.restoreFocus,
|
96 | restoreFocus = _ref$restoreFocus === void 0 ? true : _ref$restoreFocus,
|
97 | restoreFocusOptions = _ref.restoreFocusOptions,
|
98 | renderDialog = _ref.renderDialog,
|
99 | _ref$renderBackdrop = _ref.renderBackdrop,
|
100 | renderBackdrop = _ref$renderBackdrop === void 0 ? function (props) {
|
101 | return _react["default"].createElement("div", props);
|
102 | } : _ref$renderBackdrop,
|
103 | providedManager = _ref.manager,
|
104 | containerRef = _ref.container,
|
105 | containerClassName = _ref.containerClassName,
|
106 | onShow = _ref.onShow,
|
107 | _ref$onHide = _ref.onHide,
|
108 | onHide = _ref$onHide === void 0 ? function () {} : _ref$onHide,
|
109 | onExit = _ref.onExit,
|
110 | onExited = _ref.onExited,
|
111 | onExiting = _ref.onExiting,
|
112 | onEnter = _ref.onEnter,
|
113 | onEntering = _ref.onEntering,
|
114 | onEntered = _ref.onEntered,
|
115 | rest = (0, _objectWithoutPropertiesLoose2["default"])(_ref, ["show", "role", "className", "style", "children", "backdrop", "keyboard", "onBackdropClick", "onEscapeKeyDown", "transition", "backdropTransition", "autoFocus", "enforceFocus", "restoreFocus", "restoreFocusOptions", "renderDialog", "renderBackdrop", "manager", "container", "containerClassName", "onShow", "onHide", "onExit", "onExited", "onExiting", "onEnter", "onEntering", "onEntered"]);
|
116 | var container = (0, _useWaitForDOMRef["default"])(containerRef);
|
117 | var modal = useModalManager(providedManager);
|
118 | var isMounted = (0, _useMounted["default"])();
|
119 | var prevShow = (0, _usePrevious["default"])(show);
|
120 |
|
121 | var _useState = (0, _react.useState)(!show),
|
122 | exited = _useState[0],
|
123 | setExited = _useState[1];
|
124 |
|
125 | var lastFocusRef = (0, _react.useRef)(null);
|
126 | (0, _react.useImperativeHandle)(ref, function () {
|
127 | return modal;
|
128 | }, [modal]);
|
129 |
|
130 | if (_canUseDOM["default"] && !prevShow && show) {
|
131 | lastFocusRef.current = (0, _activeElement["default"])();
|
132 | }
|
133 |
|
134 | if (!transition && !show && !exited) {
|
135 | setExited(true);
|
136 | } else if (show && exited) {
|
137 | setExited(false);
|
138 | }
|
139 |
|
140 | var handleShow = (0, _useEventCallback["default"])(function () {
|
141 | modal.add(container, containerClassName);
|
142 | removeKeydownListenerRef.current = (0, _listen["default"])(document, 'keydown', handleDocumentKeyDown);
|
143 | removeFocusListenerRef.current = (0, _listen["default"])(document, 'focus',
|
144 |
|
145 | function () {
|
146 | return setTimeout(handleEnforceFocus);
|
147 | }, true);
|
148 |
|
149 | if (onShow) {
|
150 | onShow();
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 | if (autoFocus) {
|
156 | var currentActiveElement = (0, _activeElement["default"])(document);
|
157 |
|
158 | if (modal.dialog && currentActiveElement && !(0, _contains["default"])(modal.dialog, currentActiveElement)) {
|
159 | lastFocusRef.current = currentActiveElement;
|
160 | modal.dialog.focus();
|
161 | }
|
162 | }
|
163 | });
|
164 | var handleHide = (0, _useEventCallback["default"])(function () {
|
165 | modal.remove();
|
166 | removeKeydownListenerRef.current == null ? void 0 : removeKeydownListenerRef.current();
|
167 | removeFocusListenerRef.current == null ? void 0 : removeFocusListenerRef.current();
|
168 |
|
169 | if (restoreFocus) {
|
170 | var _lastFocusRef$current;
|
171 |
|
172 |
|
173 | (_lastFocusRef$current = lastFocusRef.current) == null ? void 0 : _lastFocusRef$current.focus == null ? void 0 : _lastFocusRef$current.focus(restoreFocusOptions);
|
174 | lastFocusRef.current = null;
|
175 | }
|
176 | });
|
177 |
|
178 |
|
179 |
|
180 | (0, _react.useEffect)(function () {
|
181 | if (!show || !container) return;
|
182 | handleShow();
|
183 | }, [show, container,
|
184 |
|
185 | handleShow]);
|
186 |
|
187 |
|
188 |
|
189 | (0, _react.useEffect)(function () {
|
190 | if (!exited) return;
|
191 | handleHide();
|
192 | }, [exited, handleHide]);
|
193 | (0, _useWillUnmount["default"])(function () {
|
194 | handleHide();
|
195 | });
|
196 |
|
197 | var handleEnforceFocus = (0, _useEventCallback["default"])(function () {
|
198 | if (!enforceFocus || !isMounted() || !modal.isTopModal()) {
|
199 | return;
|
200 | }
|
201 |
|
202 | var currentActiveElement = (0, _activeElement["default"])();
|
203 |
|
204 | if (modal.dialog && currentActiveElement && !(0, _contains["default"])(modal.dialog, currentActiveElement)) {
|
205 | modal.dialog.focus();
|
206 | }
|
207 | });
|
208 | var handleBackdropClick = (0, _useEventCallback["default"])(function (e) {
|
209 | if (e.target !== e.currentTarget) {
|
210 | return;
|
211 | }
|
212 |
|
213 | onBackdropClick == null ? void 0 : onBackdropClick(e);
|
214 |
|
215 | if (backdrop === true) {
|
216 | onHide();
|
217 | }
|
218 | });
|
219 |
|
220 | var handleDocumentKeyDown = function handleDocumentKeyDown(e) {
|
221 | if (keyboard && e.keyCode === 27 && modal.isTopModal()) {
|
222 | onEscapeKeyDown == null ? void 0 : onEscapeKeyDown(e);
|
223 | onHide();
|
224 | }
|
225 | };
|
226 |
|
227 | var removeFocusListenerRef = (0, _react.useRef)();
|
228 | var removeKeydownListenerRef = (0, _react.useRef)();
|
229 |
|
230 | var handleHidden = function handleHidden() {
|
231 | setExited(true);
|
232 |
|
233 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
234 | args[_key] = arguments[_key];
|
235 | }
|
236 |
|
237 | onExited == null ? void 0 : onExited.apply(void 0, args);
|
238 | };
|
239 |
|
240 | var Transition = transition;
|
241 |
|
242 | if (!container || !(show || Transition && !exited)) {
|
243 | return null;
|
244 | }
|
245 |
|
246 | var dialogProps = (0, _extends2["default"])({
|
247 | role: role,
|
248 | ref: modal.setDialogRef,
|
249 |
|
250 | 'aria-modal': role === 'dialog' ? true : undefined
|
251 | }, rest, {
|
252 | style: style,
|
253 | className: className,
|
254 | tabIndex: -1
|
255 | });
|
256 | var dialog = renderDialog ? renderDialog(dialogProps) : _react["default"].createElement("div", dialogProps, _react["default"].cloneElement(children, {
|
257 | role: 'document'
|
258 | }));
|
259 |
|
260 | if (Transition) {
|
261 | dialog = _react["default"].createElement(Transition, {
|
262 | appear: true,
|
263 | unmountOnExit: true,
|
264 | "in": !!show,
|
265 | onExit: onExit,
|
266 | onExiting: onExiting,
|
267 | onExited: handleHidden,
|
268 | onEnter: onEnter,
|
269 | onEntering: onEntering,
|
270 | onEntered: onEntered
|
271 | }, dialog);
|
272 | }
|
273 |
|
274 | var backdropElement = null;
|
275 |
|
276 | if (backdrop) {
|
277 | var BackdropTransition = backdropTransition;
|
278 | backdropElement = renderBackdrop({
|
279 | ref: modal.setBackdropRef,
|
280 | onClick: handleBackdropClick
|
281 | });
|
282 |
|
283 | if (BackdropTransition) {
|
284 | backdropElement = _react["default"].createElement(BackdropTransition, {
|
285 | appear: true,
|
286 | "in": !!show
|
287 | }, backdropElement);
|
288 | }
|
289 | }
|
290 |
|
291 | return _react["default"].createElement(_react["default"].Fragment, null, _reactDom["default"].createPortal( _react["default"].createElement(_react["default"].Fragment, null, backdropElement, dialog), container));
|
292 | });
|
293 | var propTypes = {
|
294 | |
295 |
|
296 |
|
297 | show: _propTypes["default"].bool,
|
298 |
|
299 | |
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | container: _propTypes["default"].any,
|
306 |
|
307 | |
308 |
|
309 |
|
310 | onShow: _propTypes["default"].func,
|
311 |
|
312 | |
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | onHide: _propTypes["default"].func,
|
319 |
|
320 | |
321 |
|
322 |
|
323 | backdrop: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].oneOf(['static'])]),
|
324 |
|
325 | |
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | renderDialog: _propTypes["default"].func,
|
334 |
|
335 | |
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | renderBackdrop: _propTypes["default"].func,
|
344 |
|
345 | |
346 |
|
347 |
|
348 | onEscapeKeyDown: _propTypes["default"].func,
|
349 |
|
350 | |
351 |
|
352 |
|
353 | onBackdropClick: _propTypes["default"].func,
|
354 |
|
355 | |
356 |
|
357 |
|
358 |
|
359 | containerClassName: _propTypes["default"].string,
|
360 |
|
361 | |
362 |
|
363 |
|
364 | keyboard: _propTypes["default"].bool,
|
365 |
|
366 | |
367 |
|
368 |
|
369 |
|
370 | transition: _propTypes["default"].elementType,
|
371 |
|
372 | |
373 |
|
374 |
|
375 |
|
376 | backdropTransition: _propTypes["default"].elementType,
|
377 |
|
378 | |
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | autoFocus: _propTypes["default"].bool,
|
387 |
|
388 | |
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | enforceFocus: _propTypes["default"].bool,
|
395 |
|
396 | |
397 |
|
398 |
|
399 |
|
400 | restoreFocus: _propTypes["default"].bool,
|
401 |
|
402 | |
403 |
|
404 |
|
405 |
|
406 |
|
407 | restoreFocusOptions: _propTypes["default"].shape({
|
408 | preventScroll: _propTypes["default"].bool
|
409 | }),
|
410 |
|
411 | |
412 |
|
413 |
|
414 | onEnter: _propTypes["default"].func,
|
415 |
|
416 | |
417 |
|
418 |
|
419 | onEntering: _propTypes["default"].func,
|
420 |
|
421 | |
422 |
|
423 |
|
424 | onEntered: _propTypes["default"].func,
|
425 |
|
426 | |
427 |
|
428 |
|
429 | onExit: _propTypes["default"].func,
|
430 |
|
431 | |
432 |
|
433 |
|
434 | onExiting: _propTypes["default"].func,
|
435 |
|
436 | |
437 |
|
438 |
|
439 | onExited: _propTypes["default"].func,
|
440 |
|
441 | |
442 |
|
443 |
|
444 |
|
445 | manager: _propTypes["default"].instanceOf(_ModalManager["default"])
|
446 | };
|
447 | Modal.displayName = 'Modal';
|
448 | Modal.propTypes = propTypes;
|
449 |
|
450 | var _default = Object.assign(Modal, {
|
451 | Manager: _ModalManager["default"]
|
452 | });
|
453 |
|
454 | exports["default"] = _default;
|
455 | module.exports = exports.default; |
\ | No newline at end of file |