# 骨架屏

## 使用方法

`Skeleton` 在内容尚未加载完成时渲染占位块，按最终内容的尺寸与形状撑起布局，减轻加载完成时的跳动。它适合内容尺寸已知的场景；尺寸未知时建议改用 `Spinner`。

在应用入口引入主题：

```ts
import '@astryx-vue/themes/neutral.css'
```

组件从独立入口导入，结构样式会随组件自动加载：

```vue
<script setup lang="ts">
import { Skeleton } from '@astryx-vue/core/Skeleton'
</script>

<template>
  <div aria-busy="true" aria-label="正在加载内容" role="status">
    <Skeleton :height="20" :width="200" />
  </div>
</template>
```

### 结构

- **占位**: 内容还在加载时，替它站位的着色形状。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 让占位块的尺寸和形状贴近最终内容，加载完成后布局才不会发生明显跳动。 |
| 推荐 | 为同一区域内的多个骨架屏依次设置 `index`，脉冲动画会按顺序错开，形成自然的波浪效果。 |
| 避免 | 在内容尺寸未知时使用骨架屏，占位块无法模拟未知布局，这种情况改用 `Spinner` 更合适。 |
| 避免 | 在同一内容区域混用 `Spinner` 和骨架屏，两种加载模式并存会让界面显得不一致，只选其中一种即可。 |
| 避免 | 让骨架屏长时间停留，加载明显偏慢时应改显错误或空状态，让用户知道当前发生了什么。 |

## 示例

### 卡片加载

一张卡片的占位：圆形头像、名字、一行说明，下面再接几行正文。index 让脉冲依次错开。

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { Skeleton } from '@astryx-vue/core/Skeleton'
import { HStack, VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <Card :width="320">
    <VStack :gap="3">
      <HStack align="center" :gap="3">
        <Skeleton :height="40" :index="0" radius="rounded" :width="40" />
        <VStack :gap="1">
          <Skeleton :height="14" :index="1" :width="120" />
          <Skeleton :height="12" :index="2" :width="80" />
        </VStack>
      </HStack>
      <Skeleton :height="14" :index="3" width="100%" />
      <Skeleton :height="14" :index="4" width="90%" />
      <Skeleton :height="14" :index="5" width="75%" />
    </VStack>
  </Card>
</template>
```

### 错开的列表

几条宽度不一的横条，用来占住一份列表还没回来时的位置。

```vue
<script setup lang="ts">
import { Skeleton } from '@astryx-vue/core/Skeleton'
import { VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <VStack :gap="2">
    <Skeleton :height="16" :index="0" :width="300" />
    <Skeleton :height="16" :index="1" :width="280" />
    <Skeleton :height="16" :index="2" :width="320" />
    <Skeleton :height="16" :index="3" :width="260" />
    <Skeleton :height="16" :index="4" :width="290" />
  </VStack>
</template>
```

### 表格行

每一行四列，列宽按内容长短错开，整表的脉冲也按 index 往后推。

```vue
<script setup lang="ts">
import { Skeleton } from '@astryx-vue/core/Skeleton'
import { HStack, VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <VStack :gap="2">
    <HStack v-for="row in 4" :key="row" align="center" :gap="4">
      <Skeleton :height="16" :index="(row - 1) * 4" :width="50" />
      <Skeleton :height="16" :index="(row - 1) * 4 + 1" :width="180" />
      <Skeleton :height="16" :index="(row - 1) * 4 + 2" :width="100" />
      <Skeleton :height="16" :index="(row - 1) * 4 + 3" :width="80" />
    </HStack>
  </VStack>
</template>
```

## Props

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `width` | `number \| string` | `'100%'` | 占位块的宽度。传入数字时按像素处理，字符串则保留为 CSS 尺寸。 |
| `height` | `number \| string` | `'100%'` | 占位块的高度。传入数字时按像素处理，字符串则保留为 CSS 尺寸。 |
| `radius` | `'none' \| 0 \| 1 \| 2 \| 3 \| 4 \| 'rounded'` | `3` | 圆角使用设计令牌等级，`none` 为直角，`rounded` 为完全圆角。 |
| `index` | `number` | `0` | 交错动画的索引，每增加 1，动画延后 100ms，让多个占位块依次闪烁。 |

## 无障碍

- `Skeleton` 默认设置 `aria-hidden="true"`。
- 辅助技术不会读取默认的占位块。
- 外围加载区域必须提供名称。
- 外围加载区域必须设置 `aria-busy="true"`。
- 系统启用「减少动态效果」时，脉冲动画停止。
- 系统启用强制颜色模式时，占位块保持可见。

## 原生属性

根元素为 `<div>`。

以下内容传入根元素：

- HTML 属性。
- `aria-*` 属性。
- `data-*` 属性。
- `class`。
- `style`。
- 原生事件监听器。

传入 `aria-hidden` 时，该值覆盖默认值。
