@elastic/eui
Version:
Elastic UI Component Library
118 lines (115 loc) • 8.56 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.insertPositions = exports.INSERT_POSITIONS = exports.EuiPortal = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactDom = require("react-dom");
var _services = require("../../services");
var _common = require("../common");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
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; } } /*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/ /**
* NOTE: We can't test this component because Enzyme doesn't support rendering
* into portals.
*/
var insertPositions = {
after: 'afterend',
before: 'beforebegin'
};
exports.insertPositions = insertPositions;
var INSERT_POSITIONS = (0, _common.keysOf)(insertPositions);
exports.INSERT_POSITIONS = INSERT_POSITIONS;
var EuiPortal = /*#__PURE__*/function (_Component) {
(0, _inherits2.default)(EuiPortal, _Component);
var _super = _createSuper(EuiPortal);
function EuiPortal(props) {
var _this;
(0, _classCallCheck2.default)(this, EuiPortal);
_this = _super.call(this, props);
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "portalNode", null);
if (typeof window === 'undefined') return (0, _possibleConstructorReturn2.default)(_this); // Prevent SSR errors
var insert = _this.props.insert;
_this.portalNode = document.createElement('div');
_this.portalNode.dataset.euiportal = 'true';
if (insert == null) {
// no insertion defined, append to body
document.body.appendChild(_this.portalNode);
} else {
// inserting before or after an element
var sibling = insert.sibling,
position = insert.position;
sibling.insertAdjacentElement(insertPositions[position], _this.portalNode);
}
return _this;
}
(0, _createClass2.default)(EuiPortal, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setThemeColor();
this.updatePortalRef(this.portalNode);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var _this$portalNode;
if ((_this$portalNode = this.portalNode) !== null && _this$portalNode !== void 0 && _this$portalNode.parentNode) {
this.portalNode.parentNode.removeChild(this.portalNode);
}
this.updatePortalRef(null);
}
// Set the inherited color of the portal based on the wrapping EuiThemeProvider
}, {
key: "setThemeColor",
value: function setThemeColor() {
if (this.portalNode && this.context) {
var _this$context = this.context,
hasDifferentColorFromGlobalTheme = _this$context.hasDifferentColorFromGlobalTheme,
colorClassName = _this$context.colorClassName;
if (hasDifferentColorFromGlobalTheme && this.props.insert == null) {
this.portalNode.classList.add(colorClassName);
}
}
}
}, {
key: "updatePortalRef",
value: function updatePortalRef(ref) {
if (this.props.portalRef) {
this.props.portalRef(ref);
}
}
}, {
key: "render",
value: function render() {
return this.portalNode ? /*#__PURE__*/(0, _reactDom.createPortal)(this.props.children, this.portalNode) : null;
}
}]);
return EuiPortal;
}(_react.Component);
exports.EuiPortal = EuiPortal;
(0, _defineProperty2.default)(EuiPortal, "contextType", _services.EuiNestedThemeContext);
EuiPortal.propTypes = {
/**
* ReactNode to render as this component's content
*/
children: _propTypes.default.node.isRequired,
insert: _propTypes.default.shape({
sibling: _propTypes.default.any.isRequired,
position: _propTypes.default.oneOf(["before", "after"]).isRequired
}),
portalRef: _propTypes.default.func
};