跳转到内容
Nacre
组件配色
EN

骨架 Skeleton

stableSkeleton在画廊运行
Skeleton
在浏览器里运行真实组件。

首屏加载的占位块(muted 底、脉动,减少动效时不脉动):尺寸和圆角由 class 给,按数据算的宽度用 style 给,拼成和真实内容一样的形状,数据到了不跳。加载很快结束时不该闪一下骨架:delayed 让它挂上后先透明 SKELETON_DELAY(300ms)。

状态:stable · 组件:Skeleton

import { Skeleton } from 'nacre'

一个 Skeleton 是一块:用 h-* / w-* 给出和真实文字行一样的高宽。

import { View } from 'nsh'
import { Skeleton } from 'nacre'
export function SkeletonUsage() {
return (
<View class="w-[320px] max-w-full gap-2">
<Skeleton testID="demo-skeleton-line" class="h-2.5 w-full" />
<Skeleton class="h-2.5 w-5/6" />
<Skeleton class="h-2.5 w-2/3" />
</View>
)
}

头像用 rounded-full,标题与副行一长一短;行高与真实 ListRow 相同。

import { View } from 'nsh'
import { Skeleton, TOUCH } from 'nacre'
export function SkeletonListRow() {
return (
<View class={`${TOUCH ? 'w-full' : 'w-[280px] max-w-full rounded-xl border border-border'} p-1.5 gap-0.5`}>
{[0, 1, 2].map(() => (
<View class="h-[52px] px-2.5 flex-row items-center gap-2.5">
<Skeleton class="w-[22px] h-[22px] rounded-full" />
<View class="flex-1 gap-2">
<Skeleton class="h-3 w-1/2" />
<Skeleton class="h-2.5 w-3/4" />
</View>
</View>
))}
</View>
)
}

指标卡的标题、数值、环比各占一块,卡片外框照常画。

import { View } from 'nsh'
import { Card, Skeleton, TOUCH } from 'nacre'
export function SkeletonCard() {
return (
<View classList={{ 'flex-row gap-[21px] p-[21px] rounded-xl bg-plane': !TOUCH, 'gap-3': TOUCH }}>
{[0, 1, 2].map(() => (
<View class="flex-1">
<Card variant="tile" size="sm">
<Skeleton class="h-3 w-16" />
<Skeleton class="h-6 w-24" />
<Skeleton class="h-2.5 w-20" />
</Card>
</View>
))}
</View>
)
}

首次加载出骨架,数据到了换成真实内容;占位和内容同高,切换不跳。

import { Text, View, signal } from 'nsh'
import { Avatar, Button, Skeleton, TEXT_SIZE } from 'nacre'
export function SkeletonLoading() {
const loading = signal(true)
return (
<View class="gap-3">
<View class="w-[280px] max-w-full h-[52px] px-2.5 flex-row items-center gap-2.5 rounded-xl border border-border">
<If
when={!loading()}
else={
<>
<Skeleton class="w-[22px] h-[22px] rounded-full" />
<View class="flex-1 gap-2">
<Skeleton class="h-3 w-1/2" />
<Skeleton class="h-2.5 w-3/4" />
</View>
</>
}
>
<Avatar name="林嘉怡" size={22} />
<View class="flex-1 gap-0.5">
<Text class={`${TEXT_SIZE.body} text-foreground`}>林嘉怡</Text>
<Text class={`${TEXT_SIZE.caption} text-muted-foreground`}>港股开户 · 一线</Text>
</View>
</If>
</View>
<View class="flex-row">
<Button size="sm" variant="outline" onPress={() => loading.update((v) => !v)}>
{loading() ? '模拟数据到达' : '重新加载'}
</Button>
</View>
</View>
)
}
  • 骨架块不进读屏树;加载中的区域另给读屏可读的状态(例如列表的「加载中」)。

该用

  • 行高、列宽与真实内容相同
  • 只在首屏(没有旧数据时)用骨架
  • 一屏的骨架行数和真实内容差不多,只画可视区以内的
  • 加载态过 SKELETON_DELAY 才出骨架(delayed,或自己的状态用 delayed(when))
  • 一组灰条由外层容器统一脉动,灰条传 pulse={false}

不该用

  • 翻页、刷新时整块换成骨架(保留旧数据,给 loading)
  • 骨架块用品牌色或自定义动画
  • 加载超过几秒还只有骨架(给错误或重试)
属性 类型 默认 说明
class string —
style StyleProp<ViewStyle> — 直接给尺寸、位置(按数据算出来的宽度),省掉外面再套一层定宽 View
testID string —
pulse boolean — 自己脉动(默认);一组灰条由外层容器统一脉动时传 false,一屏只跑一个动画
delayed boolean — 挂上后先透明 SKELETON_DELAY,加载很快结束时不闪

* 必填