@vnmfify/core
Version:
```shell npm i @vnmfify/core -S ```
153 lines (131 loc) • 6.64 kB
JavaScript
var _excluded = ["className", "delay", "speed", "wordwrap", "scrollable", "children"];
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 _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; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
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; }
import { View } from "@vnxjs/components";
import { nextTick } from "@vnxjs/vnmf";
import classNames from "classnames";
import * as React from "react";
import { Children, isValidElement, useEffect, useMemo, useRef, useState } from "react";
import { prefixClassname } from "../styles";
import { getRect } from "../utils/dom/rect";
import { addUnitPx } from "../utils/format/unit";
import { doubleRaf } from "../utils/raf";
import { NoticeBarAction } from "./notice-bar-action";
import { NoticeBarIcon } from "./notice-bar-icon";
function useChildren(children) {
var __children__ = {
icon: undefined,
text: [],
action: undefined
};
Children.forEach(children, child => {
if ( /*#__PURE__*/isValidElement(child)) {
var element = child;
var elementType = element.type;
if (elementType === NoticeBarIcon) {
__children__.icon = element;
} else if (elementType === NoticeBarAction) {
__children__.action = element;
} else {
__children__.text.push(child);
}
} else {
__children__.text.push(child);
}
});
return __children__;
}
function NoticeBar(props) {
var {
className,
delay = 1000,
speed = 60,
wordwrap,
scrollable = false,
children: childrenProp
} = props,
restProps = _objectWithoutProperties(props, _excluded);
var {
icon,
text,
action
} = useChildren(childrenProp);
var ellipsis = !scrollable && !wordwrap;
var startTimerRef = useRef();
var wrapRef = useRef();
var contentRef = useRef();
var [offset, setOffset] = useState(0);
var [duration, setDuration] = useState(0);
var wrapWidthRef = useRef(0);
var contentWidthRef = useRef(0);
var contentStyle = useMemo(() => ({
transform: offset ? "translateX(".concat(addUnitPx(offset), ")") : "",
transitionDuration: "".concat(duration, "s")
}), [offset, duration]);
function reset() {
wrapWidthRef.current = 0;
contentWidthRef.current = 0;
setOffset(0);
setDuration(0);
}
function onTransitionEnd() {
setOffset(wrapWidthRef.current);
setDuration(0);
nextTick(() => {
doubleRaf(() => {
setOffset(-contentWidthRef.current);
setDuration((contentWidthRef.current + wrapWidthRef.current) / +speed);
});
});
}
function start() {
reset();
if (startTimerRef.current) {
clearTimeout(startTimerRef.current);
}
startTimerRef.current = setTimeout( /*#__PURE__*/_asyncToGenerator(function* () {
if (!wrapRef.current || !contentRef.current || !scrollable) {
return;
}
nextTick(() => Promise.all([getRect(wrapRef), getRect(contentRef)]).then(_ref2 => {
var [{
width: wrapRefWidth
}, {
width: contentRefWidth
}] = _ref2;
if (scrollable || contentRefWidth > wrapRefWidth) {
doubleRaf(() => {
wrapWidthRef.current = wrapRefWidth;
contentWidthRef.current = contentRefWidth;
setOffset(-contentRefWidth);
setDuration(contentRefWidth / +speed);
});
}
}));
}), +delay);
}
useEffect(start, []);
return /*#__PURE__*/React.createElement(View, _objectSpread({
className: classNames(prefixClassname("notice-bar"), {
[prefixClassname("notice-bar--wordwrap")]: wordwrap
}, className)
}, restProps), icon, /*#__PURE__*/React.createElement(View, {
ref: wrapRef,
className: prefixClassname("notice-bar__wrap")
}, /*#__PURE__*/React.createElement(View, {
ref: contentRef,
style: contentStyle,
className: classNames(prefixClassname("notice-bar__content"), {
[prefixClassname("ellipsis")]: ellipsis
}),
children: text,
onTransitionEnd: onTransitionEnd
})), action);
}
export default NoticeBar;
//# sourceMappingURL=notice-bar.js.map