UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

252 lines (248 loc) 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.TooltipBase = exports.Tooltip = void 0; exports.defaultArrowAnchor = defaultArrowAnchor; exports.defaultDirection = defaultDirection; var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types")); var _kind = _interopRequireDefault(require("@enact/core/kind")); var _propTypes2 = _interopRequireDefault(require("prop-types")); var _Skinnable = _interopRequireDefault(require("../Skinnable")); var _TooltipLabel = _interopRequireDefault(require("./TooltipLabel")); var _TooltipModule = _interopRequireDefault(require("./Tooltip.module.css")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["arrowAnchor", "children", "css", "tooltipRef", "width", "labelOffset", "marquee"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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 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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(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); } // Set the default Arrow Anchor value based on the type of tooltip function defaultArrowAnchor(type) { return type === 'transparent' ? 'center' : 'right'; } // Set the default Direction of tooltip based on the type of tooltip function defaultDirection(type) { return type === 'transparent' ? 'below' : 'above'; } /** * A stateless tooltip component with Sandstone styling applied. * * @class TooltipBase * @memberof sandstone/TooltipDecorator * @ui * @public */ var TooltipBase = exports.TooltipBase = (0, _kind["default"])({ name: 'Tooltip', propTypes: /** @lends sandstone/TooltipDecorator.TooltipBase.prototype */{ /** * The node to be displayed as the main content of the tooltip. * * @type {Node} * @required */ children: _propTypes2["default"].node.isRequired, /** * Position of tooltip arrow in relation to the activator. * * Note that `'left'`, `'center'`, `'right'` are applicable when direction is in vertical * orientation (i.e. `'above'`, `'below'`), and `'top'`, `'middle'`, and `'bottom'` are * applicable when direction is in horizontal orientation (i.e. `'left'`, `'right'`) * * For `type="balloon"`, the default is `"right"` * For `type="transparent"`, the default is `"center"` (The arrow will not be visible) * * @type {('left'|'center'|'right'|'top'|'middle'|'bottom')} * @public */ arrowAnchor: _propTypes2["default"].oneOf(['left', 'center', 'right', 'top', 'middle', 'bottom']), /** * Customizes the component by mapping the supplied collection of CSS class names to the * corresponding internal elements and states of this component. * * The following classes are supported: * * * `tooltip` - The root component class * * `tooltipLabel` - Applied the label node * * @type {Object} * @public */ css: _propTypes2["default"].object, /** * Direction of label in relation to the activator. * * For `type="balloon"`, the default is `"above"` * For `type="transparent"`, the default is `"below"` * * @type {('above'|'below'|'left'|'right')} * @public */ direction: _propTypes2["default"].oneOf(['above', 'below', 'left', 'right']), /** * A value representing the amount to offset the label portion of the tooltip. * * In a "center" aligned tooltip, the label may be desirable to offset to one side or the * other. This prop accepts a value between -0.5 and 0.5 (representing 50% to the left or * right). This defaults to 0 offset (centered). It also automatically caps the value so it * never positions the tooltip label past the anchored arrow. If the tooltip label or arrow * has non-rectangular geometry (rounded corners, a wide tail, etc), you'll need to manually * account for that in your provided offset value. * * @type {Number} * @default 0 * @public */ labelOffset: _propTypes2["default"].number, /** * Allows the tooltip to marquee. * * Specifying a {@link sandstone/TooltipDecorator.TooltipBase.width|width} restrects * the marquee to that size. * * @type {Boolean} * @public */ marquee: _propTypes2["default"].bool, /** * Style object for tooltip position. * * @type {Object} * @public */ position: _propTypes2["default"].shape({ bottom: _propTypes2["default"].number, left: _propTypes2["default"].number, right: _propTypes2["default"].number, top: _propTypes2["default"].number }), /** * Anchors the tooltip relative to its container. * * Reconfigures the component to anchor itself to the designated edge of its container. * When this is not specified, the implication is that the component is "absolutely" * positioned, relative to the viewport, rather than its parent layer. * * @type {Boolean} * @public */ relative: _propTypes2["default"].bool, /** * Called when the tooltip mounts/unmounts, giving a reference to the DOM. * * @type {Object|Function} * @public */ tooltipRef: _propTypes["default"].ref, /** * Type of tooltip. * * | *Value* | *Tooltip Appearance* | * |---|---| * | `'balloon'` | Tooltip with a border, background and arrow to the activator | * | `'transparent'` | Text only without any of the decorations above | * * @type {('balloon'|'transparent')} * @default 'balloon' * @public */ type: _propTypes2["default"].oneOf(['balloon', 'transparent']), /** * The width of tooltip content. * * Value expects a number of pixels, which will be automatically scaled to the appropriate * size given the current screen resolution, or a string value containing a measurement and * a valid CSS unit included. * If the content goes over the given width, it will automatically wrap, or marquee if * `marquee` is enabled. * * When `null`, content will auto-size and not wrap. If `marquee` is also enabled, * marqueeing will begin when the width is greater than the default (theme specified) width. * * @type {Number|String} * @public */ width: _propTypes2["default"].oneOfType([_propTypes2["default"].number, _propTypes2["default"].string]) }, defaultProps: { type: 'balloon', labelOffset: 0 }, styles: { css: _TooltipModule["default"], className: 'tooltip', publicClassNames: ['tooltip', 'tooltipLabel'] }, computed: { labelOffset: function labelOffset(_ref) { var _labelOffset = _ref.labelOffset; if (_labelOffset) { var cappedPosition = Math.max(-0.5, Math.min(0.5, _labelOffset)); return { transform: "translateX(".concat(cappedPosition * 100, "%)") }; } }, className: function className(_ref2) { var direction = _ref2.direction, arrowAnchor = _ref2.arrowAnchor, relative = _ref2.relative, type = _ref2.type, styler = _ref2.styler; return styler.append(direction || defaultDirection(type), "".concat(arrowAnchor || defaultArrowAnchor(type), "Arrow"), { relative: relative, absolute: !relative }, type); }, style: function style(_ref3) { var position = _ref3.position, _style = _ref3.style; return _objectSpread(_objectSpread({}, _style), position); } }, render: function render(_ref4) { var arrowAnchor = _ref4.arrowAnchor, children = _ref4.children, css = _ref4.css, tooltipRef = _ref4.tooltipRef, width = _ref4.width, labelOffset = _ref4.labelOffset, marquee = _ref4.marquee, rest = _objectWithoutProperties(_ref4, _excluded); delete rest.labelOffset; delete rest.direction; delete rest.position; delete rest.relative; delete rest.type; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, { children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: css.tooltipAnchor, ref: tooltipRef, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: css.tooltipArrow }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_TooltipLabel["default"], { className: css.tooltipLabel, marquee: marquee, centered: arrowAnchor === 'center', width: width, style: labelOffset, children: children })] }) })); } }); /** * A tooltip component with Sandstone styling applied. * * @class Tooltip * @memberof sandstone/TooltipDecorator * @ui * @public */ var Tooltip = exports.Tooltip = (0, _Skinnable["default"])(TooltipBase); var _default = exports["default"] = Tooltip;