UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

293 lines (237 loc) 14 kB
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 _keys from "lodash/keys"; import _get from "lodash/get"; import _map from "lodash/map"; import _first from "lodash/first"; import _noop from "lodash/noop"; 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 _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 _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 ContextMenu from '../ContextMenu/ContextMenu'; import CloseIcon from '../Icon/CloseIcon/CloseIcon'; import * as reducers from './ToolTip.reducers'; import { lucidClassNames } from '../../util/style-helpers'; import { findTypes, omitProps } from '../../util/component-types'; import { buildModernHybridComponent } from '../../util/state-management'; var cx = lucidClassNames.bind('&-ToolTip'); var flyOutCx = cx.bind('&-FlyOut'); var bool = PropTypes.bool, func = PropTypes.func, node = PropTypes.node, number = PropTypes.number, object = PropTypes.object, oneOf = PropTypes.oneOf, string = PropTypes.string, oneOfType = PropTypes.oneOfType; var Target = ContextMenu.Target, FlyOut = ContextMenu.FlyOut; var ToolTipTarget = function ToolTipTarget(_props) { return null; }; ToolTipTarget.displayName = 'ToolTip.Target'; ToolTipTarget.peek = { description: "\n\t\tThe hover target that will trigger the ToolTip to be displayed.\n\t" }; ToolTipTarget.propName = 'Target'; var ToolTipTitle = function ToolTipTitle(_props) { return null; }; ToolTipTitle.displayName = 'ToolTip.Title'; ToolTipTitle.peek = { description: "\n\t\tThe title displayed at the top of the ToolTip.\n\t" }; ToolTipTitle.propName = 'Title'; var ToolTipBody = function ToolTipBody(_props) { return null; }; ToolTipBody.displayName = 'ToolTip.Body'; ToolTipBody.peek = { description: "\n\t\tThe body of the ToolTip displayed below the Title.\n\t" }; ToolTipBody.propName = 'Body'; var ToolTip = /*#__PURE__*/function (_React$Component) { _inherits(ToolTip, _React$Component); var _super = _createSuper(ToolTip); function ToolTip(_props2) { var _this; _classCallCheck(this, ToolTip); _this = _super.call(this, _props2); _defineProperty(_assertThisInitialized(_this), "handleMouseOut", function (event) { setTimeout(function () { var _assertThisInitialize = _assertThisInitialized(_this), props = _assertThisInitialize.props, _assertThisInitialize2 = _assertThisInitialize.state, isMouseOverFlyout = _assertThisInitialize2.isMouseOverFlyout, isMouseOverTarget = _assertThisInitialize2.isMouseOverTarget, onMouseOut = _assertThisInitialize.props.onMouseOut; if (!isMouseOverFlyout && !isMouseOverTarget) { onMouseOut && onMouseOut({ props: props, event: event }); } }, 100); }); _defineProperty(_assertThisInitialized(_this), "handleMouseOverFlyout", function () { _this.setState({ isMouseOverFlyout: true }); }); _defineProperty(_assertThisInitialized(_this), "handleMouseOutFlyout", function (event) { _this.setState({ isMouseOverFlyout: false }); _this.handleMouseOut(event); }); _defineProperty(_assertThisInitialized(_this), "handleMouseOverTarget", function (event) { _this.setState({ isMouseOverTarget: true }); _this.props.onMouseOver && _this.props.onMouseOver({ props: _this.props, event: event }); }); _defineProperty(_assertThisInitialized(_this), "handleMouseOutTarget", function (event) { _this.setState({ isMouseOverTarget: false }); _this.handleMouseOut(event); }); _defineProperty(_assertThisInitialized(_this), "handleClose", function (_ref) { var event = _ref.event, props = _ref.props; _this.props.onClose && _this.props.onClose({ event: event, props: _this.props }); }); _this.state = { isMouseOverFlyout: false, isMouseOverTarget: false, isExpanded: false }; return _this; } _createClass(ToolTip, [{ key: "render", value: function render() { var _this$props = this.props, className = _this$props.className, alignment = _this$props.alignment, direction = _this$props.direction, flyOutMaxWidth = _this$props.flyOutMaxWidth, flyOutStyle = _this$props.flyOutStyle, isCloseable = _this$props.isCloseable, isExpanded = _this$props.isExpanded, isLight = _this$props.isLight, portalId = _this$props.portalId, style = _this$props.style, passThroughs = _objectWithoutProperties(_this$props, ["className", "alignment", "direction", "flyOutMaxWidth", "flyOutStyle", "isCloseable", "isExpanded", "isLight", "portalId", "style"]); var targetProps = _first(_map(findTypes(this.props, ToolTip.Target), 'props')); var title = _get(_first(_map(findTypes(this.props, ToolTip.Title), 'props')), 'children'); var body = _get(_first(_map(findTypes(this.props, ToolTip.Body), 'props')), 'children'); var getAlignmentOffset = function getAlignmentOffset(n) { return alignment === ContextMenu.CENTER ? 0 : alignment === ContextMenu.START ? n / 2 - 22.5 : -(n / 2 - 22.5); }; return /*#__PURE__*/React.createElement(ContextMenu, _extends({ className: cx('&', className) // WARNING: Alignment is always set to center because the getAlignmentOffset function // handles the alignment instead of delegating to ContextMenu , alignment: ContextMenu.CENTER, direction: direction, directonOffset: 15, getAlignmentOffset: getAlignmentOffset, isExpanded: isExpanded, style: style, portalId: portalId }, omitProps(passThroughs, undefined, _keys(ToolTip.propTypes), false), { onMouseOver: this.handleMouseOverTarget, onMouseOut: this.handleMouseOutTarget }), /*#__PURE__*/React.createElement(Target, _extends({}, targetProps, { className: cx(_get(targetProps, 'className'), '&-Target') }), _get(targetProps, 'children')), /*#__PURE__*/React.createElement(FlyOut, { style: _objectSpread(_objectSpread({}, flyOutStyle), {}, { maxWidth: flyOutMaxWidth || flyOutStyle && flyOutStyle.maxWidth || 200 }), className: flyOutCx(className, '&', "&-".concat(direction), "&-".concat(alignment), isLight ? '&-light' : '&-default'), onMouseOver: this.handleMouseOverFlyout, onMouseOut: this.handleMouseOutFlyout }, isCloseable ? /*#__PURE__*/React.createElement(CloseIcon, { isClickable: true, size: 8, onClick: this.handleClose, className: flyOutCx('&-close') }) : null, !_isNil(title) ? /*#__PURE__*/React.createElement("h2", { className: flyOutCx('&-Title') }, title) : null, body)); } }]); return ToolTip; }(React.Component); _defineProperty(ToolTip, "displayName", 'ToolTip'); _defineProperty(ToolTip, "Title", ToolTipTitle); _defineProperty(ToolTip, "Target", ToolTipTarget); _defineProperty(ToolTip, "Body", ToolTipBody); _defineProperty(ToolTip, "peek", { description: "\n\t\t\t\tA utility component that creates a transient message anchored to\n\t\t\t\tanother component.\n\t\t\t", notes: { overview: "\n\t\t\t\t\tA text popup shown on hover.\n\t\t\t\t", intendedUse: "\n\t\t\t\t\tUse to provide an explanation for a button, text, or an operation. Often used in conjunction with `HelpIcon`.\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t**Styling notes**\n\t\t\t\t\t\n\t\t\t\t\t- Use the {direction} and {alignment} that best suit your layout.\n\t\t\t\t\t- Tooltip titles should fit on a single line and not wrap.\n\t\t\t\t\t- Use black tooltips in most interactions. White tooltips are reserved for use within charts, for example `LineChart`.\n\t\t\t\t", technicalRecommendations: "\n\t\t\t\t" }, categories: ['communication'], madeFrom: ['ContextMenu'] }); _defineProperty(ToolTip, "reducers", reducers); _defineProperty(ToolTip, "propTypes", { children: node, className: string, isCloseable: bool, isLight: bool, onClose: func, style: object, flyOutStyle: object, flyOutMaxWidth: oneOfType([number, string]), direction: oneOf(['down', 'up', 'right', 'left']), alignment: oneOf(['start', 'center', 'end']), isExpanded: bool, onMouseOver: func, onMouseOut: func, portalId: string, Body: node, Title: node, Target: node }); _defineProperty(ToolTip, "defaultProps", { alignment: ContextMenu.CENTER, direction: ContextMenu.UP, flyOutStyle: {}, isCloseable: false, isExpanded: false, isLight: false, onClose: _noop, onMouseOut: _noop, onMouseOver: _noop, portalId: null }); export default buildModernHybridComponent(ToolTip, { reducers: reducers }); export { ToolTip as ToolTipDumb };