@zohodesk/components
Version:
Dot UI is a customizable React component library built to deliver a clean, accessible, and developer-friendly UI experience. It offers a growing set of reusable components designed to align with modern design systems and streamline application development
236 lines (192 loc) • 10.1 kB
JavaScript
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _defaultProps = require("./props/defaultProps");
var _propTypes = require("./props/propTypes");
var _Portal = _interopRequireDefault(require("./Portal/Portal"));
var _Config = require("../Provider/Config");
var _AppContainerModule = _interopRequireDefault(require("../AppContainer/AppContainer.module.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var createdPortalIds = [];
var newPortalPrefix = 'CPortal';
var portalChildrenTopIndexValues;
var Modal = /*#__PURE__*/function (_React$Component) {
_inherits(Modal, _React$Component);
var _super = _createSuper(Modal);
function Modal(props) {
var _this;
_classCallCheck(this, Modal);
_this = _super.call(this, props);
_this.handleAddPortalId = _this.handleAddPortalId.bind(_assertThisInitialized(_this));
_this.handleRemovePortalId = _this.handleRemovePortalId.bind(_assertThisInitialized(_this));
_this.handleInsertPortalDiv = _this.handleInsertPortalDiv.bind(_assertThisInitialized(_this));
_this.setPortalDiv = _this.setPortalDiv.bind(_assertThisInitialized(_this));
_this.getRootElement = _this.getRootElement.bind(_assertThisInitialized(_this));
_this.getPortalPrefix = _this.getPortalPrefix.bind(_assertThisInitialized(_this));
_this.state = {
isMounted: false
};
_this.portalPrefix = _this.getPortalPrefix();
_this.portalId = props.portalId;
_this.setRef = _this.setRef.bind(_assertThisInitialized(_this));
_this.ref = null; // this.portalDiv = document.createDocumentFragment();
return _this;
}
_createClass(Modal, [{
key: "setRef",
value: function setRef(ele) {
this.ref = ele;
if (this.isZIndexAppendNeeded) {
this.ref.style.zIndex = this.zIndex;
this.isZIndexAppendNeeded = false;
}
}
}, {
key: "getRootElement",
value: function getRootElement() {
var getRootElement = (0, _Config.getLibraryConfig)('getRootElement');
if (getRootElement && typeof getRootElement === 'function') {
var parent = getRootElement();
if (parent) {
return parent;
}
}
return null;
}
}, {
key: "getPortalPrefix",
value: function getPortalPrefix() {
var portalPrefix = (0, _Config.getLibraryConfig)('portalPrefix');
return portalPrefix !== '' ? portalPrefix : '';
}
}, {
key: "setPortalDiv",
value: function setPortalDiv() {
var modalRoot = this.props.portalId ? document.querySelector("[data-portal=".concat(this.portalPrefix).concat(this.props.portalId, "]")) : '';
if (!modalRoot) {
this.portalId = this.handleAddPortalId();
this.newModalRoot = document.createElement('div');
this.newModalRoot.className = _AppContainerModule["default"].container;
this.newModalRoot.setAttribute('data-portal', "".concat(this.portalPrefix).concat(this.portalId));
this.containerDiv && this.containerDiv.appendChild(this.newModalRoot);
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
this.containerDiv = this.getRootElement() || document.getElementsByTagName('desk')[0] || document.getElementsByTagName('body')[0];
this.setPortalDiv();
this.setState({
isMounted: true
}, function () {
_this2.handleInsertPortalDiv();
});
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.newModalRoot) {
this.containerDiv && this.containerDiv.removeChild(this.newModalRoot);
this.handleRemovePortalId();
}
if (this.zIndex && portalChildrenTopIndexValues === this.zIndex) {
portalChildrenTopIndexValues -= 1;
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (prevProps.isActive != this.props.isActive) {
if (this.props.isActive) {
this.handleInsertPortalDiv();
} else if (this.zIndex && portalChildrenTopIndexValues === this.zIndex) {
portalChildrenTopIndexValues -= 1;
}
}
}
}, {
key: "handleInsertPortalDiv",
value: function handleInsertPortalDiv() {
var autoZIndexNeeded = this.props.autoZIndexNeeded;
if (autoZIndexNeeded) {
var isActive = this.props.isActive;
if (isActive) {
var newHighValue = Number(portalChildrenTopIndexValues || 10) + 1;
portalChildrenTopIndexValues = newHighValue;
this.zIndex = newHighValue;
if (this.ref) {
this.ref.style.zIndex = newHighValue;
} else {
this.isZIndexAppendNeeded = true;
}
}
}
}
}, {
key: "handleAddPortalId",
value: function handleAddPortalId() {
var createdPortalIdsLen = createdPortalIds.length;
var newPortalId = createdPortalIdsLen ? createdPortalIds[createdPortalIdsLen - 1] + 1 : 1;
createdPortalIds.push(newPortalId);
this.portalId = newPortalId;
this.isCustomPortalId = true;
return "".concat(newPortalPrefix).concat(newPortalId);
}
}, {
key: "handleRemovePortalId",
value: function handleRemovePortalId() {
var _this3 = this;
if (this.isCustomPortalId) {
createdPortalIds = createdPortalIds.filter(function (id) {
return id !== _this3.portalId;
});
}
}
}, {
key: "render",
value: function render() {
var children = this.props.children,
isMounted = this.state.isMounted;
var Element = children.type,
elementProps = children.props;
if (isMounted) {
if (Element) {
return /*#__PURE__*/_react["default"].createElement(_Portal["default"], {
portalId: "".concat(this.portalPrefix).concat(this.portalId)
}, /*#__PURE__*/_react["default"].createElement(Element, _extends({
ref: this.setRef
}, elementProps)));
}
return null;
}
return null;
}
}]);
return Modal;
}(_react["default"].Component);
exports["default"] = Modal;
Modal.defaultProps = _defaultProps.defaultProps;
Modal.propTypes = _propTypes.propTypes; // if (__DOCS__) {
// Modal.docs = {
// componentGroup: 'Atom',
// folderName: 'Style Guide',
// external: false,
// description: ' '
// };
// }