lucid-ui
Version:
A UI component library from AppNexus.
288 lines (235 loc) • 13.2 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 _isNumber from "lodash/isNumber";
import _keys from "lodash/keys";
import _first from "lodash/first";
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, findTypes, omitProps } from '../../util/component-types';
import { buildModernHybridComponent } from '../../util/state-management';
import * as reducers from './Sidebar.reducers';
import SplitVertical from '../SplitVertical/SplitVertical';
import Button from '../Button/Button';
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
import GripperVerticalIcon from '../Icon/GripperVerticalIcon/GripperVerticalIcon';
var cx = lucidClassNames.bind('&-Sidebar');
var any = PropTypes.any,
bool = PropTypes.bool,
func = PropTypes.func,
node = PropTypes.node,
number = PropTypes.number,
string = PropTypes.string,
object = PropTypes.object,
oneOf = PropTypes.oneOf,
oneOfType = PropTypes.oneOfType;
var defaultProps = {
isExpanded: true,
isAnimated: true,
width: 250,
position: 'left',
isResizeDisabled: false,
onResizing: _noop,
onResize: _noop,
onToggle: _noop
};
var Primary = function Primary(_props) {
return null;
};
Primary.peek = {
description: "\n\t\tMain pane content that will have a paired `Bar`.\n\t"
};
Primary.displayName = 'SplitHorizontal.Primary';
Primary.propName = 'Primary';
var Title = function Title(_props) {
return null;
};
Title.peek = {
description: "\n\t\tSidebar title;\n\t"
};
Title.propTypes = {
children: node
};
Title.propTypes = {
children: node
};
Title.displayName = 'Sidebar.Title';
Title.propName = ['Title', 'title'];
var Bar = function Bar(_props) {
return null;
};
Bar.peek = {
description: "\n\t\tContent to be placed alongside the Primary pane.\n\t"
};
Bar.displayName = 'Sidebar.Bar';
Bar.propName = 'Bar';
Bar.propTypes = {
Title: any,
title: any,
hasGutters: bool
};
Bar.defaultProps = {
hasGutters: true
};
var Sidebar = /*#__PURE__*/function (_React$Component) {
_inherits(Sidebar, _React$Component);
var _super = _createSuper(Sidebar);
function Sidebar() {
var _this;
_classCallCheck(this, Sidebar);
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), "handleExpanderClick", function (event) {
var onToggle = _this.props.onToggle;
onToggle && onToggle({
props: _this.props,
event: event
});
});
_defineProperty(_assertThisInitialized(_this), "handleResizing", function (width, _ref) {
var event = _ref.event;
var onResizing = _this.props.onResizing;
onResizing && onResizing(width, {
props: _this.props,
event: event
});
});
_defineProperty(_assertThisInitialized(_this), "handleResize", function (width, _ref2) {
var event = _ref2.event;
var onResize = _this.props.onResize;
onResize && onResize(width, {
props: _this.props,
event: event
});
});
return _this;
}
_createClass(Sidebar, [{
key: "render",
value: function render() {
var _this$props = this.props,
children = _this$props.children,
style = _this$props.style,
className = _this$props.className,
isExpanded = _this$props.isExpanded,
isAnimated = _this$props.isAnimated,
position = _this$props.position,
isResizeDisabled = _this$props.isResizeDisabled,
width = _this$props.width,
passThroughs = _objectWithoutProperties(_this$props, ["children", "style", "className", "isExpanded", "isAnimated", "position", "isResizeDisabled", "width"]);
var primaryProps = _get(_first(filterTypes(children, Sidebar.Primary)), 'props', {}); // props from first Primary
var barProps = _get(_first(filterTypes(children, Sidebar.Bar)), 'props', {}); // props from first Bar
var titleProps = _get(findTypes(barProps, Sidebar.Title).concat(findTypes(this.props, Sidebar.Title)), // get titles from Bar and parent Sidebar
'[0].props', // select props from the first title element
{
children: 'Title'
} // default props
);
var PrimaryPane, BarPane; // using Left/Right Pane as primary depends on position
if (position !== 'right') {
PrimaryPane = SplitVertical.RightPane;
BarPane = SplitVertical.LeftPane;
} else {
PrimaryPane = SplitVertical.LeftPane;
BarPane = SplitVertical.RightPane;
}
return /*#__PURE__*/React.createElement(SplitVertical, _extends({}, omitProps(passThroughs, undefined, _keys(Sidebar.propTypes), false), {
style: _objectSpread({
minWidth: isExpanded ? _isNumber(width) ? width + 6 : "calc(".concat(width, " + 6px)") : undefined
}, style),
className: cx('&', {
'&-is-resize-disabled': isResizeDisabled,
'&-is-position-right': position === 'right',
'&-is-position-left': position !== 'right'
}, className),
isAnimated: isAnimated,
isExpanded: isExpanded,
collapseShift: 33 // leave 33px of sidebar to stick out when collapsed
,
onResizing: this.handleResizing,
onResize: this.handleResize
}), /*#__PURE__*/React.createElement(BarPane, _extends({}, omitProps(barProps, undefined, _keys(Sidebar.Bar.propTypes), false), {
className: cx('&-Bar', barProps.className),
width: width,
style: {
overflow: isExpanded ? 'auto' : 'hidden'
}
}), /*#__PURE__*/React.createElement("div", {
className: cx('&-Bar-overlay')
}), /*#__PURE__*/React.createElement("div", {
className: cx('&-Bar-header')
}, /*#__PURE__*/React.createElement("div", _extends({}, titleProps, {
className: cx('&-Bar-Title', titleProps.className)
})), /*#__PURE__*/React.createElement(Button, {
className: cx('&-expander'),
kind: "invisible",
onMouseDown: this.handleExpanderClick,
hasOnlyIcon: true
}, /*#__PURE__*/React.createElement(ChevronIcon, {
direction: isExpanded && position === 'right' || !isExpanded && position !== 'right' ? 'right' : 'left'
}))), /*#__PURE__*/React.createElement("div", {
className: cx('&-Bar-content', {
'&-Bar-content-has-gutters': barProps.hasGutters
})
}, barProps.children)), /*#__PURE__*/React.createElement(SplitVertical.Divider, {
className: cx('&-Divider')
}, /*#__PURE__*/React.createElement(GripperVerticalIcon, {
className: cx('&-Divider-gripper')
})), /*#__PURE__*/React.createElement(PrimaryPane, _extends({}, primaryProps, {
className: cx('&-Primary', primaryProps.className),
isPrimary: true
})));
}
}]);
return Sidebar;
}(React.Component);
_defineProperty(Sidebar, "displayName", 'Sidebar');
_defineProperty(Sidebar, "Bar", Bar);
_defineProperty(Sidebar, "Primary", Primary);
_defineProperty(Sidebar, "Title", Title);
_defineProperty(Sidebar, "peek", {
description: "\n\t\t\t\t\t`Sidebar` renders a collapsible, resizeable side bar panel next to\n\t\t\t\t\tprimary content.\n\t\t\t\t",
categories: ['layout']
});
_defineProperty(Sidebar, "reducers", reducers);
_defineProperty(Sidebar, "propTypes", {
style: object,
className: string,
children: node,
width: oneOfType([number, string]),
isExpanded: bool,
isAnimated: bool,
position: oneOf(['left', 'right']),
isResizeDisabled: bool,
title: any,
Title: any,
Bar: any,
Primary: any,
onResizing: func,
onResize: func,
onToggle: func
});
_defineProperty(Sidebar, "defaultProps", defaultProps);
export default buildModernHybridComponent(Sidebar, {
reducers: reducers
});
export { Sidebar as SidebarDumb };