UNPKG

@clxx/rolling-notice

Version:

Scroll to notice

55 lines (54 loc) 2.25 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx } from "@emotion/core"; import { style } from "./style"; import { useState } from "react"; import { useInterval } from "@clxx/effect"; export function RollingNotice(props) { let { list = [], height = 32, fontSize = 16, duration = 3000, bubbleDuration = 300 } = props, defaultProps = __rest(props, ["list", "height", "fontSize", "duration", "bubbleDuration"]); if (duration < bubbleDuration + 100) { duration = bubbleDuration + 100; } const styles = style(height, fontSize, bubbleDuration); const [current, setCurrent] = useState(0); const [scroll, setScroll] = useState(undefined); useInterval(() => { if (list.length > 1) { setScroll(styles.withScroll); } }, duration); const showList = []; if (list.length === 1) { showList.push(list[0]); } else if (list.length > 1) { showList.push(list[current]); if (current === list.length - 1) { showList.push(list[0]); } else { showList.push(list[current + 1]); } } const transitionEnd = () => { setCurrent(current >= list.length - 1 ? 0 : current + 1); setScroll(undefined); }; return (jsx("div", Object.assign({}, defaultProps, { css: styles.container }), jsx("ul", { css: [styles.ul, scroll], onTransitionEnd: transitionEnd }, showList.map((item, index) => { let content = item; if (typeof content === "string") { content = jsx("p", { css: styles.textItem }, content); } return (jsx("li", { css: styles.li, key: index, className: "cl-RollingNotice-item" }, content)); })))); }