UNPKG

@catho/quantum

Version:
152 lines (151 loc) 9.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _options = _interopRequireDefault(require("./options")); var _theme = require("../shared/theme"); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["children", "placement", "text", "visible", "theme", "multiline"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var Tip = _styledComponents["default"].div.withConfig({ displayName: "Tooltip__Tip", componentId: "sc-1s2fcfh-0" })(["border-radius:4px;font-weight:bold;opacity:", ";visibility:", ";position:absolute;line-height:0;text-align:center;transition:opacity 0.2s ease-in-out,visibility 0.2s ease-in-out;z-index:100;", " ", ";&:before{content:'';position:absolute;", ";}"], function (_ref) { var visible = _ref.visible; return visible ? '1' : '0'; }, function (_ref2) { var visible = _ref2.visible; return visible ? 'visible' : 'hidden'; }, function (_ref3) { var _ref3$theme = _ref3.theme, neutral = _ref3$theme.colors.neutral, xsmall = _ref3$theme.spacing.xsmall, baseFontSize = _ref3$theme.baseFontSize; return "\n background-color: ".concat(neutral[700], ";\n border-color: ").concat(neutral[700], ";\n color: ").concat(neutral[100], ";\n font-size: ").concat(baseFontSize, "px;\n padding: ").concat(xsmall, "px;\n "); }, function (_ref4) { var placement = _ref4.placement; return _options["default"].tipPosition[placement]; }, function (_ref5) { var placement = _ref5.placement; return _options["default"].arrowPosition[placement]; }); var TipText = _styledComponents["default"].span.withConfig({ displayName: "Tooltip__TipText", componentId: "sc-1s2fcfh-1" })(["display:inline-block;max-width:", ";overflow:hidden;text-overflow:ellipsis;white-space:", ";line-height:20px;text-align:", ";"], function (_ref6) { var multiline = _ref6.multiline; return multiline ? 'unset' : '250px'; }, function (_ref7) { var multiline = _ref7.multiline; return multiline ? 'pre' : 'nowrap'; }, function (_ref8) { var multiline = _ref8.multiline; return multiline ? 'left' : 'unset'; }); var Wrapper = _styledComponents["default"].div.withConfig({ displayName: "Tooltip__Wrapper", componentId: "sc-1s2fcfh-2" })(["position:relative;float:left;clear:left;width:", ";"], function (_ref9) { var multiline = _ref9.multiline; return multiline ? 'max-content' : 'unset'; }); var Tooltip = /*#__PURE__*/function (_Component) { function Tooltip(props) { var _this; _classCallCheck(this, Tooltip); _this = _callSuper(this, Tooltip, [props]); _defineProperty(_this, "isVisible", function (visible) { return _this.setState({ visible: visible }); }); var _visible = _this.props.visible; _this.state = { visible: _visible }; return _this; } _inherits(Tooltip, _Component); return _createClass(Tooltip, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, children = _this$props.children, placement = _this$props.placement, text = _this$props.text, visibleProp = _this$props.visible, theme = _this$props.theme, multiline = _this$props.multiline, rest = _objectWithoutProperties(_this$props, _excluded); var visibleState = this.state.visible; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Wrapper, _objectSpread(_objectSpread({ onMouseEnter: function onMouseEnter() { return _this2.isVisible(true); }, onMouseLeave: function onMouseLeave() { return _this2.isVisible(false); }, multiline: multiline }, rest), {}, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Tip, { placement: placement, visible: visibleProp || visibleState, theme: theme, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(TipText, { multiline: multiline, children: text }) }), children] })); } }]); }(_react.Component); Tip.displayName = 'Tip'; Tooltip.propTypes = { /** Content the tooltip will show */ text: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].element]).isRequired, /** Define tooltip positioning */ placement: _propTypes["default"].oneOf(['top', 'right', 'bottom', 'left']), visible: _propTypes["default"].bool, multiline: _propTypes["default"].bool, children: _propTypes["default"].oneOfType([_propTypes["default"].arrayOf(_propTypes["default"].node), _propTypes["default"].node]).isRequired, theme: _propTypes["default"].shape({ spacing: _propTypes["default"].object, colors: _propTypes["default"].object, baseFontSize: _propTypes["default"].number }) }; Tooltip.defaultProps = { placement: 'top', visible: false, multiline: false, theme: { spacing: _theme.spacing, colors: _theme.colors, baseFontSize: _theme.baseFontSize } }; var _default = exports["default"] = Tooltip;