UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

129 lines 8.48 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) { 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultProps = void 0; var lodash_1 = __importDefault(require("lodash")); var react_1 = __importDefault(require("react")); var prop_types_1 = __importDefault(require("react-peek/prop-types")); var Portal_1 = __importDefault(require("../Portal/Portal")); var react_transition_group_1 = require("react-transition-group"); var style_helpers_1 = require("../../util/style-helpers"); var component_types_1 = require("../../util/component-types"); var cx = style_helpers_1.lucidClassNames.bind('&-Overlay'); var string = prop_types_1.default.string, bool = prop_types_1.default.bool, func = prop_types_1.default.func, node = prop_types_1.default.node; exports.defaultProps = { isShown: false, isModal: true, onEscape: lodash_1.default.noop, onBackgroundClick: lodash_1.default.noop, isAnimated: true, }; var Overlay = /** @class */ (function (_super) { __extends(Overlay, _super); function Overlay() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.rootHTMLDivElement = react_1.default.createRef(); _this.state = { // This must be in state because getDefaultProps only runs once per // component import which causes collisions portalId: _this.props.portalId || style_helpers_1.uniqueName('Overlay-Portal-'), }; _this.handleDocumentKeyDown = function (event) { // If the user hits the "escape" key, then fire an `onEscape` // TODO: use key helpers if (event.keyCode === 27) { _this.props.onEscape({ event: event, props: _this.props }); } }; _this.handleBackgroundClick = function (event) { // Use the reference we previously stored from the `ref` to check what // element was clicked on. if (_this.rootHTMLDivElement.current && event.target === _this.rootHTMLDivElement.current) { _this.props.onBackgroundClick({ event: event, props: _this.props }); } }; return _this; } Overlay.prototype.componentDidMount = function () { if (window && window.document) { window.document.addEventListener('keydown', this.handleDocumentKeyDown); } }; Overlay.prototype.componentWillUnmount = function () { if (window && window.document) { window.document.removeEventListener('keydown', this.handleDocumentKeyDown); } }; Overlay.prototype.render = function () { var _a = this.props, className = _a.className, isShown = _a.isShown, isModal = _a.isModal, isAnimated = _a.isAnimated, children = _a.children, passThroughs = __rest(_a, ["className", "isShown", "isModal", "isAnimated", "children"]); var portalId = this.state.portalId; var overlayElement = (react_1.default.createElement("div", __assign({}, component_types_1.omitProps(passThroughs, undefined, Object.keys(Overlay.propTypes)), { className: cx(className, '&', { '&-is-not-modal': !isModal, '&-is-animated': isAnimated, }), onClick: this.handleBackgroundClick, ref: this.rootHTMLDivElement }), children)); return (react_1.default.createElement(Portal_1.default, { portalId: portalId }, isAnimated ? (react_1.default.createElement(react_transition_group_1.CSSTransition, { in: isShown, classNames: cx('&'), timeout: 300, unmountOnExit: true }, overlayElement)) : isShown ? (overlayElement) : null)); }; Overlay.displayName = 'Overlay'; Overlay.peek = { description: "\n\t\t\tOverlay is used to block user interaction with the rest of the app\n\t\t\tuntil they have completed something.\n\t\t", categories: ['utility'], madeFrom: ['Portal'], }; Overlay.propTypes = { className: string(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t"], ["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t"]))), children: node(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\tGenerally you should only have a single child element so the centering\n\t\t\tworks correctly.\n\t\t"], ["\n\t\t\tGenerally you should only have a single child element so the centering\n\t\t\tworks correctly.\n\t\t"]))), isShown: bool(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\t\tControls visibility.\n\t\t"], ["\n\t\t\tControls visibility.\n\t\t"]))), isAnimated: bool, isModal: bool(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\t\tDetermines if it shows with a gray background. If `false`, the\n\t\t\tbackground will be rendered but will be invisible, except for the\n\t\t\tcontents, and it won't capture any of the user click events.\n\t\t"], ["\n\t\t\tDetermines if it shows with a gray background. If \\`false\\`, the\n\t\t\tbackground will be rendered but will be invisible, except for the\n\t\t\tcontents, and it won't capture any of the user click events.\n\t\t"]))), portalId: string(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\t\tSet your own id for the `Portal` is that is opened up to contain the\n\t\t\tcontents. In practice you should never need to set this manually.\n\t\t"], ["\n\t\t\tSet your own id for the \\`Portal\\` is that is opened up to contain the\n\t\t\tcontents. In practice you should never need to set this manually.\n\t\t"]))), onEscape: func(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n\t\t\tFired when the user hits escape. Signature: `({ event, props }) => {}`\n\t\t"], ["\n\t\t\tFired when the user hits escape. Signature: \\`({ event, props }) => {}\\`\n\t\t"]))), onBackgroundClick: func(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n\t\t\tFired when the user clicks on the background, this may or may not be\n\t\t\tvisible depending on `isModal`. Signature: `({ event, props }) => {}`\n\t\t"], ["\n\t\t\tFired when the user clicks on the background, this may or may not be\n\t\t\tvisible depending on \\`isModal\\`. Signature: \\`({ event, props }) => {}\\`\n\t\t"]))), }; Overlay.defaultProps = exports.defaultProps; return Overlay; }(react_1.default.Component)); exports.default = Overlay; var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7; //# sourceMappingURL=Overlay.js.map