UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

327 lines 21.6 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.SplitVerticalRightPane = 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('&-SplitVertical'); var any = prop_types_1.default.any, 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 SplitVerticalRightPane = function (_props) { return null; }; exports.SplitVerticalRightPane = SplitVerticalRightPane; exports.SplitVerticalRightPane.displayName = 'SplitVertical.RightPane'; exports.SplitVerticalRightPane.peek = { description: "Right pane of the split.", }; exports.SplitVerticalRightPane.propName = 'RightPane'; exports.SplitVerticalRightPane.propTypes = { children: node(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\tAny valid React children.\n\t"], ["\n\t\tAny valid React children.\n\t"]))), width: oneOfType([number, string])(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\tSet width of this pane.\n\t"], ["\n\t\tSet width 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 width.\n\t"], ["\n\t\tDefine this pane as the primary content pane. When the split is\n\t\tcollapsed, this pane becomes full width.\n\t"]))), }; exports.SplitVerticalRightPane.defaultProps = { isPrimary: false, }; var SplitVerticalLeftPane = function (_props) { return null; }; SplitVerticalLeftPane.displayName = 'SplitVertical.LeftPane'; SplitVerticalLeftPane.peek = { description: "\n\t\tLeft pane of the split.\n\t", }; SplitVerticalLeftPane.propName = 'LeftPane'; SplitVerticalLeftPane.propTypes = { children: node(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\tAny valid React children.\n\t"], ["\n\t\tAny valid React children.\n\t"]))), width: oneOfType([number, string])(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\tSet width of this pane.\n\t"], ["\n\t\tSet width 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 width.\n\t"], ["\n\t\tDefine this pane as the primary content pane. When the split is\n\t\tcollapsed, this pane becomes full width.\n\t"]))), }; SplitVerticalLeftPane.defaultProps = { isPrimary: false, }; var SplitVerticalDivider = function (_props) { return null; }; SplitVerticalDivider.displayName = 'SplitVertical.Divider'; SplitVerticalDivider.peek = { description: "\n\t\tThe area that separates the split panes. Can be dragged to resize\n\t\tthem.\n\t", }; SplitVerticalDivider.propName = 'Divider'; SplitVerticalDivider.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 SplitVertical = /** @class */ (function (_super) { __extends(SplitVertical, _super); function SplitVertical() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { isAnimated: false, isExpanded: true, collapseAmount: 250, }; _this.innerRef = react_1.default.createRef(); _this.leftPaneRef = react_1.default.createRef(); _this.rightPaneRef = react_1.default.createRef(); _this.secondaryStartRect = _this.leftPaneRef.current ? _this.leftPaneRef.current.getBoundingClientRect() : null; _this.getPanes = function () { var children = _this.props.children; var _a = _this, leftPaneRef = _a.leftPaneRef, rightPaneRef = _a.rightPaneRef; var leftPaneElement = lodash_1.default.get(component_types_1.filterTypes(children, SplitVertical.LeftPane), 0, react_1.default.createElement(SplitVertical.LeftPane, null)); var rightPaneElement = lodash_1.default.get(component_types_1.filterTypes(children, SplitVertical.RightPane), 0, react_1.default.createElement(SplitVertical.RightPane, null)); var primaryElement, primaryRef; var secondaryElement, secondaryRef; if (leftPaneElement.props.isPrimary && !rightPaneElement.props.isPrimary) { primaryElement = leftPaneElement; primaryRef = leftPaneRef; secondaryElement = rightPaneElement; secondaryRef = rightPaneRef; } else { primaryElement = rightPaneElement; primaryRef = rightPaneRef; secondaryElement = leftPaneElement; secondaryRef = leftPaneRef; } return { left: leftPaneElement.props, right: rightPaneElement.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 `width`. _this.applyDeltaToSecondaryWidth = function (dX, isExpanded, secondaryStartRect, secondaryRef, secondary, right, innerRef, primaryRef, collapseShift) { if (collapseShift === void 0) { collapseShift = 0; } if (isExpanded) { secondaryRef.current.style.flexBasis = secondaryStartRect.width + dX * (secondary === right ? -1 : 1) + "px"; return secondaryStartRect.width + dX * (secondary === right ? -1 : 1); } else { var overlapWidth = (secondary === right ? secondaryStartRect.width + dX : secondaryStartRect.width - dX) - collapseShift; if (overlapWidth > 0) { _this.collapseSecondary(overlapWidth); return secondaryStartRect.width - overlapWidth; } else { _this.expandSecondary(); secondaryRef.current.style.flexBasis = (dX + collapseShift) * (secondary === right ? -1 : 1) + "px"; return (dX + collapseShift) * (secondary === right ? -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.transitionDuration = '0s'; secondaryRef.current.style.transitionDuration = '0s'; primaryRef.current.style.transitionDuration = '0s'; }; _this.resetAnimation = function (innerRef, secondaryRef, primaryRef) { innerRef.current.style.transitionDuration = ''; secondaryRef.current.style.transitionDuration = ''; primaryRef.current.style.transitionDuration = ''; }; _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 dX = _a.dX; 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, right = _d.right, primaryRef = _d.primaryRef; _this.secondaryStartRect && onResizing(_this.applyDeltaToSecondaryWidth(dX, isExpanded, _this.secondaryStartRect, secondaryRef, secondary, right, _this.innerRef, primaryRef, collapseShift), { props: _this.props, event: event }); }; _this.handleDragEnd = function (_a, _b) { var dX = _a.dX; 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, right = _d.right, primaryRef = _d.primaryRef; _this.secondaryStartRect && onResize(_this.applyDeltaToSecondaryWidth(dX, isExpanded, _this.secondaryStartRect, secondaryRef, secondary, right, _this.innerRef, primaryRef, collapseShift), { props: _this.props, event: event }); _this.resetAnimation(_this.innerRef, secondaryRef, primaryRef); }; return _this; } SplitVertical.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.getBoundingClientRect(); this.collapseSecondary(secondaryRect.width - collapseShift); } else if (!this.props.isExpanded && isExpanded) { // expand secondary this.expandSecondary(); } if (this.state.isAnimated !== isAnimated) { this.setState({ isAnimated: isAnimated, }); } }; SplitVertical.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.getBoundingClientRect(); this.collapseSecondary(secondaryRect.width - collapseShift); } if (this.state.isAnimated !== isAnimated) { lodash_1.default.defer(function () { _this.setState({ isAnimated: isAnimated, }); }); } }; SplitVertical.prototype.render = function () { var _this = this; var _a = this.props, children = _a.children, className = _a.className, isResizeable = _a.isResizeable, passThroughs = __rest(_a, ["children", "className", "isResizeable"]); var _b = this.state, isAnimated = _b.isAnimated, isExpanded = _b.isExpanded, collapseAmount = _b.collapseAmount; var _c = this.getPanes(), leftPaneProps = _c.left, rightPaneProps = _c.right, secondary = _c.secondary; var dividerProps = lodash_1.default.get(lodash_1.default.first(component_types_1.filterTypes(children, SplitVertical.Divider)), 'props', {}); var from, to; if (!isExpanded) { from = { slideAmount: 0 }; to = { slideAmount: collapseAmount }; } else { from = { slideAmount: 0 }; to = { slideAmount: 0 }; } var isRightSecondary = rightPaneProps === secondary; return (react_1.default.createElement("div", __assign({}, component_types_1.omitProps(passThroughs, undefined, lodash_1.default.keys(SplitVertical.propTypes)), { className: cx('&', { '&-is-expanded': isExpanded, '&-is-animated': isAnimated, }, className), style: __assign({ 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: { display: 'flex', transform: "translateX(" + (isRightSecondary ? 1 : -1) * Math.round(tween.slideAmount) + "px)", } }, react_1.default.createElement("div", __assign({}, component_types_1.omitProps(leftPaneProps, undefined, lodash_1.default.keys(SplitVerticalLeftPane.propTypes)), { className: cx('&-LeftPane', { '&-is-secondary': leftPaneProps === secondary, }, leftPaneProps.className), style: __assign({ flexGrow: isRightSecondary ? 1 : 0, flexShrink: isRightSecondary ? 1 : 0, flexBasis: lodash_1.default.isNil(leftPaneProps.width) ? leftPaneProps === secondary ? 'calc(50% - 3px)' : '0%' : leftPaneProps.width, marginLeft: isRightSecondary ? -Math.round(tween.slideAmount) : undefined, overflow: 'auto' }, leftPaneProps.style), ref: _this.leftPaneRef }), leftPaneProps.children), isResizeable ? (react_1.default.createElement(DragCaptureZone_1.default, __assign({}, component_types_1.omitProps(dividerProps, undefined, lodash_1.default.keys(SplitVerticalDivider.propTypes), false), { className: cx('&-Divider', '&-Divider-is-resizeable', dividerProps.className), onDragStart: _this.handleDragStart, onDrag: _this.handleDrag, onDragEnd: _this.handleDragEnd, style: __assign({ width: '6px', boxSizing: 'border-box' }, dividerProps.style) }), dividerProps.children || ' ')) : (react_1.default.createElement("div", __assign({}, component_types_1.omitProps(dividerProps, undefined, lodash_1.default.keys(SplitVerticalDivider.propTypes)), { className: cx('&-Divider', dividerProps.className) }), dividerProps.children || ' ')), react_1.default.createElement("div", __assign({}, component_types_1.omitProps(rightPaneProps, undefined, lodash_1.default.keys(exports.SplitVerticalRightPane.propTypes)), { className: cx('&-RightPane', { '&-is-secondary': rightPaneProps === secondary, }, rightPaneProps.className), style: __assign({ flexGrow: !isRightSecondary ? 1 : 0, flexShrink: !isRightSecondary ? 1 : 0, flexBasis: lodash_1.default.isNil(rightPaneProps.width) ? rightPaneProps === secondary ? 'calc(50% - 3px)' : '0%' : rightPaneProps.width, marginRight: isRightSecondary ? undefined : -Math.round(tween.slideAmount), overflow: 'auto' }, rightPaneProps.style), ref: _this.rightPaneRef }), rightPaneProps.children))); }))); }; SplitVertical.displayName = 'SplitVertical'; SplitVertical.peek = { description: "\n\t\t\t`SplitVertical` renders a vertical split.\n\t\t", categories: ['helpers'], madeFrom: ['DragCaptureZone'], }; SplitVertical._isPrivate = true; SplitVertical.propTypes = { className: any(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.Leftpane,\n\t\t\tSplitvertical.Divider, Splitvertical.RightPane}. All content is composed\n\t\t\tas children of these respective elements.\n\t\t"], ["\n\t\t\tDirect children must be types {Splitvertical.Leftpane,\n\t\t\tSplitvertical.Divider, Splitvertical.RightPane}. All content is composed\n\t\t\tas children of these respective elements.\n\t\t"]))), isResizeable: bool(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\n\t\t\tAllows draggable resizing of the SplitVertical\n\t\t"], ["\n\t\t\tAllows draggable resizing of the SplitVertical\n\t\t"]))), isExpanded: bool(templateObject_11 || (templateObject_11 = __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_12 || (templateObject_12 = __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_13 || (templateObject_13 = __makeTemplateObject(["\n\t\t\tCalled when the user is currently resizing the split with the Divider.\n\t\t\tSignature: `(width, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled when the user is currently resizing the split with the Divider.\n\t\t\tSignature: \\`(width, { event, props }) => {}\\`\n\t\t"]))), onResize: func(templateObject_14 || (templateObject_14 = __makeTemplateObject(["\n\t\t\tCalled when the user resizes the split with the Divider. Signature:\n\t\t\t`(width, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled when the user resizes the split with the Divider. Signature:\n\t\t\t\\`(width, { event, props }) => {}\\`\n\t\t"]))), collapseShift: number(templateObject_15 || (templateObject_15 = __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"]))), RightPane: node, LeftPane: node, Divider: node, }; SplitVertical.defaultProps = { isExpanded: true, isAnimated: false, collapseShift: 0, onResizing: lodash_1.default.noop, onResize: lodash_1.default.noop, isResizeable: true, }; SplitVertical.RightPane = exports.SplitVerticalRightPane; SplitVertical.LeftPane = SplitVerticalLeftPane; SplitVertical.Divider = SplitVerticalDivider; return SplitVertical; }(react_1.default.Component)); exports.default = SplitVertical; 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, templateObject_15; //# sourceMappingURL=SplitVertical.js.map