lucid-ui
Version:
A UI component library from Xandr.
285 lines • 12.7 kB
JavaScript
"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 });
var lodash_1 = __importStar(require("lodash"));
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var style_helpers_1 = require("../../util/style-helpers");
var Overlay_1 = __importDefault(require("../Overlay/Overlay"));
var GripperVerticalIcon_1 = __importDefault(require("../Icon/GripperVerticalIcon/GripperVerticalIcon"));
var CloseIcon_1 = __importDefault(require("../Icon/CloseIcon/CloseIcon"));
var DragCaptureZone_1 = __importDefault(require("../DragCaptureZone/DragCaptureZone"));
var Button_1 = __importDefault(require("../Button/Button"));
var component_types_1 = require("../../util/component-types");
var cx = style_helpers_1.lucidClassNames.bind('&-SidePanel');
var any = prop_types_1.default.any, bool = prop_types_1.default.bool, func = prop_types_1.default.func, oneOf = prop_types_1.default.oneOf, node = prop_types_1.default.node, number = prop_types_1.default.number, string = prop_types_1.default.string, oneOfType = prop_types_1.default.oneOfType;
/* SidePanel Header **/
var SidePanelHeader = function (_props) { return null; };
SidePanelHeader.displayName = 'SidePanel.Header';
SidePanelHeader.propName = 'Header';
SidePanelHeader.peek = {
description: "\n\t\tDefines the Header content of SidePanel. If no content is defined, it will\n\t\tstill show the close button.\n\t",
};
SidePanelHeader.propTypes = {
/**
Children that will be rendered.
*/
children: node,
};
/** TODO: Remove the 'nonPassThroughs' when the component is converted to a functional component */
var nonPassThroughs = [
'children',
'className',
'Header',
'isAnimated',
'isExpanded',
'isResizeDisabled',
'onCollapse',
'onResize',
'position',
'preventBodyScroll',
'width',
'minWidth',
'maxWidth',
'topOffset',
'initialState',
];
var SidePanel = /** @class */ (function (_super) {
__extends(SidePanel, _super);
function SidePanel() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
isResizing: false,
width: _this.props.width,
startWidth: _this.props.width,
isExpanded: _this.props.isExpanded,
};
_this.timerId = setTimeout(function () {
return;
}, 1);
_this.handleResizeStart = function () {
_this.setState({
isResizing: true,
});
};
_this.handleResize = function (_a) {
var dX = _a.dX;
var startWidth = _this.state.startWidth;
var position = startWidth + dX * (_this.props.position === 'right' ? -1 : 1);
_this.setState({
width: lodash_1.default.clamp(position, _this.props.minWidth, _this.props.maxWidth === Infinity
? window.innerWidth
: _this.props.maxWidth),
});
};
_this.handleResizeEnd = function (_a, _b) {
var dX = _a.dX;
var event = _b.event;
var _c = _this.state, startWidth = _c.startWidth, width = _c.width;
_this.setState({
width: startWidth + dX * (_this.props.position === 'right' ? -1 : 1),
isResizing: false,
startWidth: width,
});
_this.props.onResize(startWidth - dX, { props: _this.props, event: event });
};
_this.handleCollapse = function (_a) {
var event = _a.event;
_this.props.onCollapse({ event: event, props: _this.props });
};
return _this;
}
SidePanel.prototype.componentDidUpdate = function (prevProps) {
var _this = this;
if (prevProps.isExpanded !== this.props.isExpanded) {
this.timerId = setTimeout(function () {
_this.setState({
isExpanded: _this.props.isExpanded,
});
}, 1);
}
};
SidePanel.prototype.componentWillUnmount = function () {
var preventBodyScroll = this.props.preventBodyScroll;
if (this.timerId) {
clearTimeout(this.timerId);
}
if (preventBodyScroll) {
window.document.body.style.overflow = '';
}
};
SidePanel.prototype.render = function () {
var _a = this.props, children = _a.children, className = _a.className, isAnimated = _a.isAnimated, isExpanded = _a.isExpanded, isResizeDisabled = _a.isResizeDisabled, position = _a.position, preventBodyScroll = _a.preventBodyScroll, topOffset = _a.topOffset, passThroughs = __rest(_a, ["children", "className", "isAnimated", "isExpanded", "isResizeDisabled", "position", "preventBodyScroll", "topOffset"]);
var headerEl = (0, component_types_1.getFirst)(this.props, SidePanel.Header);
var headerChildren = lodash_1.default.get(headerEl, 'props.children');
if (preventBodyScroll) {
window.document.body.style.overflow = isExpanded ? 'hidden' : '';
}
return (react_1.default.createElement(Overlay_1.default, __assign({ className: cx('&', {
'&-is-expanded': isExpanded && this.state.isExpanded,
'&-position-left': position === 'left',
'&-position-right': position === 'right',
'&-is-animated': isAnimated,
}, className), isShown: isExpanded || this.state.isExpanded, onBackgroundClick: this.handleCollapse, onEscape: this.handleCollapse, isAnimated: isAnimated, style: {
marginTop: topOffset,
} }, (0, lodash_1.omit)(passThroughs, nonPassThroughs)),
react_1.default.createElement("div", { className: cx('&-pane'), style: {
width: this.state.width,
marginTop: topOffset,
} },
headerEl && (react_1.default.createElement("div", { className: cx('&-header') },
react_1.default.createElement("div", { className: cx('&-header-inner-wrapper') },
react_1.default.createElement("div", { className: cx('&-header-content') }, headerChildren),
react_1.default.createElement(Button_1.default, { className: cx('&-header-closer-button'), kind: 'invisible', onClick: this.handleCollapse, hasOnlyIcon: true },
react_1.default.createElement(CloseIcon_1.default, { className: cx('&-header-closer'), isClickable: true, size: 14 }))))),
react_1.default.createElement("div", { className: cx('&-body') },
!isResizeDisabled && (react_1.default.createElement(DragCaptureZone_1.default, { className: cx('&-grabber'), onDragStart: this.handleResizeStart, onDrag: this.handleResize, onDragEnd: this.handleResizeEnd },
react_1.default.createElement(GripperVerticalIcon_1.default, { width: '20' }))),
react_1.default.createElement("div", { className: cx('&-content') }, children)))));
};
SidePanel.displayName = 'SidePanel';
SidePanel.peek = {
description: "A fixed-positioned overlay positioned on the side of the screen at full screen height. Supports variable widths resized by the user or defined as a prop. Animated collapse and expand with optional Header and closer.",
categories: ['layout'],
};
SidePanel.propTypes = {
/**
Content of the SidePanel, but also accepts \`<SidePanel.Header>\` to define
header content.
*/
children: node,
/**
Appended to the component-specific class names set on the root element.
*/
className: string,
/**
Alternative to using \`<SidePanel.Header>\`.
*/
Header: any,
/**
Enables animated transitions during expansion and collapse.
*/
isAnimated: bool,
/**
Controls the expanded/collapsed state as a boolean prop.
*/
isExpanded: bool,
/**
When true, hides the resizer at the edge of the SidePanel.
*/
isResizeDisabled: bool,
/**
Callback triggered when user clicks the background, hits the Esc key, or
clicks the close button in the Header.
Signature: \`({ event, props }) => {}\`
*/
onCollapse: func,
/**
Callback triggered after a user resizes to a new width.
Signature: \`(width, { event, props }) => {}\`
*/
onResize: func,
/**
Controls the position on the screen.
*/
position: oneOf(['left', 'right']),
/**
When true, it will prevent scrolling in the background when \`isExpanded\`
is true. This is accomplished by setting \`document.body.style.overflow =
'hidden'\`.
*/
preventBodyScroll: bool,
/**
Sets the initial width in pixels. The actual width may change if the user
resizes it.
*/
width: number,
/**
Sets the minimum width of the Side Panel.
*/
minWidth: number,
/**
Sets the maximum width of the Side Panel.
*/
maxWidth: number,
/**
Sets the top margin for the panel. Defaults to \`0\`.
*/
topOffset: oneOfType([number, string]),
};
SidePanel.defaultProps = {
isAnimated: true,
isExpanded: true,
isResizeDisabled: false,
onCollapse: lodash_1.default.noop,
onResize: lodash_1.default.noop,
position: 'right',
preventBodyScroll: false,
topOffset: 0,
width: 500,
minWidth: 500,
maxWidth: 1200,
};
SidePanel.Header = SidePanelHeader;
return SidePanel;
}(react_1.default.Component));
exports.default = SidePanel;
//# sourceMappingURL=SidePanel.js.map