# 空状态

## 使用方法

`EmptyState` 用来占据一块暂时没有内容的区域：搜索没有结果、列表里还没有数据、用户第一次进入某个页面。它比一句干巴巴的“暂无数据”多给出三件事——这块区域现在为什么是空的、将来会有什么、用户此刻可以做什么。

组件由四部分组成：图标或插图、标题、说明文字和操作按钮。其中只有 `title` 是必填的：

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

<template>
  <EmptyState
    title="No results found"
    description="Try adjusting your search terms, or clear the filters."
  >
    <template #icon>
      <!-- 你自己的图标类；UnoCSS 的图标预设会铺满容器，所以给盒子一个尺寸 -->
      <span class="i-carbon-search" style="width: 24px; height: 24px" />
    </template>
    <template #actions>
      <Button label="Clear filters" variant="secondary" />
      <Button label="Create item" variant="primary" />
    </template>
  </EmptyState>
</template>
```

astryx-vue 不自带图标：`icon` 插槽里放什么由你决定，可以是图标库的 CSS 类（例如 UnoCSS 的 `i-carbon-search`），也可以是你自己的 SVG 组件或内联画。注意 UnoCSS 的图标预设会让图形铺满容器，所以要给承载类名的元素一个尺寸；上例用内联样式给出 24px，换成你自己的尺寸类也可以。

图标或插图放进 `icon` 插槽。插槽外面套了一层 `aria-hidden="true"` 的容器，因为插图只是文字的补充，不必让屏幕阅读器单独读出来；放在这里的图标因此也不需要设置 `label`。

`title` 会渲染成一个标题元素，默认是 `h3`。`headingLevel` 只改变标签本身（h1–h6），让标题能接上页面的文档大纲；标题的字号在任何级别下都相同，不会因为选择 `h2` 而变大。

`description` 可以传字符串，也可以用同名插槽传入更富的内容。它渲染成 `<div>` 而不是 `<p>`，所以插槽里可以放列表、链接这类块级内容：

```vue
<EmptyState title="No results found">
  <template #description>
    <ul>
      <li>Check the spelling of the search term.</li>
      <li>Remove one of the active filters.</li>
    </ul>
  </template>
</EmptyState>
```

操作按钮放进 `actions` 插槽，通常是一到两个，给用户一个明确的下一步。与标题、说明一样，`title` 和 `description` 也都各有一个同名插槽，用来覆盖属性传入的纯文本。

在卡片、侧边栏这类空间有限的地方，加上 `isCompact` 会同时收紧间距和字号，操作按钮也从一行改为竖排。

组件自带 `role="status"`，当它在数据加载完成之后才出现时，屏幕阅读器会把内容读出来。传进来的 `class`、`style`、`data-*` 等属性会落到根元素上；显式给出的 `role` 会覆盖默认值。

### 结构

- **图标**: 标题上方的视觉提示，用来强化语境，比如没有结果时的搜索图标。
- **标题**: 说明空的是什么。写「还没有项目」，不要写「没有数据」。
- **说明**: 补充为什么是空的，或者用户可以做什么。
- **操作**: 一到两个按钮，把用户引到下一步，比如「创建项目」或「清除筛选」。

## 插画与图标

`icon` 插槽放在标题上方，居中。放图标还是放插画，看这块区域有多大。

| 区域 | 放什么 | 尺寸 |
| --- | --- | --- |
| 卡片、侧栏、表格内部 | 一个线性图标 | 20 到 24px |
| 整页空状态、首次进入 | 一张扁平插画 | 120 到 200px |
| 引导页、欢迎屏 | 一张扁平插画 | 最多 240px |

`icon` 插槽外面的容器已经标了 `aria-hidden="true"`，插画只是文字的补充。所以这里的图片 `alt` 写空字符串就好，不需要再写一遍标题说过的话。

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

<template>
  <EmptyState
    title="还没有项目"
    description="创建第一个项目，或者从模板开始。"
  >
    <template #icon>
      <img src="/illustrations/empty-projects.svg" alt="" width="160" height="160">
    </template>
  </EmptyState>
</template>
```

几条规范：

