feat: add Badge and Alert components

This commit is contained in:
maoyingjie
2026-10-21 21:32:40 +08:00
parent 3515a47f5e
commit 4f43789f60
41 changed files with 2422 additions and 17 deletions
+148
View File
@@ -0,0 +1,148 @@
---
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)
+189
View File
@@ -0,0 +1,189 @@
---
title: Badge 徽章
description: 相对其他元素定位的小型指示器,常用于通知数量、状态点与标签
component: badge
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Badge, BadgeAnchor } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="badge-basic" />
## 组件结构
Badge 通过 `BadgeAnchor` 相对其他元素定位。纯文本子节点会自动包裹在 `<BadgeLabel>` 中。
> 独立标签用法请使用 [Chip](/components/chip) 组件。
```vue
<script setup lang="ts">
import { Avatar, Badge, BadgeAnchor } from "@heroui-vue/components";
</script>
<template>
<BadgeAnchor>
<Avatar />
<Badge color="danger">5</Badge>
</BadgeAnchor>
</template>
```
`BadgeAnchor` 是定位容器,不是导航链接。
## 示例
### 变体
<ComponentPreview name="badge-variants" />
### 尺寸
<ComponentPreview name="badge-sizes" />
### 颜色
<ComponentPreview name="badge-colors" />
### 徽章位置
<ComponentPreview name="badge-placements" />
### 点状徽标
空徽章作为状态指示器,适用于在线/离线或活动信号。
<ComponentPreview name="badge-dot" />
### 带内容
Badge 支持文本、数字与图标。无子节点时渲染为点状指示器。
<ComponentPreview name="badge-with-content" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="badge-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 Badge 组件类。
```css
@layer components {
.hv-badge {
@apply rounded-full text-xs;
}
.hv-badge__label {
@apply font-semibold;
}
.hv-badge--accent {
@apply shadow-sm;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Badge 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/badge.css)):
#### 基础类 [!toc]
- `.hv-badge` - 基础徽章容器样式
- `.hv-badge__label` - 标签文本 slot 样式
- `.hv-badge-anchor` - 锚定元素的定位包装器
#### 颜色类 [!toc]
- `.hv-badge--accent` - 强调色变体
- `.hv-badge--danger` - 危险色变体
- `.hv-badge--default` - 默认色变体
- `.hv-badge--success` - 成功色变体
- `.hv-badge--warning` - 警告色变体
#### 变体类 [!toc]
- `.hv-badge--primary` - 填充背景的主变体
- `.hv-badge--secondary` - 默认背景的次变体
- `.hv-badge--soft` - 浅色背景的 soft 变体
#### 尺寸类 [!toc]
- `.hv-badge--sm` - 小尺寸
- `.hv-badge--md` - 中尺寸(默认)
- `.hv-badge--lg` - 大尺寸
#### 位置类 [!toc]
- `.hv-badge--top-right` - 右上角(默认)
- `.hv-badge--top-left` - 左上角
- `.hv-badge--bottom-right` - 右下角
- `.hv-badge--bottom-left` - 左下角
#### 复合变体类 [!toc]
Badge 支持组合变体与颜色类(如 `.hv-badge--primary.hv-badge--accent`)。以下组合有默认样式:
**Primary Variants:**
- `.hv-badge--primary.hv-badge--accent` - 填充背景的 primary accent
- `.hv-badge--primary.hv-badge--default` - 填充背景的 primary default
- `.hv-badge--primary.hv-badge--success` - 填充背景的 primary success
- `.hv-badge--primary.hv-badge--warning` - 填充背景的 primary warning
- `.hv-badge--primary.hv-badge--danger` - 填充背景的 primary danger
**Soft Variants:**
- `.hv-badge--soft.hv-badge--accent` - 浅色背景的 soft accent
- `.hv-badge--soft.hv-badge--default` - 浅色背景的 soft default
- `.hv-badge--soft.hv-badge--success` - 浅色背景的 soft success
- `.hv-badge--soft.hv-badge--warning` - 浅色背景的 soft warning
- `.hv-badge--soft.hv-badge--danger` - 浅色背景的 soft danger
## API 参考
### Badge
| Prop | 类型 | 默认值 | 描述 |
| ----------- | -------------------------------------------------------------- | ------------- | -------------------------------------------- |
| 默认插槽 | `Component` | - | 徽章内容(文本、数字或图标)。省略时渲染为点 |
| `color` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | `'default'` | 徽章颜色变体 |
| `variant` | `'primary' \| 'secondary' \| 'soft'` | `'primary'` | 视觉样式变体 |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 徽章尺寸 |
| `placement` | `'top-right' \| 'top-left' \| 'bottom-right' \| 'bottom-left'` | `'top-right'` | 相对锚点的位置 |
| `class` | `string \| Array \| Object` | - | 根元素附加 CSS 类(Vue class 绑定) |
纯文本或数字默认插槽会在渲染时包装为 `BadgeLabel`;显式 `BadgeLabel` 或图标等组合内容不会重复包装。
### BadgeAnchor
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | --------------------------------------- |
| 默认插槽 | `Component` | - | 被锚定元素及 Badge 本身 |
| `class` | `string \| Array \| Object` | - | 锚点包装器附加 CSS 类(Vue class 绑定) |
### BadgeLabel
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | --------------------------------------- |
| 默认插槽 | `Component` | - | 标签文本内容 |
| `class` | `string \| Array \| Object` | - | 标签 slot 附加 CSS 类(Vue class 绑定) |
`class` 遵循 Vue 的 class 绑定,可接收字符串、数组和对象,并与组件或 slot 基础类合并;不要使用 React 的 `className`。
## 相关组件
[Chip 标签](./chip) · [Avatar 头像](./avatar)