lucid-ui
Version:
A UI component library from AppNexus.
474 lines (393 loc) • 21.4 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import _isNil from "lodash/isNil";
import _mapValues from "lodash/mapValues";
import _keys from "lodash/keys";
import _first from "lodash/first";
import _defer from "lodash/defer";
import _get from "lodash/get";
import _noop from "lodash/noop";
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { filterTypes, omitProps } from '../../util/component-types';
import DragCaptureZone from '../DragCaptureZone/DragCaptureZone';
import { Motion, spring } from 'react-motion';
import { QUICK_SLIDE_MOTION } from '../../constants/motion-spring';
var cx = lucidClassNames.bind('&-SplitVertical');
var any = PropTypes.any,
bool = PropTypes.bool,
func = PropTypes.func,
node = PropTypes.node,
number = PropTypes.number,
string = PropTypes.string,
oneOfType = PropTypes.oneOfType;
export var SplitVerticalRightPane = function SplitVerticalRightPane(_props) {
return null;
};
SplitVerticalRightPane.displayName = 'SplitVertical.RightPane';
SplitVerticalRightPane.peek = {
description: "Right pane of the split."
};
SplitVerticalRightPane.propName = 'RightPane';
SplitVerticalRightPane.propTypes = {
children: node,
width: oneOfType([number, string]),
isPrimary: bool
};
SplitVerticalRightPane.defaultProps = {
isPrimary: false
};
var SplitVerticalLeftPane = function SplitVerticalLeftPane(_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,
width: oneOfType([number, string]),
isPrimary: bool
};
SplitVerticalLeftPane.defaultProps = {
isPrimary: false
};
var SplitVerticalDivider = function SplitVerticalDivider(_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
};
var SplitVertical = /*#__PURE__*/function (_React$Component) {
_inherits(SplitVertical, _React$Component);
var _super = _createSuper(SplitVertical);
function SplitVertical() {
var _this;
_classCallCheck(this, SplitVertical);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
isAnimated: false,
// to ensure first render doesn't show a collapse animation
isExpanded: true,
collapseAmount: 250
});
_defineProperty(_assertThisInitialized(_this), "innerRef", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "leftPaneRef", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "rightPaneRef", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "secondaryStartRect", _this.leftPaneRef.current ? _this.leftPaneRef.current.getBoundingClientRect() : null);
_defineProperty(_assertThisInitialized(_this), "getPanes", function () {
var children = _this.props.children;
var _assertThisInitialize = _assertThisInitialized(_this),
leftPaneRef = _assertThisInitialize.leftPaneRef,
rightPaneRef = _assertThisInitialize.rightPaneRef;
var leftPaneElement = _get(filterTypes(children, SplitVertical.LeftPane), 0, /*#__PURE__*/React.createElement(SplitVertical.LeftPane, null));
var rightPaneElement = _get(filterTypes(children, SplitVertical.RightPane), 0, /*#__PURE__*/React.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
};
});
_defineProperty(_assertThisInitialized(_this), "panes", _this.getPanes());
_defineProperty(_assertThisInitialized(_this), "applyDeltaToSecondaryWidth", function (dX, isExpanded, secondaryStartRect, secondaryRef, secondary, right, innerRef, primaryRef) {
var collapseShift = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : 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);
}
}
});
_defineProperty(_assertThisInitialized(_this), "expandSecondary", function () {
_this.setState({
isExpanded: true
});
});
_defineProperty(_assertThisInitialized(_this), "collapseSecondary", function (collapseAmount) {
_this.setState({
isExpanded: false,
collapseAmount: collapseAmount
});
});
_defineProperty(_assertThisInitialized(_this), "disableAnimation", function (innerRef, secondaryRef, primaryRef) {
innerRef.current.style.transitionDuration = '0s';
secondaryRef.current.style.transitionDuration = '0s';
primaryRef.current.style.transitionDuration = '0s';
});
_defineProperty(_assertThisInitialized(_this), "resetAnimation", function (innerRef, secondaryRef, primaryRef) {
innerRef.current.style.transitionDuration = '';
secondaryRef.current.style.transitionDuration = '';
primaryRef.current.style.transitionDuration = '';
});
_defineProperty(_assertThisInitialized(_this), "handleDragStart", function () {
_this.panes = _this.getPanes();
var _this$panes = _this.panes,
secondaryRef = _this$panes.secondaryRef,
primaryRef = _this$panes.primaryRef;
_this.secondaryStartRect = secondaryRef.current ? secondaryRef.current.getBoundingClientRect() : null;
_this.disableAnimation(_this.innerRef, secondaryRef, primaryRef);
});
_defineProperty(_assertThisInitialized(_this), "handleDrag", function (_ref, _ref2) {
var dX = _ref.dX;
var event = _ref2.event;
var _this$props = _this.props,
isExpanded = _this$props.isExpanded,
collapseShift = _this$props.collapseShift,
onResizing = _this$props.onResizing;
var _this$panes2 = _this.panes,
secondaryRef = _this$panes2.secondaryRef,
secondary = _this$panes2.secondary,
right = _this$panes2.right,
primaryRef = _this$panes2.primaryRef;
_this.secondaryStartRect && onResizing(_this.applyDeltaToSecondaryWidth(dX, isExpanded, _this.secondaryStartRect, secondaryRef, secondary, right, _this.innerRef, primaryRef, collapseShift), {
props: _this.props,
event: event
});
});
_defineProperty(_assertThisInitialized(_this), "handleDragEnd", function (_ref3, _ref4) {
var dX = _ref3.dX;
var event = _ref4.event;
var _this$props2 = _this.props,
isExpanded = _this$props2.isExpanded,
collapseShift = _this$props2.collapseShift,
onResize = _this$props2.onResize;
var _this$panes3 = _this.panes,
secondaryRef = _this$panes3.secondaryRef,
secondary = _this$panes3.secondary,
right = _this$panes3.right,
primaryRef = _this$panes3.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;
}
_createClass(SplitVertical, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
var isAnimated = nextProps.isAnimated,
isExpanded = nextProps.isExpanded,
collapseShift = nextProps.collapseShift;
var _this$getPanes = this.getPanes(),
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
});
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
var _this$props3 = this.props,
isAnimated = _this$props3.isAnimated,
isExpanded = _this$props3.isExpanded,
collapseShift = _this$props3.collapseShift;
var _this$getPanes2 = this.getPanes(),
secondaryRef = _this$getPanes2.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) {
_defer(function () {
_this2.setState({
isAnimated: isAnimated
});
});
}
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$props4 = this.props,
children = _this$props4.children,
className = _this$props4.className,
isResizeable = _this$props4.isResizeable,
passThroughs = _objectWithoutProperties(_this$props4, ["children", "className", "isResizeable"]);
var _this$state = this.state,
isAnimated = _this$state.isAnimated,
isExpanded = _this$state.isExpanded,
collapseAmount = _this$state.collapseAmount;
var _this$getPanes3 = this.getPanes(),
leftPaneProps = _this$getPanes3.left,
rightPaneProps = _this$getPanes3.right,
secondary = _this$getPanes3.secondary;
var dividerProps = _get(_first(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 /*#__PURE__*/React.createElement("div", _extends({}, omitProps(passThroughs, undefined, _keys(SplitVertical.propTypes)), {
className: cx('&', {
'&-is-expanded': isExpanded,
'&-is-animated': isAnimated
}, className),
style: _objectSpread({
overflow: 'hidden'
}, passThroughs.style)
}), /*#__PURE__*/React.createElement(Motion, {
defaultStyle: from,
style: isAnimated ? _mapValues(to, function (val) {
return spring(val, QUICK_SLIDE_MOTION);
}) : to
}, function (tween) {
return /*#__PURE__*/React.createElement("div", {
className: cx('&-inner'),
ref: _this3.innerRef,
style: {
display: 'flex',
transform: "translateX(".concat((isRightSecondary ? 1 : -1) * Math.round(tween.slideAmount), "px)")
}
}, /*#__PURE__*/React.createElement("div", _extends({}, omitProps(leftPaneProps, undefined, _keys(SplitVerticalLeftPane.propTypes)), {
className: cx('&-LeftPane', {
'&-is-secondary': leftPaneProps === secondary
}, leftPaneProps.className),
style: _objectSpread({
flexGrow: isRightSecondary ? 1 : 0,
flexShrink: isRightSecondary ? 1 : 0,
flexBasis: _isNil(leftPaneProps.width) ? leftPaneProps === secondary ? 'calc(50% - 3px)' : '0%' : leftPaneProps.width,
marginLeft: isRightSecondary ? -Math.round(tween.slideAmount) : undefined,
overflow: 'auto'
}, leftPaneProps.style),
ref: _this3.leftPaneRef
}), leftPaneProps.children), isResizeable ? /*#__PURE__*/React.createElement(DragCaptureZone, _extends({}, omitProps(dividerProps, undefined, _keys(SplitVerticalDivider.propTypes), false), {
className: cx('&-Divider', '&-Divider-is-resizeable', dividerProps.className),
onDragStart: _this3.handleDragStart,
onDrag: _this3.handleDrag,
onDragEnd: _this3.handleDragEnd,
style: _objectSpread({
width: '6px',
boxSizing: 'border-box'
}, dividerProps.style)
}), dividerProps.children || ' ') : /*#__PURE__*/React.createElement("div", _extends({}, omitProps(dividerProps, undefined, _keys(SplitVerticalDivider.propTypes)), {
className: cx('&-Divider', dividerProps.className)
}), dividerProps.children || ' '), /*#__PURE__*/React.createElement("div", _extends({}, omitProps(rightPaneProps, undefined, _keys(SplitVerticalRightPane.propTypes)), {
className: cx('&-RightPane', {
'&-is-secondary': rightPaneProps === secondary
}, rightPaneProps.className),
style: _objectSpread({
flexGrow: !isRightSecondary ? 1 : 0,
flexShrink: !isRightSecondary ? 1 : 0,
flexBasis: _isNil(rightPaneProps.width) ? rightPaneProps === secondary ? 'calc(50% - 3px)' : '0%' : rightPaneProps.width,
marginRight: isRightSecondary ? undefined : -Math.round(tween.slideAmount),
overflow: 'auto'
}, rightPaneProps.style),
ref: _this3.rightPaneRef
}), rightPaneProps.children));
}));
}
}]);
return SplitVertical;
}(React.Component);
_defineProperty(SplitVertical, "displayName", 'SplitVertical');
_defineProperty(SplitVertical, "peek", {
description: "\n\t\t\t`SplitVertical` renders a vertical split.\n\t\t",
categories: ['helpers'],
madeFrom: ['DragCaptureZone']
});
_defineProperty(SplitVertical, "_isPrivate", true);
_defineProperty(SplitVertical, "propTypes", {
className: any,
children: node,
isResizeable: bool,
isExpanded: bool,
isAnimated: bool,
onResizing: func,
onResize: func,
collapseShift: number,
RightPane: node,
LeftPane: node,
Divider: node
});
_defineProperty(SplitVertical, "defaultProps", {
isExpanded: true,
isAnimated: false,
collapseShift: 0,
onResizing: _noop,
onResize: _noop,
isResizeable: true
});
_defineProperty(SplitVertical, "RightPane", SplitVerticalRightPane);
_defineProperty(SplitVertical, "LeftPane", SplitVerticalLeftPane);
_defineProperty(SplitVertical, "Divider", SplitVerticalDivider);
export default SplitVertical;