# Label

Name any control with a label, and let a press on the label reach it.

A plain `<label>` only reaches inputs, buttons and selects. Label names any control by its `for`, including a radio group or a slider, and a press on the label moves focus into it.

```angular-ts title="basic.ts"
import { Component } from "@angular/core"
import { Label } from "@ui-sdk/angular/label"
import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/angular/radio"

@Component({
  imports: [Label, Radio, RadioGroup, RadioIndicator],
  template: `
    <div data-slot="stack">
      <label uiLabel for="shipping-speed">Shipping speed</label>
      <div uiRadioGroup id="shipping-speed" defaultValue="standard">
        @for (speed of speeds; track speed.value) {
          <label uiLabel>
            <button uiRadio [value]="speed.value">
              <span uiRadioIndicator></span>
            </button>
            {{ speed.label }}
          </label>
        }
      </div>
    </div>
  `,
})
class Example {
  readonly speeds = [
    { value: "standard", label: "Standard" },
    { value: "express", label: "Express" },
    { value: "overnight", label: "Overnight" },
  ]
}

export { Example }
```

Press Shipping speed. Focus moves to the checked option, and a screen reader names the group Shipping speed.

## Installation

```bash
npm install @ui-sdk/angular @ui-sdk/core
```

## Usage

Import the label and point its `for` at the control's `id`:

```ts
import { Component } from "@angular/core"
import { Label } from "@ui-sdk/angular/label"
import { Radio, RadioGroup } from "@ui-sdk/angular/radio"

@Component({
  selector: "app-example",
  imports: [Label, Radio, RadioGroup],
  template: `
    <label uiLabel for="plan">Plan</label>
    <div uiRadioGroup id="plan" defaultValue="pro">
      <button uiRadio value="free">Free</button>
      <button uiRadio value="pro">Pro</button>
    </div>
  `,
})
class Example {}

export { Example }
```

Label renders a native `<label>`. When the browser can reach the control on its own, like an input, it does. For anything else, the control takes the label as its name, and a press on the label focuses it, checks it or clicks it, as a native label would.

Inside a [Field](/docs/angular/components/field), use `uiFieldLabel`, which also ties the field's description and errors to the control.

## Build Your Own

`@ui-sdk/angular/label` comes with a ready-made Label. If you'd like to build your own, with your own name, start from the `label` primitive in `@ui-sdk/core`:

```ts title="caption.ts"
import { Directive } from "@angular/core"
import { label } from "@ui-sdk/core/label"
import { usePart, usePrimitive } from "@ui-sdk/angular/primitive"

@Directive({ selector: "[appCaption]" })
class Caption {
  readonly handle = usePrimitive(label, () => ({}))
  readonly state = usePart(this.handle, "root").state
}

export { Caption }
```

```ts
import { Component } from "@angular/core"
import { Caption } from "./caption"

@Component({
  selector: "app-caption-example",
  imports: [Caption],
  template: ` <label appCaption for="plan">Plan</label> `,
})
export class CaptionExample {}
```

`Caption` keeps the behavior of the ready-made Label: it names the control and reaches it on a press. See [Rendering Primitives](/docs/angular/rendering) and [Custom Parts](/docs/angular/custom-parts) for more.

## Slider

A label for a slider names its thumb, so the thumb reads as **Volume**, and a press on the label focuses it.

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

@Component({
  imports: [
    Label,
    Slider,
    SliderControl,
    SliderIndicator,
    SliderThumb,
    SliderTrack,
    SliderValue,
  ],
  template: `
    <div uiSlider id="volume" [defaultValue]="75">
      <div data-slot="row">
        <label uiLabel for="volume">Volume</label>
        <output uiSliderValue></output>
      </div>
      <div uiSliderControl>
        <div uiSliderTrack>
          <div uiSliderIndicator></div>
          <div uiSliderThumb></div>
        </div>
      </div>
    </div>
  `,
})
class Example {}

export { Example }
```

Press Volume, then use the arrow keys. The thumb moves, and a screen reader reads it as Volume.

## Switch

Point the label at a switch beside it. A press on the label turns the switch on and off.

```angular-ts title="switch.ts"
import { Component } from "@angular/core"
import { Label } from "@ui-sdk/angular/label"
import { Switch, SwitchThumb } from "@ui-sdk/angular/switch"

@Component({
  imports: [Label, Switch, SwitchThumb],
  template: `
    <div data-slot="row">
      <button uiSwitch id="updates">
        <span uiSwitchThumb></span>
      </button>
      <label uiLabel for="updates">Email me product updates</label>
    </div>
  `,
})
class Example {}

export { Example }
```

Press Email me product updates. The switch turns on.

## Wrapping a Control

Put the control inside the label instead of pointing at it, and you need no `id`.

```angular-ts title="wrap.ts"
import { Component } from "@angular/core"
import { LucideCheck } from "@lucide/angular"
import { Checkbox, CheckboxIndicator } from "@ui-sdk/angular/checkbox"
import { Label } from "@ui-sdk/angular/label"

@Component({
  imports: [Checkbox, CheckboxIndicator, Label, LucideCheck],
  template: `
    <label uiLabel>
      <button uiCheckbox>
        <span uiCheckboxIndicator>
          <svg lucideCheck></svg>
        </span>
      </button>
      Accept terms and conditions
    </label>
  `,
})
class Example {}

export { Example }
```

Press Accept terms and conditions. The checkbox checks, and a double click doesn't select the text.

## Custom Rendering

Put `uiLabel` on a different element, such as a `<span>`. A label that isn't a `<label>` still names its control and reaches it on a press. See [Parts and Composition](/docs/angular/parts-and-composition) for more.

## Styling

The label is unstyled. It has a `data-slot` of `label`.

A double click on the label doesn't select its text, so you don't need `user-select: none` for that.

## API Reference

Each option below is an input on the directive, along with a native label's `for`.

### Label

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

## Accessibility

A native `<label>` names the element its `for` points at only when that element is labelable, like an input or a button. Label names any other control through `aria-labelledby`, unless the control has an `aria-label` or an `aria-labelledby` of its own, which win. Its press does what a native label's does: it checks a checkbox, a switch or a radio, focuses a group's checked radio, and focuses anything else. A press on a disabled control does nothing.

### Tested Behavior

Every line below is an automated test.

- 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 and web components.
