UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

288 lines (286 loc) 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.MarqueeBase = void 0; var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types")); var _handle = require("@enact/core/handle"); var _kind = _interopRequireDefault(require("@enact/core/kind")); var _propTypes2 = _interopRequireDefault(require("prop-types")); var _react = require("react"); var _MarqueeModule = _interopRequireDefault(require("./Marquee.module.css")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["applyOffset", "children", "clientClassName", "clientRef", "clientStyle", "css", "duplicate", "onMarqueeComplete", "rtl"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } 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(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); } 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], -1 === t.indexOf(o) && {}.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 (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } var isEventSource = function isEventSource(ev) { return ev.target === ev.currentTarget; }; /** * Marquees the children of the component. * * For automated marquee calculations use {@link ui/Marquee.Marquee}. * * @class MarqueeBase * @memberof ui/Marquee * @ui * @public */ var MarqueeBase = exports.MarqueeBase = (0, _kind["default"])({ name: 'ui:Marquee', propTypes: /** @lends ui/Marquee.MarqueeBase.prototype */{ /** * Text alignment value of the marquee * * Valid values are: * * * `'left'`, * * `'right'`, and * * `'center'` * * @type {String} * @public */ alignment: _propTypes2["default"].oneOf(['left', 'right', 'center']), /** * Applies animating styles such as removing the ellipsis. * * @type {Boolean} * @public */ animating: _propTypes2["default"].bool, /** * Sets the value of the `aria-label` attribute for the wrapped component. * * @memberof ui/Marquee.MarqueeBase.prototype * @type {String} * @public */ 'aria-label': _propTypes2["default"].string, /** * The text or a set of components that should be marqueed * * This prop may be empty in some cases, which is OK. * * @type {Node} * @public */ children: _propTypes2["default"].node, /** * Called when mounting or unmounting with a reference to the client node * * @type {Object|Function} * @public */ clientRef: _propTypes["default"].ref, /** * 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: * * * `marquee` - The root component class * * `animate` - Applied to the inner content node when the text is animating * * `spacing` - The spacing node used between the repeated content * * `text` - The inner content node * * `willAnimate` - Applied to the inner content node shortly before animation * * @type {Object} * @public */ css: _propTypes2["default"].object, /** * Distance to animate the marquee * * Usually, the `distance` is the width of the text minus the width of the container * * @type {Number} * @public */ distance: _propTypes2["default"].number, /** * Called when the marquee completes its animation * * @type {Function} * @public */ onMarqueeComplete: _propTypes2["default"].func, /** * Text overflow setting. Either `'clip'` or `'ellipsis'` * * @type {('clip'|'ellipsis')} * @public */ overflow: _propTypes2["default"].oneOf(['clip', 'ellipsis']), /** * `true` if the directionality of the content is right-to-left * * @type {Boolean} * @public * @default false */ rtl: _propTypes2["default"].bool, /** * Amount of spacing, in pixels, between the instances of the content * * @type {Number} * @default 0 * @public */ spacing: _propTypes2["default"].number, /** * Speed of marquee animation in pixels/second. * * @type {Number} * @public */ speed: _propTypes2["default"].number, /** * Indicates the marquee will animate soon. * * Should be used by the component to prepare itself for animation such as promoting * composite layers for improved performance. * * @type {Boolean} * @public * @default false */ willAnimate: _propTypes2["default"].bool }, defaultProps: { spacing: 0, rtl: false, willAnimate: false }, styles: { css: _MarqueeModule["default"], className: 'marquee', publicClassNames: true }, handlers: { applyOffset: function applyOffset(node, _ref) { var distance = _ref.distance, rtl = _ref.rtl, spacing = _ref.spacing; if (!node || !global.IntersectionObserver) return; var root = node.parentNode; new global.IntersectionObserver(function (entries, observer) { var _entries$0$boundingCl = entries[0].boundingClientRect, left = _entries$0$boundingCl.left, right = _entries$0$boundingCl.right; var _entries$0$rootBounds = entries[0].rootBounds, rootLeft = _entries$0$rootBounds.left, rootRight = _entries$0$rootBounds.right; var scale = root.getBoundingClientRect().width / root.offsetWidth || 1; var textWidth = (rtl ? rootRight - right : left - rootLeft) / scale; var offset = distance - (textWidth + spacing); node.style.setProperty('--ui-marquee-offset', offset); observer.disconnect(); }, { root: root }).observe(node); }, onMarqueeComplete: (0, _handle.handle)((0, _handle.forProp)('animating', true), isEventSource, _handle.stop, (0, _handle.forwardCustom)('onMarqueeComplete')) }, computed: { 'aria-label': function ariaLabel(_ref2) { var aria = _ref2['aria-label'], children = _ref2.children, distance = _ref2.distance, willAnimate = _ref2.willAnimate; if (children != null && aria == null && willAnimate && distance > 0) { return _react.Children.map(children, function (c) { return typeof c === 'string' && c; }).filter(Boolean).join(' ') || aria; } else { return aria; } }, clientClassName: function clientClassName(_ref3) { var animating = _ref3.animating, willAnimate = _ref3.willAnimate, styler = _ref3.styler; return styler.join({ animate: animating, text: true, willAnimate: willAnimate }); }, clientStyle: function clientStyle(_ref4) { var alignment = _ref4.alignment, animating = _ref4.animating, distance = _ref4.distance, overflow = _ref4.overflow, rtl = _ref4.rtl, spacing = _ref4.spacing, speed = _ref4.speed; // If the components content directionality doesn't match the context, we need to set it // inline var direction = rtl ? 'rtl' : 'ltr'; var rtlDirectionMultiplier = rtl ? 1 : -1; var style = { '--ui-marquee-spacing': spacing, direction: direction, textAlign: alignment, textOverflow: overflow }; if (animating) { var duration = distance / speed; style.transform = "translateX(".concat(distance * rtlDirectionMultiplier, "px)"); style.transitionDuration = "".concat(duration, "s"); } else { style.transform = 'translateX(0)'; } return style; }, duplicate: function duplicate(_ref5) { var distance = _ref5.distance, willAnimate = _ref5.willAnimate; return willAnimate && distance > 0; } }, render: function render(_ref6) { var applyOffset = _ref6.applyOffset, children = _ref6.children, clientClassName = _ref6.clientClassName, clientRef = _ref6.clientRef, clientStyle = _ref6.clientStyle, css = _ref6.css, duplicate = _ref6.duplicate, onMarqueeComplete = _ref6.onMarqueeComplete, rtl = _ref6.rtl, rest = _objectWithoutProperties(_ref6, _excluded); delete rest.alignment; delete rest.animating; delete rest.distance; delete rest.onMarqueeComplete; delete rest.overflow; delete rest.spacing; delete rest.speed; delete rest.willAnimate; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, { children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: clientClassName, ref: clientRef, style: clientStyle, onTransitionEnd: onMarqueeComplete, children: [children, duplicate ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: css.spacing, ref: applyOffset }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", { dir: rtl ? "rtl" : "ltr", children: children })] }) : null] }) })); } }); var _default = exports["default"] = MarqueeBase;