@aliretail/react-materials-components
Version:
52 lines (41 loc) • 1.61 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as React from 'react';
import ReactDOM from 'react-dom';
var Modal = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(Modal, _React$Component);
function Modal(props) {
var _this;
_this = _React$Component.call(this, props) || this;
var modalRoot = document.getElementById('aliretail-form-error-modal');
if (modalRoot) {
_this.modalRoot = modalRoot;
} else {
var rootEle = document.createElement('div');
rootEle.id = 'aliretail-form-error-modal';
document.body.appendChild(rootEle);
_this.modalRoot = rootEle;
}
_this.el = document.createElement('div');
return _this;
}
var _proto = Modal.prototype;
_proto.componentDidMount = function componentDidMount() {
// The portal element is inserted in the DOM tree after
// the Modal's children are mounted, meaning that children
// will be mounted on a detached DOM node. If a child
// component requires to be attached to the DOM tree
// immediately when mounted, for example to measure a
// DOM node, or uses 'autoFocus' in a descendant, add
// state to Modal and only render the children when Modal
// is inserted in the DOM tree.
this.modalRoot.appendChild(this.el);
};
_proto.componentWillUnmount = function componentWillUnmount() {
this.modalRoot.removeChild(this.el);
};
_proto.render = function render() {
return /*#__PURE__*/ReactDOM.createPortal(this.props.children, this.el);
};
return Modal;
}(React.Component);
export default Modal;