all files / lib/components/ Modal.js

82.09% Statements 110/134
60.24% Branches 50/83
83.33% Functions 20/24
92.22% Lines 83/90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250                                                  49×   98×             97×       49×   49×   49×   49× 147×     49× 47× 47× 47×   47× 38×     49× 46×       49×     49×           48× 48×   48×   48× 48× 48×                                     48×   48×   47×   47×     47× 47× 47×   47×                 47×           50×                                                                                                   97×                                                    
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Eif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _reactDom = require('react-dom');
 
var _reactDom2 = _interopRequireDefault(_reactDom);
 
var _propTypes = require('prop-types');
 
var _propTypes2 = _interopRequireDefault(_propTypes);
 
var _exenv = require('exenv');
 
var _exenv2 = _interopRequireDefault(_exenv);
 
var _elementClass = require('element-class');
 
var _elementClass2 = _interopRequireDefault(_elementClass);
 
var _ModalPortal = require('./ModalPortal');
 
var _ModalPortal2 = _interopRequireDefault(_ModalPortal);
 
var _ariaAppHider = require('../helpers/ariaAppHider');
 
var ariaAppHider = _interopRequireWildcard(_ariaAppHider);
 
var _refCount = require('../helpers/refCount');
 
var refCount = _interopRequireWildcard(_refCount);
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
var EE = _exenv2.default;
var renderSubtreeIntoContainer = _reactDom2.default.unstable_renderSubtreeIntoContainer;
 
var SafeHTMLElement = EE.canUseDOM ? window.HTMLElement : {};
var AppElement = EE.canUseDOM ? document.body : {
  appendChild: function appendChild() {}
};
 
function getParentElement(parentSelector) {
  return parentSelector();
}
 
var Modal = function (_Component) {
  _inherits(Modal, _Component);
 
  function Modal() {
    var _ref;
 
    var _temp, _this, _ret;
 
    _classCallCheck(this, Modal);
 
    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
 
    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Modal.__proto__ || Object.getPrototypeOf(Modal)).call.apply(_ref, [this].concat(args))), _this), _this.removePortal = function () {
      _reactDom2.default.unmountComponentAtNode(_this.node);
      var parent = getParentElement(_this.props.parentSelector);
      parent.removeChild(_this.node);
 
      if (refCount.count() === 0) {
        (0, _elementClass2.default)(document.body).remove(_this.props.bodyOpenClassName);
      }
    }, _this.renderPortal = function (props) {
      if (props.isOpen || refCount.count() > 0) {
        (0, _elementClass2.default)(document.body).add(_this.props.bodyOpenClassName);
      } else {
        (0, _elementClass2.default)(document.body).remove(_this.props.bodyOpenClassName);
      }
 
      if (props.ariaHideApp) {
        ariaAppHider.toggle(props.isOpen, props.appElement);
      }
 
      _this.portal = renderSubtreeIntoContainer(_this, _react2.default.createElement(_ModalPortal2.default, _extends({ defaultStyles: Modal.defaultStyles }, props)), _this.node);
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }
 
  _createClass(Modal, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.node = document.createElement('div');
      this.node.className = this.props.portalClassName;
 
      if (this.props.isOpen) refCount.add(this);
 
      var parent = getParentElement(this.props.parentSelector);
      parent.appendChild(this.node);
      this.renderPortal(this.props);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(newProps) {
      Eif (newProps.isOpen) refCount.add(this);
      Iif (!newProps.isOpen) refCount.remove(this);
      var currentParent = getParentElement(this.props.parentSelector);
      var newParent = getParentElement(newProps.parentSelector);
 
      Iif (newParent !== currentParent) {
        currentParent.removeChild(this.node);
        newParent.appendChild(this.node);
      }
 
      this.renderPortal(newProps);
    }
  }, {
    key: 'componentWillUpdate',
    value: function componentWillUpdate(newProps) {
      Eif (newProps.portalClassName !== this.props.portalClassName) {
        this.node.className = newProps.portalClassName;
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      var _this2 = this;
 
      if (!this.node) return;
 
      refCount.remove(this);
 
      if (this.props.ariaHideApp) {
        ariaAppHider.show(this.props.appElement);
      }
 
      var state = this.portal.state;
      var now = Date.now();
      var closesAt = state.isOpen && this.props.closeTimeoutMS && (state.closesAt || now + this.props.closeTimeoutMS);
 
      Iif (closesAt) {
        if (!state.beforeClose) {
          this.portal.closeWithTimeout();
        }
 
        setTimeout(function () {
          return _this2.removePortal;
        }, closesAt - now);
      } else {
        this.removePortal();
      }
    }
  }, {
    key: 'render',
    value: function render() {
      return null;
    }
  }], [{
    key: 'setAppElement',
    value: function setAppElement(element) {
      ariaAppHider.setElement(element || AppElement);
    }
 
    /* eslint-disable no-console */
 
  }, {
    key: 'injectCSS',
    value: function injectCSS() {
      process.env.NODE_ENV !== "production" && console.warn('React-Modal: injectCSS has been deprecated ' + 'and no longer has any effect. It will be removed in a later version');
    }
    /* eslint-enable no-console */
 
    /* eslint-disable react/no-unused-prop-types */
 
    /* eslint-enable react/no-unused-prop-types */
 
  }]);
 
  return Modal;
}(_react.Component);
 
Modal.propTypes = {
  isOpen: _propTypes2.default.bool.isRequired,
  style: _propTypes2.default.shape({
    content: _propTypes2.default.object,
    overlay: _propTypes2.default.object
  }),
  portalClassName: _propTypes2.default.string,
  bodyOpenClassName: _propTypes2.default.string,
  className: _propTypes2.default.oneOfType([_propTypes2.default.String, _propTypes2.default.object]),
  overlayClassName: _propTypes2.default.oneOfType([_propTypes2.default.String, _propTypes2.default.object]),
  appElement: _propTypes2.default.instanceOf(SafeHTMLElement),
  onAfterOpen: _propTypes2.default.func,
  onRequestClose: _propTypes2.default.func,
  closeTimeoutMS: _propTypes2.default.number,
  ariaHideApp: _propTypes2.default.bool,
  shouldCloseOnOverlayClick: _propTypes2.default.bool,
  parentSelector: _propTypes2.default.func,
  role: _propTypes2.default.string,
  contentLabel: _propTypes2.default.string.isRequired
};
Modal.defaultProps = {
  isOpen: false,
  portalClassName: 'ReactModalPortal',
  bodyOpenClassName: 'ReactModal__Body--open',
  ariaHideApp: true,
  closeTimeoutMS: 0,
  shouldCloseOnOverlayClick: true,
  parentSelector: function parentSelector() {
    return document.body;
  }
};
Modal.defaultStyles = {
  overlay: {
    position: 'fixed',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    backgroundColor: 'rgba(255, 255, 255, 0.75)'
  },
  content: {
    position: 'absolute',
    top: '40px',
    left: '40px',
    right: '40px',
    bottom: '40px',
    border: '1px solid #ccc',
    background: '#fff',
    overflow: 'auto',
    WebkitOverflowScrolling: 'touch',
    borderRadius: '4px',
    outline: 'none',
    padding: '20px'
  }
};
exports.default = Modal;