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
+2
View File
@@ -44,8 +44,10 @@ const groups = [
{
title: "组件",
items: [
{ title: "Alert 警告", url: "/components/alert" },
{ title: "Avatar 头像", url: "/components/avatar" },
{ title: "AvatarGroup 头像组", url: "/components/avatar-group" },
{ title: "Badge 徽章", url: "/components/badge" },
{ title: "Button 按钮", url: "/components/button" },
{ title: "Card 卡片", url: "/components/card" },
{ title: "Chip 标签", url: "/components/chip" },
+2
View File
@@ -3,8 +3,10 @@ title: 组件
description: 已迁移组件及文档
---
- [Alert 警告](./components/alert)
- [Avatar 头像](./components/avatar)
- [AvatarGroup 头像组](./components/avatar-group)
- [Badge 徽章](./components/badge)
- [Button 按钮](./components/button)
- [Card 卡片](./components/card)
- [Chip 标签](./components/chip)
+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)
+92
View File
@@ -0,0 +1,92 @@
<script setup lang="ts">
import {
Alert,
AlertContent,
AlertDescription,
AlertIndicator,
AlertTitle,
Button,
CloseButton,
Spinner,
} from "@heroui-vue/components";
</script>
<template>
<div class="grid w-full max-w-xl gap-4">
<!-- 默认 — 一般信息 -->
<Alert>
<AlertIndicator />
<AlertContent>
<AlertTitle>新功能已上线</AlertTitle>
<AlertDescription>
查看我们的最新更新,包括深色模式支持与改进的无障碍体验。
</AlertDescription>
</AlertContent>
</Alert>
<!-- 强调 — 重要信息含操作 -->
<Alert status="accent">
<AlertIndicator />
<AlertContent>
<AlertTitle>有可用更新</AlertTitle>
<AlertDescription>
应用有新版本可用。请刷新页面以获取最新功能与问题修复。
</AlertDescription>
<Button class="mt-2 sm:hidden" size="sm" variant="primary">刷新</Button>
</AlertContent>
<Button class="hidden sm:block" size="sm" variant="primary">刷新</Button>
</Alert>
<!-- 危险 — 错误与排查步骤 -->
<Alert status="danger">
<AlertIndicator />
<AlertContent>
<AlertTitle>无法连接到服务器</AlertTitle>
<AlertDescription>
当前遇到连接问题,请尝试以下操作:
<ul class="mt-2 list-inside list-disc space-y-1 text-sm">
<li>检查网络连接</li>
<li>刷新页面</li>
<li>清除浏览器缓存</li>
</ul>
</AlertDescription>
<Button class="mt-2 sm:hidden" size="sm" variant="danger">重试</Button>
</AlertContent>
<Button class="hidden sm:block" size="sm" variant="danger">重试</Button>
</Alert>
<!-- 无描述 -->
<Alert status="success">
<AlertIndicator />
<AlertContent>
<AlertTitle>个人资料已更新</AlertTitle>
</AlertContent>
<CloseButton />
</Alert>
<!-- 自定义指示器 — 加载中 -->
<Alert status="accent">
<AlertIndicator>
<Spinner size="sm" />
</AlertIndicator>
<AlertContent>
<AlertTitle>正在处理你的请求</AlertTitle>
<AlertDescription>
正在同步你的数据,请稍候,这可能需要一点时间。
</AlertDescription>
</AlertContent>
</Alert>
<!-- 无关闭按钮 -->
<Alert status="warning">
<AlertIndicator />
<AlertContent>
<AlertTitle>计划维护</AlertTitle>
<AlertDescription>
我们将于 UTC 时间 3 月 15 日(周日)凌晨 2:00 至上午 6:00
进行计划维护,期间服务将暂时不可用。
</AlertDescription>
</AlertContent>
</Alert>
</div>
</template>
+44
View File
@@ -0,0 +1,44 @@
<script setup lang="ts">
import {
Alert,
AlertContent,
AlertDescription,
AlertIndicator,
AlertTitle,
Button,
CloseButton,
} from "@heroui-vue/components";
</script>
<template>
<div class="w-full max-w-xl">
<Alert
class="relative overflow-hidden rounded-xl border border-warning/20 bg-linear-to-br from-warning/10 via-surface to-surface-secondary shadow-sm dark:border-warning/30 dark:from-warning/15 dark:via-surface dark:to-warning/5"
status="warning"
>
<div
aria-hidden="true"
class="pointer-events-none absolute -top-8 -right-8 size-28 rounded-full bg-warning/15 blur-2xl dark:bg-warning/25"
/>
<AlertIndicator class="relative text-warning" />
<AlertContent class="relative">
<AlertTitle>支付方式即将过期</AlertTitle>
<AlertDescription>
您的 Visa 卡(尾号 4242)将于 3 月 28 日过期。请更新账单信息,以免影响
Pro 订阅。
</AlertDescription>
<Button class="mt-3 sm:hidden" size="sm" variant="tertiary">
更新账单
</Button>
</AlertContent>
<Button
class="relative hidden shrink-0 sm:inline-flex"
size="sm"
variant="tertiary"
>
更新账单
</Button>
<CloseButton class="relative" />
</Alert>
</div>
</template>
+44
View File
@@ -0,0 +1,44 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
} from "@heroui-vue/components";
const GREEN_AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg";
const ORANGE_AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg";
const BLUE_AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg";
</script>
<template>
<div class="flex items-center gap-6">
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="GREEN_AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge color="danger" size="sm">5</Badge>
</BadgeAnchor>
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="ORANGE_AVATAR_URL" />
<AvatarFallback>AB</AvatarFallback>
</Avatar>
<Badge color="accent" size="sm">新</Badge>
</BadgeAnchor>
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="BLUE_AVATAR_URL" />
<AvatarFallback>CD</AvatarFallback>
</Avatar>
<Badge color="success" placement="bottom-right" size="sm" />
</BadgeAnchor>
</div>
</template>
+26
View File
@@ -0,0 +1,26 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
} from "@heroui-vue/components";
const AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg";
const colors = ["default", "accent", "success", "warning", "danger"] as const;
</script>
<template>
<div class="flex items-center gap-6">
<BadgeAnchor v-for="color in colors" :key="color">
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge :color="color" size="sm" />
</BadgeAnchor>
</div>
</template>
+29
View File
@@ -0,0 +1,29 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
} from "@heroui-vue/components";
</script>
<template>
<BadgeAnchor>
<Avatar>
<AvatarImage
alt="凯特·威尔逊"
src="https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg"
/>
<AvatarFallback>KW</AvatarFallback>
</Avatar>
<Badge
class="min-w-5 font-semibold tabular-nums"
color="accent"
size="sm"
variant="soft"
>
5
</Badge>
</BadgeAnchor>
</template>
+26
View File
@@ -0,0 +1,26 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
} from "@heroui-vue/components";
const AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg";
const colors = ["accent", "success", "warning", "danger"] as const;
</script>
<template>
<div class="flex items-center gap-6">
<BadgeAnchor v-for="color in colors" :key="color">
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge :color="color" placement="bottom-right" size="sm" />
</BadgeAnchor>
</div>
</template>
+45
View File
@@ -0,0 +1,45 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
} from "@heroui-vue/components";
const AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg";
const placements = [
"top-right",
"top-left",
"bottom-right",
"bottom-left",
] as const;
const PLACEMENT_LABELS: Record<(typeof placements)[number], string> = {
"bottom-left": "左下",
"bottom-right": "右下",
"top-left": "左上",
"top-right": "右上",
};
</script>
<template>
<div class="flex items-center gap-8">
<div
v-for="placement in placements"
:key="placement"
class="flex flex-col items-center gap-2"
>
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge color="accent" :placement="placement" size="sm" />
</BadgeAnchor>
<span class="text-xs text-muted">{{ PLACEMENT_LABELS[placement] }}</span>
</div>
</div>
</template>
+26
View File
@@ -0,0 +1,26 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
} from "@heroui-vue/components";
const AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg";
const sizes = ["sm", "md", "lg"] as const;
</script>
<template>
<div class="flex items-center gap-6">
<BadgeAnchor v-for="size in sizes" :key="size">
<Avatar :size="size">
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge color="danger" :size="size">5</Badge>
</BadgeAnchor>
</div>
</template>
+45
View File
@@ -0,0 +1,45 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
Separator,
} from "@heroui-vue/components";
const AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg";
const variants = ["primary", "secondary", "soft"] as const;
const VARIANT_LABELS: Record<(typeof variants)[number], string> = {
primary: "主色",
secondary: "次色",
soft: "柔和",
};
const colors = ["accent", "default", "success", "warning", "danger"] as const;
</script>
<template>
<div class="flex flex-col gap-8">
<template v-for="(variant, index) in variants" :key="variant">
<div class="flex flex-col gap-4">
<h3 class="text-sm font-semibold text-muted">
{{ VARIANT_LABELS[variant] }}
</h3>
<div class="flex items-center gap-6">
<BadgeAnchor v-for="color in colors" :key="color">
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge :color="color" size="sm" :variant="variant">5</Badge>
</BadgeAnchor>
</div>
</div>
<Separator v-if="index < variants.length - 1" />
</template>
</div>
</template>
+51
View File
@@ -0,0 +1,51 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Badge,
BadgeAnchor,
} from "@heroui-vue/components";
import { IconBell } from "@tabler/icons-vue";
const AVATAR_URL =
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg";
</script>
<template>
<div class="flex items-center gap-6">
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge color="danger" size="sm">5</Badge>
</BadgeAnchor>
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge color="danger" size="sm">新</Badge>
</BadgeAnchor>
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge color="danger" size="sm">99+</Badge>
</BadgeAnchor>
<BadgeAnchor>
<Avatar>
<AvatarImage alt="头像" :src="AVATAR_URL" />
<AvatarFallback>JD</AvatarFallback>
</Avatar>
<Badge color="accent" size="sm">
<IconBell class="size-2.5" aria-hidden="true" />
</Badge>
</BadgeAnchor>
</div>
</template>