index.tsx 6.37 KB
import { useId } from 'react';
import type { CSSProperties } from 'react';

/**
 * 列表加载指示器:把 BrandLoader 的「门店覆盖网络」语言平移到数据行场景。
 *
 * 与 packages/biz 的 BrandLoader(全屏接入占位)同源,不是另起一套图形:
 *   - 表头右上角一枚缩微双环 + 青绿脉冲核,就是全屏版基站双环的等比缩写;
 *   - 全屏版绕外环的扫描弧,在这里变成自上而下逐行扫过的数据卡光带(同为 2.2s);
 *   - 3 行左侧的青绿节点沿用 uj-brand-node 的 1.8s 错峰呼吸(0 / 0.3 / 0.6s),
 *     行占位条另用 2.2s 提亮,两个周期互质,避免整枚图机械同步摆动。
 *
 * 由 UTable / FillTable 的 resolveTableLoading 统一注入 antd Spin 的 indicator,
 * 页面只传 boolean,不需要知道这个组件存在。动效与色值全部由 ui.css 下发。
 * 组件无状态、无随机数、无定时器,纯 SVG + CSS,尊重 prefers-reduced-motion。
 */
export interface TableLoaderProps {
  /** 图形宽度,高度按 120:76 等比换算,默认 96 */
  size?: number;
  /** 图形下方文案,传空字符串隐藏 */
  label?: string;
  className?: string;
  /** antd Spin 的 Indicator 会 cloneElement 合并 style,这里保留透传口 */
  style?: CSSProperties;
}

const CARD = { x: 6, y: 4, w: 108, h: 60, r: 8 };
/** 3 行占位条:[x, width],刻意长短不一,读成真实数据而不是进度条。 */
const ROWS = [
  {
    y: 32,
    bars: [
      [26, 30],
      [62, 22],
    ],
  },
  {
    y: 45,
    bars: [
      [26, 40],
      [72, 14],
    ],
  },
  {
    y: 58,
    bars: [
      [26, 24],
      [56, 28],
    ],
  },
];

export function TableLoader({
  size = 96,
  label = '正在加载数据…',
  className,
  style,
}: TableLoaderProps) {
  // 同一页可能有多张表格同时 loading,渐变/裁剪的 id 必须每个实例唯一。
  const uid = useId().replace(/:/g, '');
  const clipId = `uj-tl-clip-${uid}`;
  const gradientId = `uj-tl-scan-${uid}`;

  return (
    <div
      // antd 6 给自定义 indicator 追加 .ant-spin-dot(1em 方形),内联尺寸盖掉它,
      // 同时把 font-size 收回 12px,避免 antd 的 dot-holder-size 把文案撑大。
      className={`uj-table-loader${className ? ` ${className}` : ''}`}
      style={{ width: size, height: 'auto', fontSize: 12, ...style }}
      role="status"
      aria-label={label || '正在加载'}
    >
      <svg
        viewBox="0 0 120 76"
        width={size}
        height={(size * 76) / 120}
        aria-hidden="true"
        focusable="false"
      >
        <defs>
          <clipPath id={clipId}>
            <rect x={CARD.x} y={CARD.y} width={CARD.w} height={CARD.h} rx={CARD.r} />
          </clipPath>
          {/* stop-color 走内联 style:presentation attribute 上的 var() 在部分浏览器不解析 */}
          <linearGradient id={gradientId} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" style={{ stopColor: 'var(--uj-primary)' }} stopOpacity="0" />
            <stop offset="55%" style={{ stopColor: 'var(--uj-primary)' }} stopOpacity="0.22" />
            <stop offset="100%" style={{ stopColor: 'var(--uj-primary)' }} stopOpacity="0" />
          </linearGradient>
        </defs>

        {/* 数据卡:不透明底让它从 antd 的半透明模糊遮罩里浮出来 */}
        <rect
          x={CARD.x}
          y={CARD.y}
          width={CARD.w}
          height={CARD.h}
          rx={CARD.r}
          fill="var(--uj-surface)"
          stroke="var(--uj-border)"
        />
        {/* 表头:只圆上方两角,与卡片共用描边 */}
        <path
          d={`M${CARD.x + CARD.r} ${CARD.y} H${CARD.x + CARD.w - CARD.r} A${CARD.r} ${CARD.r} 0 0 1 ${CARD.x + CARD.w} ${CARD.y + CARD.r} V22 H${CARD.x} V${CARD.y + CARD.r} A${CARD.r} ${CARD.r} 0 0 1 ${CARD.x + CARD.r} ${CARD.y} Z`}
          fill="var(--uj-surface-3)"
        />
        <line x1={CARD.x} y1="22" x2={CARD.x + CARD.w} y2="22" stroke="var(--uj-border)" />
        {[
          [14, 16],
          [46, 14],
          [78, 12],
        ].map(([x, w]) => (
          <rect
            key={x}
            x={x}
            y="12"
            width={w}
            height="3"
            rx="1.5"
            fill="var(--uj-text-4)"
            fillOpacity="0.65"
          />
        ))}

        {/* 缩微基站双环:全屏版 r=26 / r=14 的外环与唯一青绿核 */}
        <circle
          cx="104"
          cy="13"
          r="4.5"
          fill="none"
          stroke="var(--uj-brand-light)"
          strokeWidth="0.9"
          strokeOpacity="0.55"
        />
        <circle
          className="uj-table-loader__ping"
          cx="104"
          cy="13"
          r="1.8"
          fill="none"
          stroke="var(--uj-brand-pulse)"
          strokeWidth="1"
          vectorEffect="non-scaling-stroke"
        />
        <circle cx="104" cy="13" r="1.8" fill="var(--uj-brand-pulse)" />

        {/* 节点与占位条分开动画:节点 1.8s 呼吸,行条 2.2s 提亮,节奏不同步 */}
        {ROWS.map((row, i) => (
          <g key={row.y}>
            <circle
              className={`uj-table-loader__node uj-table-loader__node--${i + 1}`}
              cx="16"
              cy={row.y - 1.5}
              r="2.2"
              fill="var(--uj-brand-pulse)"
            />
            <g className={`uj-table-loader__row uj-table-loader__row--${i + 1}`}>
              {row.bars.map(([x, w]) => (
                <rect
                  key={x}
                  x={x}
                  y={row.y - 2.5}
                  width={w}
                  height="4"
                  rx="2"
                  fill="var(--uj-border-strong)"
                  fillOpacity="0.72"
                />
              ))}
            </g>
          </g>
        ))}

        {/* 扫描光带:裁剪到卡片内,从表头上方进入、自底部离开 */}
        <g clipPath={`url(#${clipId})`}>
          <g className="uj-table-loader__scan">
            <rect x={CARD.x} y="-12" width={CARD.w} height="16" fill={`url(#${gradientId})`} />
            <rect
              x={CARD.x}
              y="2.6"
              width={CARD.w}
              height="1.4"
              fill="var(--uj-primary)"
              fillOpacity="0.5"
            />
          </g>
        </g>
      </svg>
      {label ? <span className="uj-table-loader__label">{label}</span> : null}
    </div>
  );
}