|
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
- import _extends from "@babel/runtime/helpers/esm/extends";
- import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
- import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
- import React from 'react';
- import PropTypes from 'prop-types';
- import classNames from 'classnames';
- import Portal from './Portal';
- import Fade from './Fade';
- import { getOriginalBodyPadding, conditionallyUpdateScrollbar, setScrollbarWidth, mapToCssModules, omit, focusableElements, TransitionTimeouts } from './utils';
-
- function noop() {}
-
- var FadePropTypes = PropTypes.shape(Fade.propTypes);
- var propTypes = {
- isOpen: PropTypes.bool,
- autoFocus: PropTypes.bool,
- centered: PropTypes.bool,
- size: PropTypes.string,
- toggle: PropTypes.func,
- keyboard: PropTypes.bool,
- role: PropTypes.string,
- labelledBy: PropTypes.string,
- backdrop: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['static'])]),
- onEnter: PropTypes.func,
- onExit: PropTypes.func,
- onOpened: PropTypes.func,
- onClosed: PropTypes.func,
- children: PropTypes.node,
- className: PropTypes.string,
- wrapClassName: PropTypes.string,
- modalClassName: PropTypes.string,
- backdropClassName: PropTypes.string,
- contentClassName: PropTypes.string,
- external: PropTypes.node,
- fade: PropTypes.bool,
- cssModule: PropTypes.object,
- zIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
- backdropTransition: FadePropTypes,
- modalTransition: FadePropTypes,
- innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func])
- };
- var propsToOmit = Object.keys(propTypes);
- var defaultProps = {
- isOpen: false,
- autoFocus: true,
- centered: false,
- role: 'dialog',
- backdrop: true,
- keyboard: true,
- zIndex: 1050,
- fade: true,
- onOpened: noop,
- onClosed: noop,
- modalTransition: {
- timeout: TransitionTimeouts.Modal
- },
- backdropTransition: {
- mountOnEnter: true,
- timeout: TransitionTimeouts.Fade // uses standard fade transition
-
- }
- };
-
- var Modal =
- /*#__PURE__*/
- function (_React$Component) {
- _inheritsLoose(Modal, _React$Component);
-
- function Modal(props) {
- var _this;
-
- _this = _React$Component.call(this, props) || this;
- _this._element = null;
- _this._originalBodyPadding = null;
- _this.getFocusableChildren = _this.getFocusableChildren.bind(_assertThisInitialized(_assertThisInitialized(_this)));
- _this.handleBackdropClick = _this.handleBackdropClick.bind(_assertThisInitialized(_assertThisInitialized(_this)));
- _this.handleBackdropMouseDown = _this.handleBackdropMouseDown.bind(_assertThisInitialized(_assertThisInitialized(_this)));
- _this.handleEscape = _this.handleEscape.bind(_assertThisInitialized(_assertThisInitialized(_this)));
- _this.handleTab = _this.handleTab.bind(_assertThisInitialized(_assertThisInitialized(_this)));
- _this.onOpened = _this.onOpened.bind(_assertThisInitialized(_assertThisInitialized(_this)));
- _this.onClosed = _this.onClosed.bind(_assertThisInitialized(_assertThisInitialized(_this)));
- _this.state = {
- isOpen: props.isOpen
- };
-
- if (props.isOpen) {
- _this.init();
- }
-
- return _this;
- }
-
- var _proto = Modal.prototype;
-
- _proto.componentDidMount = function componentDidMount() {
- if (this.props.onEnter) {
- this.props.onEnter();
- }
-
- if (this.state.isOpen && this.props.autoFocus) {
- this.setFocus();
- }
-
- this._isMounted = true;
- };
-
- _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
- if (nextProps.isOpen && !this.props.isOpen) {
- this.setState({
- isOpen: nextProps.isOpen
- });
- }
- };
-
- _proto.componentWillUpdate = function componentWillUpdate(nextProps, nextState) {
- if (nextState.isOpen && !this.state.isOpen) {
- this.init();
- }
- };
-
- _proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
- if (this.props.autoFocus && this.state.isOpen && !prevState.isOpen) {
- this.setFocus();
- }
-
- if (this._element && prevProps.zIndex !== this.props.zIndex) {
- this._element.style.zIndex = this.props.zIndex;
- }
- };
-
- _proto.componentWillUnmount = function componentWillUnmount() {
- if (this.props.onExit) {
- this.props.onExit();
- }
-
- if (this.state.isOpen) {
- this.destroy();
- }
-
- this._isMounted = false;
- };
-
- _proto.onOpened = function onOpened(node, isAppearing) {
- this.props.onOpened();
- (this.props.modalTransition.onEntered || noop)(node, isAppearing);
- };
-
- _proto.onClosed = function onClosed(node) {
- // so all methods get called before it is unmounted
- this.props.onClosed();
- (this.props.modalTransition.onExited || noop)(node);
- this.destroy();
-
- if (this._isMounted) {
- this.setState({
- isOpen: false
- });
- }
- };
-
- _proto.setFocus = function setFocus() {
- if (this._dialog && this._dialog.parentNode && typeof this._dialog.parentNode.focus === 'function') {
- this._dialog.parentNode.focus();
- }
- };
-
- _proto.getFocusableChildren = function getFocusableChildren() {
- return this._element.querySelectorAll(focusableElements.join(', '));
- };
-
- _proto.getFocusedChild = function getFocusedChild() {
- var currentFocus;
- var focusableChildren = this.getFocusableChildren();
-
- try {
- currentFocus = document.activeElement;
- } catch (err) {
- currentFocus = focusableChildren[0];
- }
-
- return currentFocus;
- } // not mouseUp because scrollbar fires it, shouldn't close when user scrolls
- ;
-
- _proto.handleBackdropClick = function handleBackdropClick(e) {
- if (e.target === this._mouseDownElement) {
- e.stopPropagation();
- if (!this.props.isOpen || this.props.backdrop !== true) return;
- var backdrop = this._dialog ? this._dialog.parentNode : null;
-
- if (backdrop && e.target === backdrop && this.props.toggle) {
- this.props.toggle(e);
- }
- }
- };
-
- _proto.handleTab = function handleTab(e) {
- if (e.which !== 9) return;
- var focusableChildren = this.getFocusableChildren();
- var totalFocusable = focusableChildren.length;
- var currentFocus = this.getFocusedChild();
- var focusedIndex = 0;
-
- for (var i = 0; i < totalFocusable; i += 1) {
- if (focusableChildren[i] === currentFocus) {
- focusedIndex = i;
- break;
- }
- }
-
- if (e.shiftKey && focusedIndex === 0) {
- e.preventDefault();
- focusableChildren[totalFocusable - 1].focus();
- } else if (!e.shiftKey && focusedIndex === totalFocusable - 1) {
- e.preventDefault();
- focusableChildren[0].focus();
- }
- };
-
- _proto.handleBackdropMouseDown = function handleBackdropMouseDown(e) {
- this._mouseDownElement = e.target;
- };
-
- _proto.handleEscape = function handleEscape(e) {
- if (this.props.isOpen && this.props.keyboard && e.keyCode === 27 && this.props.toggle) {
- e.preventDefault();
- e.stopPropagation();
- this.props.toggle(e);
- }
- };
-
- _proto.init = function init() {
- try {
- this._triggeringElement = document.activeElement;
- } catch (err) {
- this._triggeringElement = null;
- }
-
- this._element = document.createElement('div');
-
- this._element.setAttribute('tabindex', '-1');
-
- this._element.style.position = 'relative';
- this._element.style.zIndex = this.props.zIndex;
- this._originalBodyPadding = getOriginalBodyPadding();
- conditionallyUpdateScrollbar();
- document.body.appendChild(this._element);
-
- if (Modal.openCount === 0) {
- document.body.className = classNames(document.body.className, mapToCssModules('modal-open', this.props.cssModule));
- }
-
- Modal.openCount += 1;
- };
-
- _proto.destroy = function destroy() {
- if (this._element) {
- document.body.removeChild(this._element);
- this._element = null;
- }
-
- if (this._triggeringElement) {
- if (this._triggeringElement.focus) this._triggeringElement.focus();
- this._triggeringElement = null;
- }
-
- if (Modal.openCount <= 1) {
- var modalOpenClassName = mapToCssModules('modal-open', this.props.cssModule); // Use regex to prevent matching `modal-open` as part of a different class, e.g. `my-modal-opened`
-
- var modalOpenClassNameRegex = new RegExp("(^| )" + modalOpenClassName + "( |$)");
- document.body.className = document.body.className.replace(modalOpenClassNameRegex, ' ').trim();
- }
-
- Modal.openCount -= 1;
- setScrollbarWidth(this._originalBodyPadding);
- };
-
- _proto.renderModalDialog = function renderModalDialog() {
- var _classNames,
- _this2 = this;
-
- var attributes = omit(this.props, propsToOmit);
- var dialogBaseClass = 'modal-dialog';
- return React.createElement("div", _extends({}, attributes, {
- className: mapToCssModules(classNames(dialogBaseClass, this.props.className, (_classNames = {}, _classNames["modal-" + this.props.size] = this.props.size, _classNames[dialogBaseClass + "-centered"] = this.props.centered, _classNames)), this.props.cssModule),
- role: "document",
- ref: function ref(c) {
- _this2._dialog = c;
- }
- }), React.createElement("div", {
- className: mapToCssModules(classNames('modal-content', this.props.contentClassName), this.props.cssModule)
- }, this.props.children));
- };
-
- _proto.render = function render() {
- if (this.state.isOpen) {
- var _this$props = this.props,
- wrapClassName = _this$props.wrapClassName,
- modalClassName = _this$props.modalClassName,
- backdropClassName = _this$props.backdropClassName,
- cssModule = _this$props.cssModule,
- isOpen = _this$props.isOpen,
- backdrop = _this$props.backdrop,
- role = _this$props.role,
- labelledBy = _this$props.labelledBy,
- external = _this$props.external,
- innerRef = _this$props.innerRef;
- var modalAttributes = {
- onClick: this.handleBackdropClick,
- onMouseDown: this.handleBackdropMouseDown,
- onKeyUp: this.handleEscape,
- onKeyDown: this.handleTab,
- style: {
- display: 'block'
- },
- 'aria-labelledby': labelledBy,
- role: role,
- tabIndex: '-1'
- };
- var hasTransition = this.props.fade;
-
- var modalTransition = _objectSpread({}, Fade.defaultProps, this.props.modalTransition, {
- baseClass: hasTransition ? this.props.modalTransition.baseClass : '',
- timeout: hasTransition ? this.props.modalTransition.timeout : 0
- });
-
- var backdropTransition = _objectSpread({}, Fade.defaultProps, this.props.backdropTransition, {
- baseClass: hasTransition ? this.props.backdropTransition.baseClass : '',
- timeout: hasTransition ? this.props.backdropTransition.timeout : 0
- });
-
- var Backdrop = backdrop && (hasTransition ? React.createElement(Fade, _extends({}, backdropTransition, {
- in: isOpen && !!backdrop,
- cssModule: cssModule,
- className: mapToCssModules(classNames('modal-backdrop', backdropClassName), cssModule)
- })) : React.createElement("div", {
- className: mapToCssModules(classNames('modal-backdrop', 'show', backdropClassName), cssModule)
- }));
- return React.createElement(Portal, {
- node: this._element
- }, React.createElement("div", {
- className: mapToCssModules(wrapClassName)
- }, React.createElement(Fade, _extends({}, modalAttributes, modalTransition, {
- in: isOpen,
- onEntered: this.onOpened,
- onExited: this.onClosed,
- cssModule: cssModule,
- className: mapToCssModules(classNames('modal', modalClassName), cssModule),
- innerRef: innerRef
- }), external, this.renderModalDialog()), Backdrop));
- }
-
- return null;
- };
-
- return Modal;
- }(React.Component);
-
- Modal.propTypes = propTypes;
- Modal.defaultProps = defaultProps;
- Modal.openCount = 0;
- export default Modal;
|