UNPKG

@nutui/nutui-react

Version:

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

92 lines (91 loc) 3.6 kB
import { _ as _object_spread } from "@swc/helpers/_/_object_spread"; import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props"; import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties"; import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array"; import React, { useEffect, useState } from "react"; import classNames from "classnames"; import { ComponentDefaults } from "../../utils/typings"; var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), { rows: 1, animated: true, visible: false, size: 'normal', shape: 'round', duration: 0.6, inline: false }); export var Skeleton = function(props) { var shapeStyle = function shapeStyle() { if (shape === 'circle') return { borderRadius: '50%' }; if (shape === 'square') return { borderRadius: '0' }; return {}; }; var durationStyle = function durationStyle() { if (typeof duration !== 'undefined') return { animationDuration: "".concat(duration, "s") }; return {}; }; var _ref = _object_spread({}, defaultProps, props), className = _ref.className, width = _ref.width, height = _ref.height, shape = _ref.shape, animated = _ref.animated, rows = _ref.rows, visible = _ref.visible, size = _ref.size, duration = _ref.duration, children = _ref.children, rest = _object_without_properties(_ref, [ "className", "width", "height", "shape", "animated", "rows", "visible", "size", "duration", "children" ]); var classPrefix = 'nut-skeleton'; var classes = classNames(classPrefix, className); var repeatCount = function(num) { return Array.from({ length: num }, function(v, i) { return i; }); }; var _useState = _sliced_to_array(useState(false), 2), animate = _useState[0], setAnimate = _useState[1]; var playAnimation = function() { setAnimate(false) // 首先将 animate 设置为 false ; setTimeout(function() { setAnimate(true) // 1 毫秒后再设置为 true,触发动画 ; }, 10); }; useEffect(function() { if (!animated) return; playAnimation(); // 每隔 3 秒播放一次动画 var intervalId = setInterval(playAnimation, 1000 + duration * 1000) // xs 动画 + 1s 间隔 ; // 清理定时器 return function() { return clearInterval(intervalId); }; }, []); return /*#__PURE__*/ React.createElement(React.Fragment, null, visible ? children : /*#__PURE__*/ React.createElement("div", _object_spread({ className: classes }, rest), repeatCount(rows).map(function(item, index) { var contentClass = "".concat(classPrefix, "-content ").concat(classPrefix, "-content-").concat(size, " ").concat(classPrefix, "-content-").concat(size, "-").concat(index); return /*#__PURE__*/ React.createElement("div", { className: "".concat(contentClass), key: index, style: _object_spread({ width: width, height: height }, shapeStyle()) }, animated && /*#__PURE__*/ React.createElement("div", { className: "".concat(classPrefix, "-animated ").concat(animate ? "".concat(classPrefix, "-animation") : ''), style: durationStyle() })); }))); }; Skeleton.displayName = 'NutSkeleton';