UNPKG

lucid-ui

Version:

A UI component library from Xandr.

424 lines 20.7 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.SplitVerticalLeftPane = exports.SplitVerticalRightPane = 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('&-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 = { /** Any valid React children. */ children: node, /** Set width of this pane. */ width: oneOfType([number, string]), /** Define this pane as the primary content pane. When the split is collapsed, this pane becomes full width. */ isPrimary: bool, }; exports.SplitVerticalRightPane.defaultProps = { isPrimary: false, }; var SplitVerticalLeftPane = function (_props) { return null; }; exports.SplitVerticalLeftPane = SplitVerticalLeftPane; exports.SplitVerticalLeftPane.displayName = 'SplitVertical.LeftPane'; exports.SplitVerticalLeftPane.peek = { description: "Left pane of the split.", }; exports.SplitVerticalLeftPane.propName = 'LeftPane'; exports.SplitVerticalLeftPane.propTypes = { /** Any valid React children. */ children: node, /** Set width of this pane. */ width: oneOfType([number, string]), /** Define this pane as the primary content pane. When the split is collapsed, this pane becomes full width. */ isPrimary: bool, }; exports.SplitVerticalLeftPane.defaultProps = { isPrimary: false, }; /** SplitVertical Divider */ var SplitVerticalDivider = function (_props) { return null; }; SplitVerticalDivider.displayName = 'SplitVertical.Divider'; SplitVerticalDivider.peek = { description: "The area that separates the split panes. Can be dragged to resize them.", }; SplitVerticalDivider.propName = 'Divider'; SplitVerticalDivider.propTypes = { /** Any valid React children. */ children: node, }; 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((0, component_types_1.filterTypes)(children, SplitVertical.LeftPane), 0, react_1.default.createElement(SplitVertical.LeftPane, null)); var rightPaneElement = lodash_1.default.get((0, 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 = "".concat(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 = "".concat((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((0, 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({}, (0, lodash_1.omit)(passThroughs, [ 'className', 'children', 'isExpanded', 'isAnimated', 'onResizing', 'onResize', 'collapseShift', 'RightPane', 'LeftPane', 'Divider', 'initialState', 'callbackId', ]), { 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 (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: { display: 'flex', transform: "translateX(".concat((isRightSecondary ? 1 : -1) * Math.round(tween.slideAmount), "px)"), } }, react_1.default.createElement("div", __assign({}, (0, lodash_1.omit)(leftPaneProps, ['children', 'isPrimary', 'width'].concat([ 'initialState', 'callbackId', ])), { 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({}, (0, lodash_1.omit)(dividerProps, ['children'].concat('initialState')), { 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({}, (0, lodash_1.omit)(dividerProps, ['children'].concat('initialState', 'callbackId')), { className: cx('&-Divider', dividerProps.className) }), dividerProps.children || ' ')), react_1.default.createElement("div", __assign({}, (0, lodash_1.omit)(rightPaneProps, ['children', 'isPrimary', 'width'].concat([ 'initialState', 'callbackId', ])), { 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: "`SplitVertical` renders a vertical split.", categories: ['helpers'], madeFrom: ['DragCaptureZone'], }; SplitVertical._isPrivate = true; SplitVertical.propTypes = { /** Appended to the component-specific class names set on the root element. Value is run through the \`classnames\` library. */ className: any, /** Direct children must be types {SplitVertical.Leftpane, SplitVertical.Divider, SplitVertical.RightPane}. All content is composed as children of these respective elements. */ children: node, /** Allows draggable resizing of the SplitVertical */ isResizeable: bool, /** 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: \`(width, { event, props }) => {}\` */ onResizing: func, /** Called when the user resizes the split with the Divider. Signature: \`(width, { event, props }) => {}\` */ onResize: func, /** Use this prop to shift the collapsed position by a known value. */ collapseShift: number, /** Direct child of SplitVertical */ RightPane: node, /** Direct child of SplitVertical */ LeftPane: node, /** Direct child of SplitVertical. Rendered when `isResizeable` is true. */ 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 = exports.SplitVerticalLeftPane; SplitVertical.Divider = SplitVerticalDivider; return SplitVertical; }(react_1.default.Component)); exports.default = SplitVertical; //# sourceMappingURL=SplitVertical.js.map