# Label

用标签为任何控件提供名称，让按压标签能到达它。

普通的 `<label>` 只对输入框、按钮和 select 元素生效。Label 通过 `for` 为任何控件提供名称，包括单选组或滑块，按压标签会把焦点移入控件。

```tsx title="basic.tsx"
import { Label } from "@ui-sdk/solid/label"
import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/solid/radio"
import { For } from "solid-js"

const speeds = [
  { value: "standard", label: "Standard" },
  { value: "express", label: "Express" },
  { value: "overnight", label: "Overnight" },
]

function Example() {
  return (
    <div data-slot="stack">
      <Label for="shipping-speed">Shipping speed</Label>
      <RadioGroup id="shipping-speed" defaultValue="standard">
        <For each={speeds}>
          {(speed) => (
            <Label>
              <Radio value={speed.value}>
                <RadioIndicator />
              </Radio>
              {speed.label}
            </Label>
          )}
        </For>
      </RadioGroup>
    </div>
  )
}

export { Example }
```

按压 Shipping speed。焦点移到已选中的选项，屏幕阅读器会把分组读作 Shipping speed。

## 安装
```bash
npm install @ui-sdk/solid @ui-sdk/core
```

## 用法
导入标签，把它的 `for` 指向控件的 `id`：

```tsx
import { Label } from "@ui-sdk/solid/label"
import { Radio, RadioGroup } from "@ui-sdk/solid/radio"

function Plan() {
  return (
    <>
      <Label for="plan">Plan</Label>
      <RadioGroup id="plan" defaultValue="pro">
        <Radio value="free">Free</Radio>
        <Radio value="pro">Pro</Radio>
      </RadioGroup>
    </>
  )
}

export { Plan }
```

Label 渲染一个原生 `<label>`。对于输入框这类浏览器自己能到达的控件，就由浏览器直接处理。对其他控件，控件会把标签作为自己的名称，按压标签会像原生标签那样聚焦它、勾选它或点击它。

在 [Field](/docs/solid/components/field) 里，用 `FieldLabel`，它还会把字段的描述和错误连接到控件。

## 自己构建
`@ui-sdk/solid/label` 自带一个现成的 Label。如果你想用自己的名字构建一个，从 `@ui-sdk/core` 里的 `label` 原语（primitive）开始：

```ts title="caption.ts"
import { label } from "@ui-sdk/core/label"
import { createParts } from "@ui-sdk/solid/primitive"

const { Root: Caption } = createParts(label, { root: "label" })

export { Caption }
```

```tsx
<Caption for="plan">Plan</Caption>
```

`Caption` 保留现成 Label 的行为：它为控件提供名称，按压时能到达它。更多内容见[渲染原语](/docs/solid/rendering)和[自定义部件](/docs/solid/custom-parts)。

## 滑块
滑块的标签为它的滑块手柄提供名称，所以滑块手柄会被读作 **Volume**，按压标签会聚焦滑块手柄。

```tsx title="slider.tsx"
import { Label } from "@ui-sdk/solid/label"
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from "@ui-sdk/solid/slider"

function Example() {
  return (
    <Slider id="volume" defaultValue={75}>
      <div data-slot="row">
        <Label for="volume">Volume</Label>
        <SliderValue />
      </div>
      <SliderControl>
        <SliderTrack>
          <SliderIndicator />
          <SliderThumb />
        </SliderTrack>
      </SliderControl>
    </Slider>
  )
}

export { Example }
```

按压 Volume，然后用方向键。滑块手柄会移动，屏幕阅读器会把它读作 Volume。

## 开关
把标签指向旁边的开关。按压标签会打开和关闭开关。

```tsx title="switch.tsx"
import { Label } from "@ui-sdk/solid/label"
import { Switch, SwitchThumb } from "@ui-sdk/solid/switch"

function Example() {
  return (
    <div data-slot="row">
      <Switch id="updates">
        <SwitchThumb />
      </Switch>
      <Label for="updates">Email me product updates</Label>
    </div>
  )
}

export { Example }
```

按压 Email me product updates。开关会打开。

## 包住控件
把控件放进标签里，而不是指向它，这样就不需要 `id`。

```tsx title="wrap.tsx"
import { Checkbox, CheckboxIndicator } from "@ui-sdk/solid/checkbox"
import { Label } from "@ui-sdk/solid/label"

function Example() {
  return (
    <Label>
      <Checkbox>
        <CheckboxIndicator>
          <svg
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <path d="M20 6 9 17l-5-5" />
          </svg>
        </CheckboxIndicator>
      </Checkbox>
      Accept terms and conditions
    </Label>
  )
}

export { Example }
```

按压 Accept terms and conditions。复选框会勾选，双击也不会选中文字。

## 自定义渲染
传入 `render` 来换用别的元素，比如 `render={(props) => <span {...props} />}`。不是 `<label>` 的标签仍然为它的控件提供名称，按压标签时仍然能到达它。更多内容见[部件与组合](/docs/solid/parts-and-composition)。

## 样式
标签没有样式。它的 `data-slot` 是 `label`。

双击标签不会选中它的文字，所以不需要为此写 `user-select: none`。

## API 参考
属性与下面相同，都是 Solid props，另外还有原生 `<label>` 的 `for`。

### Label
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.

## 无障碍
原生 `<label>` 只在 `for` 指向的元素可被标注（labelable）时才为它提供名称，比如输入框或按钮。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.

Runs in React、Vue 和 web components.
