这个模块解决什么问题?
空白等待和旋转图标无法说明页面将出现什么,也容易在内容载入后造成明显跳动。
Skeleton Loader一个支持结构占位、加载状态语义和减少动画偏好的内容骨架。
"use client";
import type { ReactNode } from "react";
export type SkeletonLoaderProps = {
children: ReactNode;
label?: string;
loading: boolean;
rows?: number;
showAvatar?: boolean;
};
const WIDTH_PATTERN = [92, 76, 84, 58, 70];
export function buildSkeletonWidths(rows: number) {
const safeRows = Math.max(1, Math.min(8, Math.floor(rows)));
return Array.from(
{ length: safeRows },
(_, index) => WIDTH_PATTERN[index % WIDTH_PATTERN.length],
);
}
export function SkeletonLoader({
children,
label = "内容正在加载",
loading,
rows = 3,
showAvatar = true,
}: SkeletonLoaderProps) {
return (
<section
aria-busy={loading}
aria-label={label}
className="skeleton-loader-module"
>
{loading ? (
<>
<span className="sr-only" role="status">
{label}
</span>
<div aria-hidden="true" className="skeleton-loader-card">
<div className="skeleton-loader-head">
{showAvatar ? <span className="skeleton-loader-avatar" /> : null}
<span className="skeleton-loader-heading-lines">
<span style={{ width: "44%" }} />
<span style={{ width: "28%" }} />
</span>
</div>
<div className="skeleton-loader-lines">
{buildSkeletonWidths(rows).map((width, index) => (
<span
key={`${width}-${index}`}
style={{ width: `${width}%` }}
/>
))}
</div>
<div className="skeleton-loader-actions">
<span />
<span />
</div>
</div>
</>
) : (
<div className="skeleton-loader-content">{children}</div>
)}
</section>
);
}
空白等待和旋转图标无法说明页面将出现什么,也容易在内容载入后造成明显跳动。
操作上方真实 Demo,确认状态和交互符合产品需要。
只有真实存在的源码、Prompt 和参数文件才会出现在代码面板。
保留许可证,并根据自己的设计系统调整参数和样式。