Files
heroui-vue/apps/docs/components/label.md
T
maoyingjie dc859fb8ae feat(styles): export shared field variant maps and restore the border fallback
HD-05 scope, against heroui ce90ea3 styles sources:

- add labelVariants/descriptionVariants/fieldErrorVariants/
  textFieldVariants/switchGroupVariants (+ VariantProps types) to
  @heroui-vue/styles and re-export them from @heroui-vue/components;
  variant keys, defaultVariants and slot sets match the pinned
  *.styles.ts files key for key (hv- namespace only)
- Label/Description/FieldError/useTextField/SwitchGroup now consume
  those mappings instead of inline class strings, so the state classes
  have a single source; rendered class output is unchanged and pinned
  by tests/shared-field-styles.test.ts (mapping output <-> component
  output)
- restore the global border-color fallback
  (*, ::after, ::before, ::backdrop, ::file-selector-button ->
  border-color: var(--border, currentColor)) in a new base/base.css,
  imported layer(base) right after preflight like the pinned entry:
  a low-priority default that components, utilities and consumer CSS
  override without !important
- view-transition globals from the pinned base.css stay unported: no
  View Transitions API usage exists in the library or docs site
- shared-styles package contract: packed runtime exports from both
  entries, variant-output assertions, CSS tokens incl. the fallback,
  strict nodenext+bundler type contract, and a real-browser consumer
  fixture (light/dark --border, explicit colors, consumer overrides,
  currentColor fallback) wired into check-package.mjs
- docs: styling layers/border fallback/exports notes, five component
  API pages, compatibility matrix and upstream source records

Full suite 111 files / 1634 tests, typecheck and build:docs pass;
shared-styles + api-root-aliases + handbook-styles verified against
the final tarballs.
2026-10-28 23:36:59 +08:00

4.0 KiB
Raw Blame History

title, description, component, group
title description component group
Label 标签 渲染与表单控件关联的无障碍标签。 label 组件

用法

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

示例

带必填指示符

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

<template>
  <Label for="email" is-required>邮箱地址</Label>
  <Input id="email" type="email" />
</template>

禁用状态

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

<template>
  <Label for="username" is-disabled>用户名</Label>
  <Input id="username" disabled />
</template>

无效状态

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

<template>
  <Label for="password" is-invalid>密码</Label>
  <Input id="password" invalid />
</template>

在 TextField 内使用时,is-required / is-disabled / is-invalid 会自动继承字段状态,无需重复标注。

自定义样式

Tailwind CSS

全局 CSS

若要自定义组件类,可使用 @layer components 指令。

@layer components {
  .hv-label {
    @apply font-semibold text-foreground;
  }

  .hv-label--required::after {
    @apply text-accent;
  }
}

样式参考

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

CSS 类

Label 使用以下 CSS 类(查看源码样式):

变体映射 labelVariants 从 @heroui-vue/styles 公开(@heroui-vue/components 再导出同一函数与 LabelVariants 类型);组件渲染的类名由该映射生成,状态修饰类随 isDisabled / isInvalid / isRequired 输出。

基础类 [!toc]

  • .hv-label — 基础标签文本样式

状态修饰类 [!toc]

  • .hv-label--required 或 [data-required="true"] > .hv-label — 显示必填星号
  • .hv-label--disabled 或 [data-disabled="true"] .hv-label — 禁用状态样式
  • .hv-label--invalid 或 [data-invalid="true"] .hv-label — 无效状态样式(危险/红色文本)

说明: 必填星号会基于 role 与 data-slot 智能应用,并排除 role="group" / radiogroup / checkboxgroup 以及 data-slot="radio" / checkbox 的元素,避免分组内重复星号。

API 参考

Label

Prop 类型 默认值 描述
for string - 标签所关联元素的 id(Vue 使用 for)
isRequired boolean false 是否显示必填指示符
isDisabled boolean false 标签是否处于禁用状态
isInvalid boolean false 标签是否处于无效状态
默认插槽 Component - 标签内容
class string | Array | Object - 附加 CSS 类(Vue class 绑定)

在 TextField 内且未显式传 for 时,自动关联当前控件 id。Switch 等 toggle 字段内渲染为非交互 span,避免嵌套交互。

无障碍

Label 基于原生 HTML <label>,并遵循 WAI-ARIA 最佳实践:

  • 使用 for 与表单控件关联(上游 htmlFor 的 Vue 等价)
  • 提供语义化的 <label> 元素
  • 与表单控件关联时支持键盘导航
  • 向屏幕阅读器传达必填与无效状态
  • 点击标签可聚焦/激活关联的表单控件

相关组件

Input 输入框 · TextArea 多行文本框 · Fieldset 字段组