UNPKG

lucid-ui

Version:

A UI component library from Xandr.

105 lines 4.26 kB
"use strict"; 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) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 lodash_1 = require("lodash"); var prop_types_1 = __importDefault(require("prop-types")); var react_dom_1 = __importDefault(require("react-dom")); 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({ "data-test-id": this.props.className, className: (0, classnames_1.default)(cx('&'), this.props.className) }, (0, lodash_1.omit)(this.props, [ 'className', 'children', 'portalId', 'initialState', 'callbackId', ])), this.props.children), this.portalElement) : null; }; Portal.displayName = 'Portal'; Portal.peek = { description: "A `Portal` component is used to render content in a container that is appended to `document.body`.", categories: ['utility'], }; Portal.propTypes = { /** any valid React children */ children: node, /** Appended to the component-specific class names set on the root element. Value is run through the \`classnames\` library. */ className: any, /** The \`id\` of the portal element that is appended to \`document.body\`. */ portalId: string, }; return Portal; }(react_1.default.Component)); exports.default = Portal; //# sourceMappingURL=Portal.js.map