Files
heroui-vue/apps/docs/components/alert.md
T
2026-10-21 21:32:40 +08:00

149 lines
5.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Alert 警告
description: 向用户展示重要消息与通知,并提供状态指示
component: alert
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Alert,
AlertContent,
AlertDescription,
AlertIndicator,
AlertTitle,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview name="alert-basic" />
## 组件结构
```vue
<script setup lang="ts">
import {
Alert,
AlertContent,
AlertDescription,
AlertIndicator,
AlertTitle,
} from "@heroui-vue/components";
</script>
<template>
<Alert>
<AlertIndicator />
<AlertContent>
<AlertTitle />
<AlertDescription />
</AlertContent>
</Alert>
</template>
```
省略 `AlertIndicator` 默认插槽时显示随 `status` 变化的状态图标;传入自定义内容会替换默认图标。
## 自定义样式
### Tailwind CSS
<ComponentPreview name="alert-custom-styles" />
### 全局 CSS
要自定义 Alert 的组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-alert {
@apply rounded-2xl shadow-lg;
}
.hv-alert__title {
@apply font-bold text-lg;
}
.hv-alert--danger {
@apply border-l-4 border-red-600;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Alert 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/alert.css)):
#### 基础类 [!toc]
- `.hv-alert` - Alert 根容器
- `.hv-alert__indicator` - 图标/指示器容器
- `.hv-alert__content` - 包裹标题与说明的内容容器
- `.hv-alert__title` - Alert 标题文本
- `.hv-alert__description` - Alert 说明文本
#### 状态变体类 [!toc]
- `.hv-alert--default` - 默认灰色状态
- `.hv-alert--accent` - 强调蓝色状态
- `.hv-alert--success` - 成功绿色状态
- `.hv-alert--warning` - 警告黄/橙色状态
- `.hv-alert--danger` - 危险红色状态
### 交互状态
Alert 主要用于信息展示,基础组件本身通常没有交互状态;但它可以包含按钮或关闭按钮等交互元素。不会自动获得焦点、弹窗或自动关闭行为。
## API 参考
### Alert
| Prop | 类型 | 默认值 | 描述 |
| -------- | ------------------------------------------------------------- | ----------- | ----------------------------- |
| `status` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | `'default'` | Alert 的视觉状态 |
| 默认插槽 | `Component` | - | Alert 内容 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### AlertIndicator
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ------------------------------------------------------------ |
| 默认插槽 | `Component` | - | 自定义指示图标;省略或内容为空时显示随 status 更新的默认图标 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### AlertContent
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ----------------------------------- |
| 默认插槽 | `Component` | - | 内容(通常为 Title 与 Description) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### AlertTitle
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ----------------------------- |
| 默认插槽 | `Component` | - | Alert 标题文本 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### AlertDescription
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ----------------------------- |
| 默认插槽 | `Component` | - | Alert 说明文本 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
`class` 遵循 Vue 的 class 绑定,可接收字符串、数组和对象,并与对应 slot 的基础类合并;不要使用 React 的 `className`。命名导出为 `Alert` / `AlertIndicator` / `AlertContent` / `AlertTitle` / `AlertDescription`,不使用 `Alert.Indicator` 对象式子部件。
Alert 会把内部 `surfaceContext` 重置为 `default`,避免嵌套在其他 Surface 中时意外继承外层表面变体。
## 相关组件
[Button 按钮](./button) · [CloseButton 关闭按钮](./close-button) · [Spinner 加载指示器](./spinner)