UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

333 lines 21.4 kB
"use strict"; var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; 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 __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.SplitHorizontalTopPane = 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 style_helpers_1 = require("../../util/style-helpers"); var component_types_1 = require("../../util/component-types"); var DragCaptureZone_1 = __importDefault(require("../DragCaptureZone/DragCaptureZone")); var react_motion_1 = require("react-motion"); var motion_spring_1 = require("../../constants/motion-spring"); var cx = style_helpers_1.lucidClassNames.bind('&-SplitHorizontal'); var bool = prop_types_1.default.bool, func = prop_types_1.default.func, node = prop_types_1.default.node, number = prop_types_1.default.number, string = prop_types_1.default.string, oneOfType = prop_types_1.default.oneOfType; var SplitHorizontalTopPane = function (_props) { return null; }; exports.SplitHorizontalTopPane = SplitHorizontalTopPane; exports.SplitHorizontalTopPane.displayName = 'SplitHorizontal.TopPane'; exports.SplitHorizontalTopPane.peek = { description: "Top pane of the split.", }; exports.SplitHorizontalTopPane.propName = 'TopPane'; exports.SplitHorizontalTopPane.propTypes = { children: node(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\tAny valid React children.\n\t"], ["\n\t\tAny valid React children.\n\t"]))), height: oneOfType([number, string])(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\tSet height of this pane.\n\t"], ["\n\t\tSet height of this pane.\n\t"]))), isPrimary: bool(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\tDefine this pane as the primary content pane. When the split is\n\t\tcollapsed, this pane becomes full height.\n\t"], ["\n\t\tDefine this pane as the primary content pane. When the split is\n\t\tcollapsed, this pane becomes full height.\n\t"]))), }; exports.SplitHorizontalTopPane.defaultProps = { isPrimary: false, }; var SplitHorizontalBottomPane = function (_props) { return null; }; SplitHorizontalBottomPane.displayName = 'SplitHorizontal.BottomPane'; SplitHorizontalBottomPane.peek = { description: "\n\t\tBottom pane of the split.\n\t", }; SplitHorizontalBottomPane.propName = 'BottomPane'; SplitHorizontalBottomPane.propTypes = { children: node(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\tAny valid React children.\n\t"], ["\n\t\tAny valid React children.\n\t"]))), height: oneOfType([number, string])(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\tSet height of this pane.\n\t"], ["\n\t\tSet height of this pane.\n\t"]))), isPrimary: bool(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n\t\tDefine this pane as the primary content pane. When the split is\n\t\tcollapsed, this pane becomes full height.\n\t"], ["\n\t\tDefine this pane as the primary content pane. When the split is\n\t\tcollapsed, this pane becomes full height.\n\t"]))), }; SplitHorizontalBottomPane.defaultProps = { isPrimary: false, }; var SplitHorizontalDivider = function (_props) { return null; }; SplitHorizontalDivider.displayName = 'SplitHorizontal.Divider'; SplitHorizontalDivider.peek = { description: "\n\t\tThe area that separates the split panes. Can be dragged to resize\n\t\tthem.\n\t", }; SplitHorizontalDivider.propName = 'Divider'; SplitHorizontalDivider.propTypes = { children: node(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n\t\tAny valid React children.\n\t"], ["\n\t\tAny valid React children.\n\t"]))), }; var SplitHorizontal = /** @class */ (function (_super) { __extends(SplitHorizontal, _super); function SplitHorizontal() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { collapseAmount: 250, isAnimated: false, isExpanded: false, }; _this.innerRef = react_1.default.createRef(); _this.topPaneRef = react_1.default.createRef(); _this.bottomPaneRef = react_1.default.createRef(); _this.secondaryStartRect = _this.topPaneRef.current ? _this.topPaneRef.current.getBoundingClientRect() : null; _this.getPanes = function () { var children = _this.props.children; var _a = _this, topPaneRef = _a.topPaneRef, bottomPaneRef = _a.bottomPaneRef; var topPaneElement = lodash_1.default.get(component_types_1.filterTypes(children, SplitHorizontal.TopPane), 0, react_1.default.createElement(SplitHorizontal.TopPane, null)); var bottomPaneElement = lodash_1.default.get(component_types_1.filterTypes(children, SplitHorizontal.BottomPane), 0, react_1.default.createElement(SplitHorizontal.BottomPane, null)); var primaryElement, primaryRef; var secondaryElement, secondaryRef; if (topPaneElement.props.isPrimary && !bottomPaneElement.props.isPrimary) { primaryElement = topPaneElement; primaryRef = topPaneRef; secondaryElement = bottomPaneElement; secondaryRef = bottomPaneRef; } else { primaryElement = bottomPaneElement; primaryRef = bottomPaneRef; secondaryElement = topPaneElement; secondaryRef = topPaneRef; } return { top: topPaneElement.props, bottom: bottomPaneElement.props, primary: primaryElement.props, primaryRef: primaryRef, secondary: secondaryElement.props, secondaryRef: secondaryRef, }; }; _this.panes = _this.getPanes(); // Style changes to DOM nodes are updated here to shortcut the state -> render cycle for better performance. Also the Style updates in this // function are entirely transient and can be flushed with a props update to `height`. _this.applyDeltaToSecondaryHeight = function (dY, isExpanded, secondaryStartRect, secondaryRef, secondary, bottom, innerRef, primaryRef, collapseShift) { if (collapseShift === void 0) { collapseShift = 0; } if (isExpanded) { secondaryRef.current.style.flexBasis = secondaryStartRect.height + dY * (secondary === bottom ? -1 : 1) + "px"; return secondaryStartRect.height + dY * (secondary === bottom ? -1 : 1); } else { var overlapHeight = (secondary === bottom ? secondaryStartRect.height + dY : secondaryStartRect.height - dY) - collapseShift; if (overlapHeight > 0) { _this.collapseSecondary(overlapHeight); return secondaryStartRect.height - overlapHeight; } else { _this.expandSecondary(); secondaryRef.current.style.flexBasis = (dY + collapseShift) * (secondary === bottom ? -1 : 1) + "px"; return (dY + collapseShift) * (secondary === bottom ? -1 : 1); } } }; _this.expandSecondary = function () { _this.setState({ isExpanded: true }); }; _this.collapseSecondary = function (collapseAmount) { _this.setState({ isExpanded: false, collapseAmount: collapseAmount }); }; _this.disableAnimation = function (innerRef, secondaryRef, primaryRef) { innerRef.current.style.transition = 'all 0s'; secondaryRef.current.style.transition = 'all 0s'; primaryRef.current.style.transition = 'all 0s'; }; _this.resetAnimation = function (innerRef, secondaryRef, primaryRef) { innerRef.current.style.transition = ''; secondaryRef.current.style.transition = ''; primaryRef.current.style.transition = ''; }; _this.handleDragStart = function () { _this.panes = _this.getPanes(); var _a = _this.panes, secondaryRef = _a.secondaryRef, primaryRef = _a.primaryRef; _this.secondaryStartRect = secondaryRef.current ? secondaryRef.current.getBoundingClientRect() : null; _this.disableAnimation(_this.innerRef, secondaryRef, primaryRef); }; _this.handleDrag = function (_a, _b) { var dY = _a.dY; var event = _b.event; var _c = _this.props, isExpanded = _c.isExpanded, collapseShift = _c.collapseShift, onResizing = _c.onResizing; var _d = _this.panes, secondaryRef = _d.secondaryRef, secondary = _d.secondary, bottom = _d.bottom, primaryRef = _d.primaryRef; _this.secondaryStartRect && onResizing(_this.applyDeltaToSecondaryHeight(dY, isExpanded, _this.secondaryStartRect, secondaryRef, secondary, bottom, _this.innerRef, primaryRef, collapseShift), { event: event, props: _this.props }); }; _this.handleDragEnd = function (_a, _b) { var dY = _a.dY; var event = _b.event; var _c = _this.props, isExpanded = _c.isExpanded, collapseShift = _c.collapseShift, onResize = _c.onResize; var _d = _this.panes, secondaryRef = _d.secondaryRef, secondary = _d.secondary, bottom = _d.bottom, primaryRef = _d.primaryRef; _this.secondaryStartRect && onResize(_this.applyDeltaToSecondaryHeight(dY, isExpanded, _this.secondaryStartRect, secondaryRef, secondary, bottom, _this.innerRef, primaryRef, collapseShift), { event: event, props: _this.props }); _this.resetAnimation(_this.innerRef, secondaryRef, primaryRef); }; return _this; } SplitHorizontal.prototype.UNSAFE_componentWillReceiveProps = function (nextProps) { var isAnimated = nextProps.isAnimated, isExpanded = nextProps.isExpanded, collapseShift = nextProps.collapseShift; var secondaryRef = this.getPanes().secondaryRef; if (!isExpanded && // check if collapseShift changed or secondary pane collapsed (this.props.isExpanded || this.props.collapseShift !== collapseShift)) { // collapse secondary var secondaryRect = secondaryRef.current ? secondaryRef.current.getBoundingClientRect() : null; secondaryRect && this.collapseSecondary(secondaryRect.height - collapseShift); } else if (!this.props.isExpanded && isExpanded) { // expand secondary this.expandSecondary(); } if (this.state.isAnimated !== isAnimated) { this.setState({ isAnimated: isAnimated, }); } }; SplitHorizontal.prototype.componentDidMount = function () { var _this = this; var _a = this.props, isAnimated = _a.isAnimated, isExpanded = _a.isExpanded, collapseShift = _a.collapseShift; var secondaryRef = this.getPanes().secondaryRef; if (isExpanded) { // expand secondary this.expandSecondary(); } else { // collapse secondary var secondaryRect = secondaryRef.current ? secondaryRef.current.getBoundingClientRect() : null; secondaryRect && this.collapseSecondary(secondaryRect.height - collapseShift); } if (this.state.isAnimated !== isAnimated) { lodash_1.default.defer(function () { _this.setState({ isAnimated: isAnimated, }); }); } }; SplitHorizontal.prototype.render = function () { var _this = this; var _a = this.props, children = _a.children, className = _a.className, passThroughs = __rest(_a, ["children", "className"]); var _b = this.state, isAnimated = _b.isAnimated, isExpanded = _b.isExpanded, collapseAmount = _b.collapseAmount; var _c = this.getPanes(), topPaneProps = _c.top, bottomPaneProps = _c.bottom, secondary = _c.secondary; var dividerProps = lodash_1.default.get(lodash_1.default.first(component_types_1.filterTypes(children, SplitHorizontalDivider)), 'props', {}); var from, to; if (!isExpanded) { from = { slideAmount: 0 }; to = { slideAmount: collapseAmount }; } else { from = { slideAmount: 0 }; to = { slideAmount: 0 }; } var isBottomSecondary = bottomPaneProps === secondary; return (react_1.default.createElement("div", __assign({}, component_types_1.omitProps(this.props, undefined, Object.keys(SplitHorizontal.propTypes)), { className: cx('&', { '&-is-expanded': this.props.isExpanded, '&-is-animated': this.props.isAnimated, }, className), style: __assign({ flex: 1, overflow: 'hidden' }, passThroughs.style) }), react_1.default.createElement(react_motion_1.Motion, { defaultStyle: from, style: isAnimated ? lodash_1.default.mapValues(to, function (val) { return react_motion_1.spring(val, motion_spring_1.QUICK_SLIDE_MOTION); }) : to }, function (tween) { return (react_1.default.createElement("div", { className: cx('&-inner'), ref: _this.innerRef, style: { height: '100%', display: 'flex', flexDirection: 'column', transform: "translateY(" + (isBottomSecondary ? 1 : -1) * Math.round(tween.slideAmount) + "px)", } }, react_1.default.createElement("div", __assign({}, component_types_1.omitProps(topPaneProps, undefined, lodash_1.default.keys(exports.SplitHorizontalTopPane.propTypes)), { className: cx('&-TopPane', { '&-is-secondary': topPaneProps === secondary, }, topPaneProps.className), style: __assign({ flexGrow: isBottomSecondary ? 1 : 0, flexShrink: isBottomSecondary ? 1 : 0, flexBasis: lodash_1.default.isNil(topPaneProps.height) ? topPaneProps === secondary ? 'calc(50% - 3px)' : '0%' : topPaneProps.height, marginTop: isBottomSecondary ? -Math.round(tween.slideAmount) : undefined, overflow: 'auto' }, topPaneProps.style), ref: _this.topPaneRef }), topPaneProps.children), react_1.default.createElement(DragCaptureZone_1.default, __assign({}, component_types_1.omitProps(dividerProps, undefined, lodash_1.default.keys(SplitHorizontalDivider.propTypes), false), { className: cx('&-Divider', dividerProps.className), onDragStart: _this.handleDragStart, onDrag: _this.handleDrag, onDragEnd: _this.handleDragEnd, style: __assign({ height: '6px', boxSizing: 'border-box' }, dividerProps.style) }), dividerProps.children || ' '), react_1.default.createElement("div", __assign({}, component_types_1.omitProps(bottomPaneProps, undefined, lodash_1.default.keys(SplitHorizontalBottomPane.propTypes)), { className: cx('&-BottomPane', { '&-is-secondary': bottomPaneProps === secondary, }, bottomPaneProps.className), style: __assign({ flexGrow: !isBottomSecondary ? 1 : 0, flexShrink: !isBottomSecondary ? 1 : 0, flexBasis: lodash_1.default.isNil(bottomPaneProps.height) ? bottomPaneProps === secondary ? 'calc(50% - 3px)' : '0%' : bottomPaneProps.height, marginBottom: isBottomSecondary ? undefined : -Math.round(tween.slideAmount), overflow: 'auto' }, bottomPaneProps.style), ref: _this.bottomPaneRef }), bottomPaneProps.children))); }))); }; SplitHorizontal.displayName = 'SplitHorizontal'; SplitHorizontal.peek = { description: "\n\t\t\t`SplitHorizontal` renders a vertical split.\n\t\t", categories: ['helpers'], madeFrom: ['DragCaptureZone'], }; SplitHorizontal._isPrivate = true; SplitHorizontal.propTypes = { className: string(templateObject_8 || (templateObject_8 = __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"]))), children: node(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n\t\t\tDirect children must be types {Splitvertical.Toppane,\n\t\t\tSplitvertical.Divider, Splitvertical.BottomPane}.\tAll content is\n\t\t\tcomposed as children of these respective elements.\n\t\t"], ["\n\t\t\tDirect children must be types {Splitvertical.Toppane,\n\t\t\tSplitvertical.Divider, Splitvertical.BottomPane}.\tAll content is\n\t\t\tcomposed as children of these respective elements.\n\t\t"]))), isExpanded: bool(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\n\t\t\tRender as expanded or collapsed.\n\t\t"], ["\n\t\t\tRender as expanded or collapsed.\n\t\t"]))), isAnimated: bool(templateObject_11 || (templateObject_11 = __makeTemplateObject(["\n\t\t\tAllows animated expand and collapse behavior.\n\t\t"], ["\n\t\t\tAllows animated expand and collapse behavior.\n\t\t"]))), onResizing: func(templateObject_12 || (templateObject_12 = __makeTemplateObject(["\n\t\t\tCalled when the user is currently resizing the split with the Divider.\n\t\t\tSignature: `(height, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled when the user is currently resizing the split with the Divider.\n\t\t\tSignature: \\`(height, { event, props }) => {}\\`\n\t\t"]))), onResize: func(templateObject_13 || (templateObject_13 = __makeTemplateObject(["\n\t\t\tCalled when the user resizes the split with the Divider.\tSignature:\n\t\t\t`(height, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled when the user resizes the split with the Divider.\tSignature:\n\t\t\t\\`(height, { event, props }) => {}\\`\n\t\t"]))), collapseShift: number(templateObject_14 || (templateObject_14 = __makeTemplateObject(["\n\t\t\tUse this prop to shift the collapsed position by a known value.\n\t\t"], ["\n\t\t\tUse this prop to shift the collapsed position by a known value.\n\t\t"]))), TopPane: node, BottomPane: node, Divider: node, }; SplitHorizontal.defaultProps = { isExpanded: true, isAnimated: false, collapseShift: 0, onResizing: lodash_1.default.noop, onResize: lodash_1.default.noop, }; SplitHorizontal.TopPane = exports.SplitHorizontalTopPane; SplitHorizontal.BottomPane = SplitHorizontalBottomPane; SplitHorizontal.Divider = SplitHorizontalDivider; return SplitHorizontal; }(react_1.default.Component)); exports.default = SplitHorizontal; var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13, templateObject_14; //# sourceMappingURL=SplitHorizontal.js.map