lucid-ui
Version:
A UI component library from Xandr.
174 lines • 7.76 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) {
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 __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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultProps = exports.overlayPropTypes = void 0;
var lodash_1 = __importDefault(require("lodash"));
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("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 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.overlayPropTypes = [
'children',
'className',
'isShown',
'isAnimated',
'isModal',
'portalId',
'onEscape',
'onBackgroundClick',
];
var nonPassThroughs = __spreadArray(__spreadArray([], exports.overlayPropTypes, true), ['initialState', 'callbackId'], false);
exports.defaultProps = {
isAnimated: true,
isModal: true,
isShown: false,
onBackgroundClick: lodash_1.default.noop,
onEscape: lodash_1.default.noop,
};
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 || (0, 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({}, lodash_1.default.omit(passThroughs, nonPassThroughs), { 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: "`Overlay` is used to block user interaction with the rest of the app until something has been completed.",
categories: ['utility'],
madeFrom: ['Portal'],
};
Overlay.propTypes = {
/**
Appended to the component-specific class names set on the root element.
*/
className: string,
/**
Generally you should only have a single child element so the centering
works correctly.
*/
children: node,
/**
Controls visibility.
*/
isShown: bool,
/**
Enables animated transitions during expansion and collapse.
*/
isAnimated: bool,
/**
Determines if it shows with a gray background. If \`false\`, the
background will be rendered but will be invisible, except for the
contents, and it won't capture any of the user click events.
*/
isModal: bool,
/**
Set your own id for the \`Portal\` is that is opened up to contain the
contents. In practice you should never need to set this manually.
*/
portalId: string,
/**
Fired when the user hits escape. Signature: \`({ event, props }) => {}\`
*/
onEscape: func,
/**
Fired when the user clicks on the background, this may or may not be
visible depending on \`isModal\`. Signature: \`({ event, props }) => {}\`
*/
onBackgroundClick: func,
};
Overlay.defaultProps = exports.defaultProps;
return Overlay;
}(react_1.default.Component));
exports.default = Overlay;
//# sourceMappingURL=Overlay.js.map