Label
用标签为任何控件提供名称,让按压标签能到达它。
普通的 <label> 只对输入框、按钮和 select 元素生效。ui-label 通过 for 为任何控件提供名称,包括单选组或滑块,按压标签会把焦点移入控件。
import "@ui-sdk/web/label"
import "@ui-sdk/web/radio"
const speeds = [
{ value: "standard", label: "Standard" },
{ value: "express", label: "Express" },
{ value: "overnight", label: "Overnight" },
]
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<ui-label for="shipping-speed">Shipping speed</ui-label>
<ui-radio-group id="shipping-speed" default-value="standard">
${speeds
.map(
(speed) => `
<ui-label>
<ui-radio value="${speed.value}">
<ui-radio-indicator></ui-radio-indicator>
</ui-radio>
${speed.label}
</ui-label>
`
)
.join("")}
</ui-radio-group>
</div>
`
}
export { mount }安装
npm install @ui-sdk/web @ui-sdk/core导入这个组件会定义它的元素。在服务器上,导入什么也不做。
import "@ui-sdk/web/label"用法
导入标签,把它的 for 指向控件的 id:
<script type="module">
import "@ui-sdk/web/label"
import "@ui-sdk/web/radio"
</script>
<ui-label for="plan">Plan</ui-label>
<ui-radio-group id="plan" default-value="pro">
<ui-radio value="free">Free</ui-radio>
<ui-radio value="pro">Pro</ui-radio>
</ui-radio-group>控件会把 ui-label 作为自己的名称,按压标签会像原生标签那样聚焦它、勾选它或点击它。
在 Field 里,用 ui-field-label,它还会把字段的描述和错误连接到控件。
自己构建
@ui-sdk/web/label 自带现成的 ui-label 元素。如果你想用自己起的元素名构建一个,从 @ui-sdk/core 里的 label 原语(primitive)开始:
import { label } from "@ui-sdk/core/label"
import { createParts } from "@ui-sdk/web"
createParts(label, {
root: { tag: "app-caption" },
})<app-caption for="plan">Plan</app-caption>app-caption 保留现成 ui-label 的行为:它为控件提供名称,按压时能到达它。更多内容见渲染原语和自定义部件。
滑块
滑块的标签为它的滑块手柄提供名称,所以滑块手柄会被读作 Volume,按压标签会聚焦滑块手柄。
import "@ui-sdk/web/label"
import "@ui-sdk/web/slider"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-slider id="volume" default-value="75">
<div data-slot="row">
<ui-label for="volume">Volume</ui-label>
<ui-slider-value></ui-slider-value>
</div>
<ui-slider-control>
<ui-slider-track>
<ui-slider-indicator></ui-slider-indicator>
<ui-slider-thumb></ui-slider-thumb>
</ui-slider-track>
</ui-slider-control>
</ui-slider>
`
}
export { mount }开关
把标签指向旁边的开关。按压标签会打开和关闭开关。
import "@ui-sdk/web/label"
import "@ui-sdk/web/switch"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="row">
<ui-switch id="updates">
<ui-switch-thumb></ui-switch-thumb>
</ui-switch>
<ui-label for="updates">Email me product updates</ui-label>
</div>
`
}
export { mount }包住控件
把控件放进标签里,而不是指向它,这样就不需要 id。
import "@ui-sdk/web/checkbox"
import "@ui-sdk/web/label"
import { Check, createElement } from "lucide"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-label>
<ui-checkbox>
<ui-checkbox-indicator>
${createElement(Check, { "aria-hidden": "true" }).outerHTML}
</ui-checkbox-indicator>
</ui-checkbox>
Accept terms and conditions
</ui-label>
`
}
export { mount }样式
标签没有样式。它的 data-slot 是 label。
双击标签不会选中它的文字,所以不需要为此写 user-select: none。
API 参考
属性与下面相同,另外还有 for,它指向控件的 id。
Label
debug?boolean- Logs every state change with its reason to the console, in development builds.
无障碍
ui-label 通过 aria-labelledby 为它的控件提供名称,除非控件自带 aria-label 或 aria-labelledby,以控件自己的为准。它被按压时做的事和原生标签一样:勾选复选框、开关或单选按钮,聚焦分组里已选中的单选按钮,其余控件则聚焦它本身。按压禁用的控件什么也不做。
经过测试的行为
下面每一行都是一个自动化测试。
- A label names a native input, and a press focuses it.
- A label names a radio group, and a press focuses its checked radio.
- A label for a slider names its thumb, and a press focuses it.
- A label around a checkbox names it, and a press toggles it.
- A press on the label of a disabled group moves no focus.
- A control its author names keeps that name.
- A label names a control that renders after it.
- A double click on a label doesn't select its text.
运行于 React、Vue 和 web components。