UNPKG

@nutui/nutui-react

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

125 lines (124 loc) 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Barrage", { enumerable: true, get: function() { return Barrage; } }); var _interop_require_default = require("@swc/helpers/_/_interop_require_default"); var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard"); var _object_spread = require("@swc/helpers/_/_object_spread"); var _object_spread_props = require("@swc/helpers/_/_object_spread_props"); var _object_without_properties = require("@swc/helpers/_/_object_without_properties"); var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react")); var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames")); var _typings = require("../../utils/typings"); var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), { list: [], interval: 500, loop: true, duration: 3000, rows: 3, gapY: 10 }); var classPrefix = "nut-barrage"; var InternalBarrage = function InternalBarrage(props, ref) { var _ref = (0, _object_spread._)({}, defaultProps, props), className = _ref.className, interval = _ref.interval, loop = _ref.loop, list = _ref.list, duration = _ref.duration, rows = _ref.rows, gapY = _ref.gapY, restProps = (0, _object_without_properties._)(_ref, [ "className", "interval", "loop", "list", "duration", "rows", "gapY" ]); var barrageBody = (0, _react.useRef)(null); var barrageContainer = (0, _react.useRef)(null); var barrageCWidth = (0, _react.useRef)(0); var timer = (0, _react.useRef)(0); var index = (0, _react.useRef)(0); var times = (0, _react.useRef)([]); var historyIndex = (0, _react.useRef)(-1); var classes = (0, _classnames.default)(classPrefix, className); (0, _react.useImperativeHandle)(ref, function() { return { add: function add(word) { var _index = index.current % list.length; if (!loop && index.current === list.length) { list.splice(list.length, 0, word); } else { list.splice(_index, 0, word); } } }; }); (0, _react.useEffect)(function() { if (barrageBody.current) { barrageCWidth.current = barrageBody.current.offsetWidth; } setTimeout(function() { var _barrageBody_current; (_barrageBody_current = barrageBody.current) === null || _barrageBody_current === void 0 ? void 0 : _barrageBody_current.style.setProperty('--move-distance', "-".concat(barrageCWidth.current, "px")); index.current = 0; run(); }, 300); return function() { clearInterval(timer.current); }; }, [ list ]); var run = function run() { clearInterval(timer.current); var intervalCache = interval; var _index = (loop ? index.current % list.length : index.current) % rows; var result = times.current[_index] - rows * interval; if (result > 0) { intervalCache += result; } timer.current = window.setTimeout(function() { play(); }, intervalCache); }; var play = function play() { if (!loop && index.current >= list.length) { return; } var _index = loop ? index.current % list.length : index.current; var el = document.createElement("div"); var currentIndex = _index % rows; if (currentIndex <= historyIndex.current || historyIndex.current === 3 && currentIndex !== 0 || Math.abs(currentIndex - historyIndex.current) !== 1) { currentIndex = historyIndex.current + 1 >= rows ? 0 : historyIndex.current + 1; } historyIndex.current = currentIndex; el.innerHTML = list[_index]; el.classList.add('barrage-item'); barrageContainer.current.appendChild(el); var width = el.offsetWidth; var height = el.offsetHeight; el.classList.add('move'); var elScrollDuration = Math.ceil(width / barrageCWidth.current * duration); times.current[currentIndex] = elScrollDuration; el.style.animationDuration = "".concat(duration + elScrollDuration, "ms"); el.style.top = "".concat(currentIndex * (height + gapY) + 20, "px"); el.style.width = "".concat(width, "px"); el.addEventListener('animationend', function() { ; barrageContainer.current.removeChild(el); }); index.current++; run(); }; return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({ className: classes, ref: barrageBody }, restProps), /*#__PURE__*/ _react.default.createElement("div", { ref: barrageContainer, className: "bContainer" })); }; var Barrage = /*#__PURE__*/ _react.default.forwardRef(InternalBarrage); Barrage.displayName = 'NutBarrage';