- **一套插画用到底。** 同一个产品里不要混不同来源、不同线条粗细的插画。
- **线条和配色跟着主题走。** 插画用 `currentColor`，或者只引 `var(--astryx-color-*)`，深色模式就不用另存一套文件。必须用位图时，准备浅色和深色两张，用 `light-dark()` 或媒体查询切换。
- **尺寸按容器定。** 插画给固定的 `width` 和 `height`，避免加载时抖动。容器变窄时插画跟着缩小，不要让它把卡片撑破。
- **插画只管情绪，标题负责说事。** 去掉插画也要能看懂这块为什么是空的。
- **不要为了填空而放插画。** 一个列表底部没有内容，什么都不放才对；插画留给「这块本来应该有东西，现在没有」的地方。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 除了标题，再给出一个明确的下一步操作，让用户知道接下来能做什么。 |
| 推荐 | 用与场景相符的图标或插图，让人一眼看出缺的是什么内容。 |
| 推荐 | 卡片、侧边栏等空间有限的地方使用 `isCompact`。 |
| 避免 | 只放一个空状态却不给任何指引；一定要说明发生了什么，以及用户可以做什么。 |
| 避免 | 使用“暂无数据”这类宽泛的文案；要具体说明哪里是空的、为什么是空的。 |
| 避免 | 用空状态承载需要用户立即处理的错误；那种情况应该改用 [Banner](/components/banner)。 |

## 示例

### 操作

带图标、说明和操作按钮的完整空状态。搜索没有结果、筛选清空列表，或列表被清空时使用。按钮给用户下一步。

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

<template>
  <EmptyState
    title="No results found"
    description="Try adjusting your search terms or clearing filters to see more results."
  >
    <template #icon>
      <Icon icon="i-heroicons-magnifying-glass" size="lg" />
    </template>
    <template #actions>
      <Button label="Go back" variant="secondary" />
      <Button label="Clear filters" variant="primary" />
    </template>
  </EmptyState>
</template>
```

### 紧凑

间距更小的空状态，适合侧栏、卡片和通知抽屉这类放不下完整空状态的区域。

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { EmptyState } from '@astryx-vue/core/EmptyState'
import { Icon } from '@astryx-vue/core/Icon'
import { HStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <EmptyState
    title="No notifications"
    description="You're all caught up. New notifications will appear here."
    is-compact
  >
    <template #icon>
      <Icon icon="i-heroicons-inbox" size="lg" />
    </template>
    <template #actions>
      <HStack :gap="2">
        <Button label="Settings" variant="secondary" size="sm" />
        <Button label="Refresh" variant="primary" size="sm" />
      </HStack>
    </template>
  </EmptyState>
</template>
```

### 容器

放在 Card 里的空状态，适合首次设置或引导。用户还没创建任何内容时使用，例如项目列表。

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

<template>
  <Card>
    <EmptyState
      title="No projects yet"
      description="Create your first project to start organizing your work. You can invite team members after."
    >
      <template #icon>
        <Icon icon="i-heroicons-folder-plus" size="lg" />
      </template>
      <template #actions>
        <Button label="Import" variant="secondary" />
        <Button label="Create project" variant="primary" />
      </template>
    </EmptyState>
  </Card>
</template>
```

## Props

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `title` | `string` | 必填 | 主要信息，渲染为空状态里的标题元素。要写清“什么是空的”，例如 “No projects yet”，避免 “No data” 这类宽泛说法。 |
| `description` | `string` | - | 标题下方的辅助文字，解释这块区域为什么是空的，或者用户接下来可以做什么。渲染为 `<div>`。 |
| `headingLevel` | `1 \| 2 \| 3 \| 4 \| 5 \| 6` | `3` | 标题的语义级别，只决定渲染的 HTML 标签（h1–h6），使标题能接上文档大纲。标题的视觉大小在任何级别下都相同。 |
| `isCompact` | `boolean` | `false` | 启用紧凑变体：收紧间距和文字大小，适用于卡片、侧边栏等空间受限的区域，操作按钮也会改为竖排。 |

## 插槽

| 名称 | 说明 |
| --- | --- |
| `icon` | 标题上方的图标或插图。外层容器带有 `aria-hidden="true"`，因此放在这里的图标不需要设置 `label`。 |
| `title` | 标题的富内容，会覆盖 `title` 属性，例如在标题里放一个链接。 |
| `description` | 说明文字的富内容，会覆盖 `description` 属性。因为渲染为 `<div>`，这里可以放列表、链接等块级元素。 |
| `actions` | 说明文字下方的操作按钮，通常是一到两个。默认横向排列并自动换行，`isCompact` 为 true 时改为竖向排列。 |

## 原生属性

根元素为 `<div>`。

以下内容传入根元素：

- HTML 属性。
- `aria-*` 属性。
- `data-*` 属性。
- `class`。
- `style`。

根元素默认带有 `role="status"`，显式传入的 `role` 会覆盖它。

## 主题令牌

`packages/themes/neutral/empty-state.css` 提供以下令牌，主题可以覆盖它们：

| 令牌 | 说明 |
| --- | --- |
| `--astryx-empty-state-title-foreground` | 标题颜色，默认与主要文字色一致。 |
| `--astryx-empty-state-description-foreground` | 说明文字颜色，默认与次要文字色一致。 |
| `--astryx-empty-state-text-width` | 标题与说明组成的文字列的最大宽度，默认 360px。 |
