lucid-ui
Version:
A UI component library from AppNexus.
92 lines • 4.71 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("react-peek/prop-types"));
var react_dom_1 = __importDefault(require("react-dom"));
var component_types_1 = require("../../util/component-types");
var style_helpers_1 = require("../../util/style-helpers");
var classnames_1 = __importDefault(require("classnames"));
var cx = style_helpers_1.lucidClassNames.bind('&-Portal');
var any = prop_types_1.default.any, node = prop_types_1.default.node, string = prop_types_1.default.string;
var Portal = /** @class */ (function (_super) {
__extends(Portal, _super);
function Portal() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
isReady: false,
};
_this.manuallyCreatedPortal = false;
_this.portalElement = document.createElement('div');
return _this;
}
Portal.prototype.componentDidMount = function () {
var portalId = this.props.portalId;
var portalElement;
if (portalId) {
portalElement = document.getElementById(portalId);
}
if (!portalElement) {
this.manuallyCreatedPortal = true;
portalElement = document.createElement('div');
portalElement.id = portalId;
document.body.appendChild(portalElement);
}
this.portalElement = portalElement;
this.setState({ isReady: true });
};
Portal.prototype.componentWillUnmount = function () {
if (this.manuallyCreatedPortal) {
this.portalElement.remove();
}
};
Portal.prototype.render = function () {
return this.state.isReady
? react_dom_1.default.createPortal(react_1.default.createElement("div", __assign({ className: classnames_1.default(cx('&'), this.props.className) }, component_types_1.omitProps(this.props, undefined, Object.keys(Portal.propTypes))), this.props.children), this.portalElement)
: null;
};
Portal.displayName = 'Portal';
Portal.peek = {
description: "\n\t\t\tA Portal component is used to render content in a container that is\n\t\t\tappended to `document.body`.\n\t\t",
categories: ['utility'],
};
Portal.propTypes = {
children: node(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\tany valid React children\n\t\t"], ["\n\t\t\tany valid React children\n\t\t"]))),
className: any(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t\tValue is run through the `classnames` library.\n\t\t"], ["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t\tValue is run through the \\`classnames\\` library.\n\t\t"]))),
portalId: string(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\t\tThe `id` of the portal element that is appended to `document.body`.\n\t\t"], ["\n\t\t\tThe \\`id\\` of the portal element that is appended to \\`document.body\\`.\n\t\t"]))),
};
return Portal;
}(react_1.default.Component));
exports.default = Portal;
var templateObject_1, templateObject_2, templateObject_3;
//# sourceMappingURL=Portal.js.map