# Alert

> 在页面内展示信息、成功、警告或错误，支持图标、操作与关闭。

网页来源：/components/alert

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

Alert 将提示保留在相关内容旁，适合展示状态说明、操作结果和需要留意的信息。
标题、说明、图标与操作可以独立组合。

## 基本用法

通过 `title`、`description` 提供提示内容，默认使用信息色与浅底描边。静态说明保留默认角色；动态结果可使用 `role="status"`。

### 示例：alert/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { QuireAlert } from '@quirekit/ui/vue';
</script>
<template>
  <div class="p-qk-surface"><QuireAlert title="有可用更新" description="新版本已准备就绪，你可以在方便的时候更新。" /></div>
</template>
```

## 色彩

使用 `color` 选择六种语义色族。下面可以同时切换所有提示的外观；右上角主题设置可切换明暗模式。

### 示例：alert/colors

文件：`colors.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import type { QuireKitAlertColor, QuireKitAlertVariant } from "@quirekit/ui";
import { QuireAlert, QuireSegmented } from "@quirekit/ui/vue";

const variant = ref<string>("subtle");
const variants = [
  { value: "subtle", label: "浅底描边" }, { value: "soft", label: "浅底" },
  { value: "outline", label: "描边" }, { value: "solid", label: "实色" }
];
const messages: readonly { color: QuireKitAlertColor; title: string; description: string }[] = [
  { color: "neutral", title: "工作区提示", description: "你可以在设置中管理工作区的偏好。" },
  { color: "primary", title: "发现新的可能", description: "选择适合你的主题，让工作区更有自己的风格。" },
  { color: "info", title: "有可用更新", description: "新版本已准备就绪，你可以在方便的时候更新。" },
  { color: "success", title: "更改已保存", description: "所有更改均已同步，你可以放心关闭此页面。" },
  { color: "warning", title: "存储空间即将用尽", description: "剩余空间不足 10%，建议清理旧文件或增加容量。" },
  { color: "danger", title: "上传未完成", description: "网络连接中断，请检查连接后重新尝试。" }
];
</script>

<template>
  <section aria-label="Alert 色彩比较" class="flex min-w-0 flex-col gap-5 p-qk-surface">
    <div class="max-w-full overflow-x-auto pb-1">
      <QuireSegmented v-model="variant" label="Alert 外观" :items="variants" />
    </div>
    <div class="grid min-w-0 gap-4">
      <div v-for="item in messages" :key="item.color" class="grid min-w-0 gap-2">
        <span class="text-xs font-mono text-qk-text-secondary">{{ item.color }} · {{ variant }}</span>
        <QuireAlert :color="item.color" :variant="variant as QuireKitAlertVariant" :title="item.title" :description="item.description" />
      </div>
    </div>
  </section>
</template>
```

## 外观

`subtle` 是默认外观，以浅色背景和细边框组织内容。`soft` 只保留浅色背景，
`outline` 使用透明背景与色彩边框，`solid` 以实色背景强调信息。

### 示例：alert/variants

文件：`variants.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import type { QuireKitAlertColor, QuireKitAlertVariant } from "@quirekit/ui";
import { QuireAlert, QuireSelect } from "@quirekit/ui/vue";

const color = ref("warning");
const colors = ["neutral", "primary", "info", "success", "warning", "danger"].map(value => ({ value, label: value }));
const variants: readonly QuireKitAlertVariant[] = ["subtle", "soft", "outline", "solid"];
</script>

<template>
  <section aria-label="Alert 外观比较" class="grid min-w-0 gap-5 p-qk-surface">
    <QuireSelect v-model="color" label="比较色族" :items="colors" class="w-48 max-w-full" />
    <div v-for="variant in variants" :key="variant" class="grid min-w-0 gap-2">
      <span class="text-xs font-mono text-qk-text-secondary">{{ variant }}</span>
      <QuireAlert :color="color as QuireKitAlertColor" :variant="variant" title="请留意账户状态" description="部分设置即将发生变化，请在继续之前确认相关信息。" />
    </div>
  </section>
</template>
```

## 操作与关闭

在 `actions` 插槽中组合按钮或链接。设置 `closable` 显示关闭图标，其名称跟随 [Locale Provider](/raw/components/locale-provider.md) 的语言。
通过 `v-model:open` 控制显示状态，默认显示；用户关闭时同时触发 `close` 事件。

下面的示例可以保存设置、关闭和重新显示提示。关闭后，焦点移到恢复按钮。

### 示例：alert/actions

文件：`actions.vue`

```vue
<script setup lang="ts">
import { nextTick, ref } from "vue";
import { QuireAlert, QuireButton } from "@quirekit/ui/vue";

const example = ref<HTMLElement | null>(null);
const open = ref(true);
const saved = ref(false);
const restore = ref<InstanceType<typeof QuireButton> | null>(null);

/** 关闭提示后，将键盘焦点移到稳定的恢复入口。 */
async function afterClose(): Promise<void> {
  await nextTick();
  (restore.value?.$el as HTMLButtonElement | undefined)?.focus();
}

