Skeleton骨架屏
在内容加载时保留文字、图标与列表的布局,支持同步扫光和延迟淡入。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add skeleton安装
在项目根目录运行,安装源码。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add skeleton使用
React
import { KalosLoading, KalosSkeleton } from "@/components/ui/kalos-skeleton";
type Contact = { id: string; name: string; email: string };
export default function ContactList({ contacts }: { contacts?: Contact[] }) { if (!contacts) return ( <KalosLoading label="正在加载联系人" className="flex w-full max-w-sm flex-col gap-4"> {[160, 128, 144].map((width, index) => ( <div key={index} className="flex items-center gap-3"> <KalosSkeleton width={32} height={32} radius="50%" /> <div className="min-w-0 flex-1"> <KalosSkeleton line={20} width={width} height={10} /> <KalosSkeleton line={16} width={112} height={8} /> </div> </div> ))} </KalosLoading> );
return <ul className="flex w-full max-w-sm flex-col gap-4"> {contacts.map(contact => <li key={contact.id} className="flex items-center gap-3"> <span className="flex size-8 shrink-0 items-center justify-center rounded-full bg-muted" aria-hidden="true">{contact.name.slice(0, 1)}</span> <div className="min-w-0 flex-1"> <p className="truncate leading-5">{contact.name}</p> <p className="truncate text-xs leading-4 text-muted-foreground">{contact.email}</p> </div> </li>)} </ul>;}API
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
KalosSkeleton.width | number | string | 100% | 占位形状宽度;数字按 px,字符串支持 CSS 长度,最大不超过父容器 |
KalosSkeleton.height | number | string | 12 | 占位形状自身的高度,数字按 px |
KalosSkeleton.radius | number | string | 6px | 圆角;传 50% 可展示圆形头像占位 |
KalosSkeleton.line | boolean | number | string | false | 将形状居中放入行高盒,保留真实文字的行高;true 使用 1lh,也可传入 20 等高度 |
KalosSkeleton.className / style / ref | ComponentPropsWithRef<"span"> | — | 原生 span 属性;开启 line 时作用于外层行高盒,形状对屏幕阅读器隐藏 |
KalosLoading.label | string | 正在加载 | 加载区域的可访问名称;使用 role=status 和 aria-busy,仅播报一次区域状态 |
KalosLoading.children / className / style / ref | ComponentPropsWithRef<"div"> | — | 使用真实内容的布局包裹占位形状;150ms 后淡入,数据就绪时替换整个加载区域 |
动画变量 | CSS custom properties | 1600ms / 150ms / 300ms | 通过 --kalos-skeleton-duration、--kalos-skeleton-delay 和 --kalos-skeleton-fade-duration 调整扫光、等待与淡入时长 |