Files
heroui-vue/apps/docs/components/badge.md
T
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

6.1 KiB

title, description, component, group
title description component group
Badge 徽章 相对其他元素定位的小型指示器,常用于通知数量、状态点与标签 badge 组件

用法

<script setup lang="ts">
import { Badge, BadgeAnchor } from "@heroui-vue/components";
</script>

组件结构

Badge 通过 BadgeAnchor 相对其他元素定位。纯文本子节点会自动包裹在 <BadgeLabel> 中。

独立标签用法请使用 Chip 组件。

<script setup lang="ts">
import { Avatar, Badge, BadgeAnchor } from "@heroui-vue/components";
</script>

<template>
  <BadgeAnchor>
    <Avatar />
    <Badge color="danger">5</Badge>
  </BadgeAnchor>
</template>

BadgeAnchor 是定位容器,不是导航链接。

示例

变体

尺寸

颜色

徽章位置

点状徽标

空徽章作为状态指示器,适用于在线/离线或活动信号。

带内容

Badge 支持文本、数字与图标。无子节点时渲染为点状指示器。

自定义样式

Tailwind CSS

全局 CSS

可使用 @layer components 指令自定义 Badge 组件类。

@layer components {
  .hv-badge {
    @apply rounded-full text-xs;
  }

  .hv-badge__label {
    @apply font-semibold;
  }

  .hv-badge--accent {
    @apply shadow-sm;
  }
}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。

CSS 类

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。

相关组件

Avatar 头像 · Chip 标签