Label
保留归档 Duostrap 排版并通过显式引用关联控件命名与点击激活的标签组件。
Duostrap Label 保留经典 14px / 20px Helvetica Neue 常规字重排版,并通过显式引用与目标控件配对,可独立控制无障碍命名(naming)与点击激活(activation)行为。
表单输入字段请优先使用 Field 组件,内置
FieldLabel、FieldDescription与FieldError自动关联与校验支持。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div
id="label-overview"
class="flex w-full max-w-xl flex-col gap-4"
>
<div class="flex items-center gap-2">
<wc-duostrap-checkbox-root data-pair="updates">
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root data-pair="updates">
Community updates
</wc-duostrap-label-root>
</div>
<div class="flex items-center gap-2">
<wc-duostrap-switch-root data-pair="notify">
<wc-duostrap-switch-thumb></wc-duostrap-switch-thumb>
</wc-duostrap-switch-root>
<wc-duostrap-label-root data-pair="notify">
Enable notifications
</wc-duostrap-label-root>
</div>
<div class="flex flex-col gap-2">
<wc-duostrap-label-root data-pair="project">
Project name
</wc-duostrap-label-root>
<wc-duostrap-input-root
data-pair="project"
default-value="Proto UI"
></wc-duostrap-input-root>
</div>
<div class="flex flex-col gap-2">
<wc-duostrap-label-root data-pair="notes">
Project notes
</wc-duostrap-label-root>
<wc-duostrap-textarea-root
data-pair="notes"
default-value="Share a brief summary of your project."
rows="3"
></wc-duostrap-textarea-root>
</div>
<div class="flex items-center gap-2">
<wc-duostrap-checkbox-root data-pair="passive">
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root data-pair="passive" activation="false">
Naming-only label (selectable text)
</wc-duostrap-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>当前仅在源码工作区提供,可直接从工作区包导入:
import { labelRoot } from '@proto.ui/prototypes-duostrap/label';通过 @proto.ui/core 的 createControlLabelRef() 为每对标签与控件创建稳定引用,并通过适配器通道传入:
import { useMemo } from 'react';import { createControlLabelRef } from '@proto.ui/core';import { BootstrapCheckboxIndicator, BootstrapCheckboxRoot, BootstrapLabelRoot,} from '../proto-ui/components/react';
export function TermsCheckbox() { const termsRef = useMemo(() => createControlLabelRef(), []);
return ( <div className="flex items-center gap-2"> <BootstrapCheckboxRoot instanceAssociations={{ controlLabel: termsRef }}> <BootstrapCheckboxIndicator /> </BootstrapCheckboxRoot> <BootstrapLabelRoot instanceAssociations={{ controlLabel: termsRef }}> Accept terms and conditions </BootstrapLabelRoot> </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):让目标控件自动引用该Label作为无障碍名称;若目标已声明非空ariaLabel或labelledBy,建议设为naming={false}仅用于点击激活。activation(默认true):点击已配对的Label会触发目标控件的默认操作(切换Checkbox/Switch、选择Radio Group Item或聚焦Input/Textarea)。如需仅被动命名,请设为activation={false}。selection(默认'policy'):可激活标签默认防止误选文字;需要支持选中复制时可设为selection="text",或使用'auto'/'none'显式覆盖。- 对齐与无障碍:采用
inline-flex布局且不附加硬编码下外边距,既能与行内勾选框垂直居中对齐,也便于在垂直表单容器中按容器间距排布;不增加额外Tab停靠点,并过滤嵌套交互元素、修饰键点击与拖拽。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="flex items-center gap-2">
<wc-duostrap-checkbox-root id="terms-checkbox">
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root id="terms-label">
Accept terms and conditions
</wc-duostrap-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 中使用” Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="flex w-full max-w-md flex-col gap-5">
<wc-duostrap-field-root required validation-mode="onBlur">
<wc-duostrap-field-label>Email address</wc-duostrap-field-label>
<wc-duostrap-field-control
placeholder="name@example.com"
></wc-duostrap-field-control>
<wc-duostrap-field-description>
We will use this address for account security notices.
</wc-duostrap-field-description>
<wc-duostrap-field-error></wc-duostrap-field-error>
</wc-duostrap-field-root>
<wc-duostrap-field-root>
<wc-duostrap-field-label>Display name</wc-duostrap-field-label>
<wc-duostrap-field-control
default-value="Alex Chen"
></wc-duostrap-field-control>
<wc-duostrap-field-description>
Shown alongside your comments and reviews.
</wc-duostrap-field-description>
</wc-duostrap-field-root>
<div class="flex items-center gap-2">
<wc-duostrap-checkbox-root
id="field-updates-checkbox"
default-checked
>
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root id="field-updates-label">
Send me product release notes
</wc-duostrap-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 “命名、激活与文本选择” Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div id="label-modes" class="flex w-full max-w-md flex-col gap-4">
<div class="flex items-center gap-2">
<wc-duostrap-checkbox-root data-pair="copyable">
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root data-pair="copyable" selection="text">
Weekly digest (clickable and selectable)
</wc-duostrap-label-root>
</div>
<div class="flex items-center gap-2">
<wc-duostrap-checkbox-root data-pair="naming-only">
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root
data-pair="naming-only"
activation="false"
>
Release policy notice (naming only)
</wc-duostrap-label-root>
</div>
<div class="flex flex-col gap-2">
<wc-duostrap-label-root
data-pair="activation-only"
naming="false"
>
Quick filter (activation only)
</wc-duostrap-label-root>
<wc-duostrap-input-root
data-pair="activation-only"
aria-label="Filter workspace items"
placeholder="Filter workspace items..."
></wc-duostrap-input-root>
</div>
<div class="flex items-center gap-2">
<wc-duostrap-checkbox-root data-pair="unavailable" disabled>
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root data-pair="unavailable">
Unavailable option
</wc-duostrap-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)” Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<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-duostrap-checkbox-root data-pair="rtl-terms">
<wc-duostrap-checkbox-indicator></wc-duostrap-checkbox-indicator>
</wc-duostrap-checkbox-root>
<wc-duostrap-label-root data-pair="rtl-terms">
قبول الشروط والأحكام
</wc-duostrap-label-root>
</div>
<div class="flex flex-col gap-2">
<wc-duostrap-label-root data-pair="rtl-email">
البريد الإلكتروني
</wc-duostrap-label-root>
<wc-duostrap-input-root
data-pair="rtl-email"
placeholder="name@example.com"
></wc-duostrap-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' | 文本选择策略;默认仅在可激活状态下抑制误选 |
关联辅助接口
Section titled “关联辅助接口”createControlLabelRef():从@proto.ui/core导入,创建一对一控件关联引用。instanceAssociations:React / Vue / Vue 2 传入{ controlLabel: ref }的适配器关联属性。setElementAssociations(element, { controlLabel: ref }):从@proto.ui/adapter-web-component导入,为 Web Components 绑定引用。