index.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import clsx from "clsx";
  2. import React from "react";
  3. import styles from "./index.module.scss";
  4. interface LoadingProps {
  5. className?: string; // 可选的 className 属性,用于传递额外的样式类名
  6. color?: string;
  7. width?: number;
  8. space?: number;
  9. point?: number;
  10. animationDuration?: number;
  11. }
  12. const Loading: React.FC<LoadingProps> = ({
  13. className,
  14. color,
  15. width = 5,
  16. point = 4,
  17. space = 2,
  18. animationDuration = 800,
  19. }) => {
  20. const Point = ({ idx }: { idx: number }) => {
  21. return (
  22. <span
  23. style={{
  24. backgroundColor: color,
  25. width: `${width}px`,
  26. height: `${width}px`,
  27. animationDelay: `${pointTime * idx}ms`,
  28. animationDuration: `${animationDuration}ms`,
  29. margin: `0 ${space}px`,
  30. }}
  31. ></span>
  32. );
  33. };
  34. const pointTime = React.useMemo(() => {
  35. return animationDuration / 2 / point;
  36. }, [point, animationDuration]);
  37. const PointList = React.useCallback(() => {
  38. return Array.from({ length: point }, (_, index) => <Point key={index} idx={index}></Point>);
  39. }, [point]);
  40. return (
  41. <div className={clsx(styles.loading, className)}>
  42. <PointList></PointList>
  43. </div>
  44. );
  45. };
  46. export default Loading;