UNPKG

lucid-ui

Version:

A UI component library from Xandr.

429 lines 20.5 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) { 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 }); exports.SplitHorizontalBottomPane = exports.SplitHorizontalTopPane = void 0; 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 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 = { /** Any valid React children. */ children: node, /** Set height of this pane. */ height: oneOfType([number, string]), /** Define this pane as the primary content pane. When the split is collapsed, this pane becomes full height. */ isPrimary: bool, }; exports.SplitHorizontalTopPane.defaultProps = { isPrimary: false, }; var SplitHorizontalBottomPane = function (_props) { return null; }; exports.SplitHorizontalBottomPane = SplitHorizontalBottomPane; exports.SplitHorizontalBottomPane.displayName = 'SplitHorizontal.BottomPane'; exports.SplitHorizontalBottomPane.peek = { description: "Bottom pane of the split.", }; exports.SplitHorizontalBottomPane.propName = 'BottomPane'; exports.SplitHorizontalBottomPane.propTypes = { /** Any valid React children. */ children: node, /** Set height of this pane. */ height: oneOfType([number, string]), /** Define this pane as the primary content pane. When the split is collapsed, this pane becomes full height. */ isPrimary: bool, }; exports.SplitHorizontalBottomPane.defaultProps = { isPrimary: false, }; var SplitHorizontalDivider = function (_props) { return null; }; SplitHorizontalDivider.displayName = 'SplitHorizontal.Divider'; SplitHorizontalDivider.peek = { description: "The area that separates the split panes. Can be dragged to resize them.", }; SplitHorizontalDivider.propName = 'Divider'; SplitHorizontalDivider.propTypes = { /** Any valid React children. */ children: node, }; var nonPassThroughs = [ 'className', 'children', 'isExpanded', 'isAnimated', 'onResizing', 'onResize', 'collapseShift', 'TopPane', 'BottomPane', 'Divider', 'onSelect', 'onToggle', 'initialState', 'callbackId', ]; 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((0, component_types_1.filterTypes)(children, SplitHorizontal.TopPane), 0, react_1.default.createElement(SplitHorizontal.TopPane, null)); var bottomPaneElement = lodash_1.default.get((0, 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 = "".concat(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 = "".concat((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((0, 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({}, (0, lodash_1.omit)(passThroughs, nonPassThroughs), { 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 (0, 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(".concat((isBottomSecondary ? 1 : -1) * Math.round(tween.slideAmount), "px)"), } }, react_1.default.createElement("div", __assign({}, (0, lodash_1.omit)(topPaneProps, [ 'height', 'isPrimary', 'initialState', 'callbackId', ]), { 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({}, (0, lodash_1.omit)(dividerProps, [ 'children', 'initialState', 'callbackId', ]), { 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({}, (0, lodash_1.omit)(bottomPaneProps, [ 'height', 'isPrimary', 'initialState', 'callbackId', ]), { 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: "`SplitHorizontal` renders a vertical split.", categories: ['helpers'], madeFrom: ['DragCaptureZone'], }; SplitHorizontal._isPrivate = true; SplitHorizontal.propTypes = { /** Appended to the component-specific class names set on the root element. Value is run through the \`classnames\` library. */ className: string, /** Direct children must be types {Splitvertical.Toppane, Splitvertical.Divider, Splitvertical.BottomPane}. All content is composed as children of these respective elements. */ children: node, /** Render as expanded or collapsed. */ isExpanded: bool, /** Allows animated expand and collapse behavior. */ isAnimated: bool, /** Called when the user is currently resizing the split with the Divider. Signature: \`(height, { event, props }) => {}\` */ onResizing: func, /** Called when the user resizes the split with the Divider. Signature: \`(height, { event, props }) => {}\` */ onResize: func, /** Use this prop to shift the collapsed position by a known value. */ collapseShift: number, 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 = exports.SplitHorizontalBottomPane; SplitHorizontal.Divider = SplitHorizontalDivider; return SplitHorizontal; }(react_1.default.Component)); exports.default = SplitHorizontal; //# sourceMappingURL=SplitHorizontal.js.map