Label
通过显式关联为控件提供无障碍命名与点击激活能力的 Shadcn 标签组件。
Label 通过显式引用关联为控件提供无障碍名称(naming)与点击激活/聚焦能力(activation),无需手写 DOM id 或隐式查找相邻节点。本页示例采用 Shadcn Rhea 风格。
表单输入字段请优先使用 Field 组件,内置
FieldLabel、FieldDescription与FieldError自动关联与校验支持。
<div
id="label-overview"
class="flex w-full max-w-xl flex-col gap-4"
>
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root
style-profile="rhea"
data-pair="updates"
>
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root style-profile="rhea" data-pair="updates">
Community updates
</wc-shadcn-label-root>
</div>
<div class="flex items-center gap-2">
<wc-shadcn-switch-root style-profile="rhea" data-pair="notify">
<wc-shadcn-switch-thumb
style-profile="rhea"
></wc-shadcn-switch-thumb>
</wc-shadcn-switch-root>
<wc-shadcn-label-root style-profile="rhea" data-pair="notify">
Enable notifications
</wc-shadcn-label-root>
</div>
<div class="flex flex-col gap-2">
<wc-shadcn-label-root style-profile="rhea" data-pair="project">
Project name
</wc-shadcn-label-root>
<wc-shadcn-input-root
style-profile="rhea"
data-pair="project"
default-value="Proto UI"
></wc-shadcn-input-root>
</div>
<div class="flex flex-col gap-2">
<wc-shadcn-label-root style-profile="rhea" data-pair="notes">
Project notes
</wc-shadcn-label-root>
<wc-shadcn-textarea-root
style-profile="rhea"
data-pair="notes"
default-value="Share a brief summary of your project."
rows="3"
></wc-shadcn-textarea-root>
</div>
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root
style-profile="rhea"
data-pair="passive"
>
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root
style-profile="rhea"
data-pair="passive"
activation="false"
>
Naming-only label (selectable text)
</wc-shadcn-label-root>
</div>
</div>
<script type="module">
import { createControlLabelRef } from '@proto.ui/core';
import { setElementAssociations } from '@proto.ui/adapter-web-component';
const refs = new Map();
document
.querySelectorAll('#label-overview [data-pair]')
.forEach((el) => {
const key = el.getAttribute('data-pair');
if (!key) return;
const ref = refs.get(key) ?? createControlLabelRef();
refs.set(key, ref);
setElementAssociations(el, { controlLabel: ref });
});
</script>此组件由工作区源码提供。请在应用中安装同一工作区提交构建的 CLI、Shadcn 原型包与目标适配器,用该 CLI 初始化项目,再运行本地命令生成组件封装。
npm exec -- proto-ui add wc shadcn-label
pnpm exec proto-ui add wc shadcn-label
把 wc 替换为 react、vue 或 vue2,即可生成对应框架的本地组件封装。也可以直接从 @proto.ui/prototypes-shadcn/label 导入 labelRoot。
每对标签与控件通过 @proto.ui/core 的 createControlLabelRef() 创建唯一引用,并通过适配器通道传入(不放入普通 JSON Props):
import { useMemo } from 'react';import { createControlLabelRef } from '@proto.ui/core';import { ShadcnCheckboxIndicator, ShadcnCheckboxRoot, ShadcnLabelRoot,} from '../proto-ui/components/react';
export function TermsCheckbox() { const termsRef = useMemo(() => createControlLabelRef(), []);
return ( <div className="flex items-center gap-2"> <ShadcnCheckboxRoot styleProfile="rhea" instanceAssociations={{ controlLabel: termsRef }}> <ShadcnCheckboxIndicator styleProfile="rhea" /> </ShadcnCheckboxRoot> <ShadcnLabelRoot styleProfile="rhea" instanceAssociations={{ controlLabel: termsRef }}> Accept terms and conditions </ShadcnLabelRoot> </div> );}Web Components 通过 @proto.ui/adapter-web-component 的 setElementAssociations() 绑定同一引用:
import { createControlLabelRef } from '@proto.ui/core';import { setElementAssociations } from '@proto.ui/adapter-web-component';
const termsRef = createControlLabelRef();setElementAssociations(checkboxEl, { controlLabel: termsRef });setElementAssociations(labelEl, { controlLabel: termsRef });命名、激活与无障碍
Section titled “命名、激活与无障碍”naming(默认true):为目标控件建立无障碍命名关联,无需重复放置隐藏文本。如果目标控件本身已有非空ariaLabel或labelledBy,建议设为naming={false}(仅激活模式)。activation(默认true):点击标签会同步请求目标控件的既有操作——切换Checkbox/Switch、选择Radio Group Item,或聚焦Input/Textarea(不修改文本内容或选区)。设为false可保留命名并关闭点击激活。selection(默认'policy'):可激活标签默认抑制误选文字;仅命名标签与长说明保持自然可选中。需要可激活标签也能复制文字时请设为selection="text"。- 键盘与指针边界:
Label不增加额外Tab停靠点,点击嵌套链接/按钮、带修饰键的点击、拖拽或已取消的指针手势不会误触控件。 - 支持的关联目标:
Checkbox、Switch、Radio Group Item、Input、Textarea、Button、Toggle、Toggle Group Item、独立Select Trigger与Number Field Input。 - 风格预设(
styleProfile):支持vega、nova、maia、lyra、mira、luma、sera、rhea与new-york-v4;默认'inherit'会继承最近的 Style Scope,否则回退到new-york-v4。
将 Label 与 Checkbox 配对,点击标签即可切换勾选状态。
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root style-profile="rhea" id="terms-checkbox">
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root style-profile="rhea" id="terms-label">
Accept terms and conditions
</wc-shadcn-label-root>
</div>
<script type="module">
import { createControlLabelRef } from '@proto.ui/core';
import { setElementAssociations } from '@proto.ui/adapter-web-component';
const termsRef = createControlLabelRef();
const checkbox = document.querySelector('#terms-checkbox');
const label = document.querySelector('#terms-label');
if (
checkbox instanceof HTMLElement &&
label instanceof HTMLElement
) {
setElementAssociations(checkbox, { controlLabel: termsRef });
setElementAssociations(label, { controlLabel: termsRef });
}
</script>在 Field 中使用
Section titled “在 Field 中使用”在完整表单中,使用 Field 组合 FieldLabel、FieldControl、FieldDescription 与 FieldError,并可按需搭配独立的 Label 与 Checkbox。
<div class="flex w-full max-w-md flex-col gap-5">
<wc-shadcn-field-root
style-profile="rhea"
required
validation-mode="onBlur"
>
<wc-shadcn-field-label style-profile="rhea">
Email address
</wc-shadcn-field-label>
<wc-shadcn-field-control
style-profile="rhea"
placeholder="name@example.com"
></wc-shadcn-field-control>
<wc-shadcn-field-description style-profile="rhea">
We will use this address for account security notices.
</wc-shadcn-field-description>
<wc-shadcn-field-error
style-profile="rhea"
></wc-shadcn-field-error>
</wc-shadcn-field-root>
<wc-shadcn-field-root style-profile="rhea">
<wc-shadcn-field-label style-profile="rhea">
Display name
</wc-shadcn-field-label>
<wc-shadcn-field-control
style-profile="rhea"
default-value="Alex Chen"
></wc-shadcn-field-control>
<wc-shadcn-field-description style-profile="rhea">
Shown alongside your comments and reviews.
</wc-shadcn-field-description>
</wc-shadcn-field-root>
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root
style-profile="rhea"
id="field-updates-checkbox"
default-checked
>
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root
style-profile="rhea"
id="field-updates-label"
>
Send me product release notes
</wc-shadcn-label-root>
</div>
</div>
<script type="module">
import { createControlLabelRef } from '@proto.ui/core';
import { setElementAssociations } from '@proto.ui/adapter-web-component';
const updatesRef = createControlLabelRef();
const checkbox = document.querySelector(
'#field-updates-checkbox'
);
const label = document.querySelector('#field-updates-label');
if (
checkbox instanceof HTMLElement &&
label instanceof HTMLElement
) {
setElementAssociations(checkbox, { controlLabel: updatesRef });
setElementAssociations(label, { controlLabel: updatesRef });
}
</script>命名、激活与文本选择
Section titled “命名、激活与文本选择”独立配置 selection="text"(可点击且可复制)、activation={false}(仅命名)与 naming={false}(保留目标自身的 ariaLabel,仅保留点击聚焦)。
<div id="label-modes" class="flex w-full max-w-md flex-col gap-4">
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root
style-profile="rhea"
data-pair="copyable"
>
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root
style-profile="rhea"
data-pair="copyable"
selection="text"
>
Weekly digest (clickable and selectable)
</wc-shadcn-label-root>
</div>
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root
style-profile="rhea"
data-pair="naming-only"
>
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root
style-profile="rhea"
data-pair="naming-only"
activation="false"
>
Release policy notice (naming only)
</wc-shadcn-label-root>
</div>
<div class="flex flex-col gap-2">
<wc-shadcn-label-root
style-profile="rhea"
data-pair="activation-only"
naming="false"
>
Quick filter (activation only)
</wc-shadcn-label-root>
<wc-shadcn-input-root
style-profile="rhea"
data-pair="activation-only"
aria-label="Filter workspace items"
placeholder="Filter workspace items..."
></wc-shadcn-input-root>
</div>
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root
style-profile="rhea"
data-pair="unavailable"
disabled
>
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root
style-profile="rhea"
data-pair="unavailable"
>
Unavailable option
</wc-shadcn-label-root>
</div>
</div>
<script type="module">
import { createControlLabelRef } from '@proto.ui/core';
import { setElementAssociations } from '@proto.ui/adapter-web-component';
const refs = new Map();
document
.querySelectorAll('#label-modes [data-pair]')
.forEach((el) => {
const key = el.getAttribute('data-pair');
if (!key) return;
const ref = refs.get(key) ?? createControlLabelRef();
refs.set(key, ref);
setElementAssociations(el, { controlLabel: ref });
});
</script>从右到左(RTL)
Section titled “从右到左(RTL)”在 dir="rtl" 容器中,Label 使用逻辑文本对齐(text-start)并与 Checkbox、Input 自然排布。
<div
id="label-rtl"
dir="rtl"
class="flex w-full max-w-sm flex-col gap-4"
>
<div class="flex items-center gap-2">
<wc-shadcn-checkbox-root
style-profile="rhea"
data-pair="rtl-terms"
>
<wc-shadcn-checkbox-indicator
style-profile="rhea"
></wc-shadcn-checkbox-indicator>
</wc-shadcn-checkbox-root>
<wc-shadcn-label-root
style-profile="rhea"
data-pair="rtl-terms"
>
قبول الشروط والأحكام
</wc-shadcn-label-root>
</div>
<div class="flex flex-col gap-2">
<wc-shadcn-label-root
style-profile="rhea"
data-pair="rtl-email"
>
البريد الإلكتروني
</wc-shadcn-label-root>
<wc-shadcn-input-root
style-profile="rhea"
data-pair="rtl-email"
placeholder="name@example.com"
></wc-shadcn-input-root>
</div>
</div>
<script type="module">
import { createControlLabelRef } from '@proto.ui/core';
import { setElementAssociations } from '@proto.ui/adapter-web-component';
const refs = new Map();
document
.querySelectorAll('#label-rtl [data-pair]')
.forEach((el) => {
const key = el.getAttribute('data-pair');
if (!key) return;
const ref = refs.get(key) ?? createControlLabelRef();
refs.set(key, ref);
setElementAssociations(el, { controlLabel: ref });
});
</script>| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
naming | boolean | true | 是否为关联控件提供无障碍名称 |
activation | boolean | true | 点击标签时是否请求目标控件的默认操作 |
selection | 'policy' 'auto' 'text' 'none' | 'policy' | 文本选择策略;默认仅在可激活状态下抑制误选 |
styleProfile | 'inherit' 'rhea' 'vega' 'nova' 'maia' 'lyra' 'mira' 'luma' 'sera' 'new-york-v4' | 'inherit' | 排版预设;默认继承最近的 Style Scope,否则回退到 new-york-v4 |
关联辅助接口
Section titled “关联辅助接口”createControlLabelRef():从@proto.ui/core导入,为每对标签与控件创建一对一不透明引用。instanceAssociations:React / Vue / Vue 2 传入{ controlLabel: ref }的适配器关联通道。setElementAssociations(element, { controlLabel: ref }):从@proto.ui/adapter-web-component导入,用于 Web Components 绑定。