/** 保存后将焦点移到仍然存在的关闭入口。 */
async function save(): Promise<void> {
  saved.value = true;
  await nextTick();
  example.value?.querySelector<HTMLButtonElement>(".qk-alert__close")?.focus();
}

/** 恢复示例的初始状态。 */
function reset(): void {
  saved.value = false;
  open.value = true;
}
</script>

<template>
  <section ref="example" aria-label="可关闭提示示例" class="grid min-w-0 gap-4 p-qk-surface">
    <div class="min-h-40">
      <QuireAlert v-model:open="open" :color="saved ? 'success' : 'warning'" :title="saved ? '设置已更新' : '更改尚未保存'" :description="saved ? '新的偏好将在下次访问时生效。' : '离开前保存你的设置，方便下次继续使用。'" closable role="status" @close="afterClose">
        <template v-if="!saved" #actions>
          <QuireButton size="sm" color="warning" variant="outline" @click="save">保存设置</QuireButton>
          <QuireButton size="sm" variant="ghost" @click="open = false; afterClose()">稍后处理</QuireButton>
        </template>
      </QuireAlert>
    </div>
    <div><QuireButton ref="restore" variant="outline" @click="reset">重新显示提示</QuireButton></div>
  </section>
</template>
```

## 内容组合与窄布局

标题与说明均可省略。`icon` 默认按色族选择，也可以传入已注册图标名，或设置为 `false` 隐藏。
使用 `title`、`description`、`icon` 插槽替换局部内容，默认插槽可补充列表等正文。
长文本会换行，操作区随容器宽度换行。

### 示例：alert/content

文件：`content.vue`

```vue
<script setup lang="ts">
import { QuireAlert, QuireIcon } from "@quirekit/ui/vue";
</script>

<template>
  <div class="grid min-w-0 gap-4 p-qk-surface">
    <QuireAlert color="info" description="维护将在今晚 23:00 开始，预计持续 15 分钟。" />
    <QuireAlert color="primary" title="开始前的准备">
      <template #icon><QuireIcon name="circleCheck" :size="20" /></template>
      <ul class="m-0 ps-5 leading-6">
        <li>检查个人资料与通知偏好。</li>
        <li>邀请协作者，一起完成项目设置。</li>
      </ul>
    </QuireAlert>
    <div class="w-full max-w-xs">
      <QuireAlert color="warning" title="检查导入内容" closable>
        <template #description>文件 <code class="text-inherit">quarterly-report-with-a-very-long-name-2026.csv</code> 缺少必需字段，请补充后再上传。</template>
      </QuireAlert>
    </div>
    <QuireAlert color="neutral" :icon="false" title="阅读提示" description="你可以只保留文字，适应更安静的内容区域。" />
  </div>
</template>
```

## 可访问性

- 默认 `role="group"`，按页面阅读顺序呈现，存在标题时由标题命名。
- 动态、非紧急的状态更新可以设置 `role="status"`；需要立即通知用户的错误可以设置 `role="alert"`。
- 图标是装饰内容；标题或说明应明确表达提示含义。
- 关闭按钮支持 Tab 聚焦及 Enter、空格激活。关闭后的焦点位置由页面结合后续操作安排。

## API

### QuireAlert

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| title | 否 | — | 提示标题，也可通过 title 插槽提供。 |
| description | 否 | — | 标题下方的补充说明。 |
| color | 否 | "info" | 提示的语义色族。 |
| variant | 否 | "subtle" | 背景与边框外观。 |
| icon | 否 | undefined | 已注册的图标名；false 隐藏图标，省略时按色族选择。 |
| closable | 否 | false | 显示图标关闭按钮。 |
| role | 否 | "group" | group 用于静态内容；status 温和播报更新，alert 播报紧急更新。 |
| open | 否 | true | 控制提示是否显示，支持 v-model\:open。 |

**title**

```ts
string | undefined
```

```ts
undefined | string
```

**description**

```ts
string | undefined
```

```ts
undefined | string
```

**color**

```ts
QuireKitAlertColor | undefined
```

```ts
undefined | "info" | "neutral" | "primary" | "success" | "warning" | "danger"
```

**variant**

```ts
QuireKitAlertVariant | undefined
```

```ts
undefined | "subtle" | "soft" | "outline" | "solid"
```

**icon**

```ts
string | false | undefined
```

```ts
undefined | string | false
```

**closable**

```ts
boolean | undefined
```

**role**

```ts
QuireKitAlertRole | undefined
```

```ts
undefined | "group" | "status" | "alert"
```

**open**

```ts
boolean | undefined
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| update\:open | 否 | — |  |
| close | 否 | — |  |

**update:open**

```ts
[value: boolean]
```

**close**

```ts
[]
```

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| icon | 否 | — | 替换默认装饰图标。 |
| title | 否 | — | 替换标题内容。 |
| description | 否 | — | 替换说明内容，可包含链接。 |
| default | 否 | — | 补充列表或其他正文内容。 |
| actions | 否 | — | 操作按钮与链接区域。 |

**icon**

```ts
any
```

**title**

```ts
any
```

**description**

```ts
any
```

**default**

```ts
any
```

**actions**

```ts
any
```

#### 公开实例成员

无。
