# Label

The name of a form control, shown above or beside it.

The name of a form control, shown above or beside it. Its type is text-sm, medium, ink (the label of Input's field wrapper). Ark UI's Field.Label: inside a Field it is wired to the control (for / id) and fades with a disabled control; on its own, point it at a control with htmlFor. It wraps rather than being cut.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `field` machine, through Ark UI

## Add

```bash
npx fndn add label
```

It writes the folder `label/` into the project's ui folder (`index.ts`, `label.tsx`, `Label.vue`, `Label.svelte`, `label.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { Label } from "@/components/ui/label";
```

```vue
import { Label } from "@/components/ui/label";
```

```svelte
import { Label } from "$lib/components/ui/label";
```

## Parts

In the DOM the root carries `data-slot="label"` and each part `data-slot="label-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `label` | `label` |  | A <label>: text.sm, medium, ink. Author text keeps its own direction and wraps (an unbreakable name breaks anywhere); never truncated. Inside a disabled Field it is at half opacity (data-disabled from Ark). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `htmlFor` | `string` |  | id of the control it names. Set by the Field when the label is inside one. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Slots

| Slot | Content |
| --- | --- |
| `default` | The label text (and, inside a Field, its required indicator). |

## States

`disabled`

## Keyboard and accessibility

Notes:

- A <label> names its control: a click on it focuses (or toggles) the control, and assistive technology reads it as the control's name.
- Not focusable itself.

## Motion

- none

## Tokens

`color.ink`, `text.sm`

## Examples

### Email address

React · `email-address.tsx`

```tsx
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

/* on its own, a label points at its control with htmlFor (inside a Field this is done for you) */
export default function EmailAddress() {
  return (
    <div className="flex w-full max-w-80 flex-col gap-1.5">
      <Label htmlFor="signup-email">Email address</Label>
      <Input id="signup-email" type="email" autoComplete="email" placeholder="maya@northwind.example" />
    </div>
  );
}
```

Vue · `email-address.vue`

```vue
<script setup lang="ts">
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
</script>

<!-- on its own, a label points at its control with for (inside a Field this is done for you) -->
<template>
  <div class="flex w-full max-w-80 flex-col gap-1.5">
    <Label for="signup-email">Email address</Label>
    <Input id="signup-email" type="email" autocomplete="email" placeholder="maya@northwind.example" />
  </div>
</template>
```

Svelte · `email-address.svelte`

```svelte
<script lang="ts">
  import { Input } from "$lib/components/ui/input";
  import { Label } from "$lib/components/ui/label";
</script>

<!-- on its own, a label points at its control with for (inside a Field this is done for you) -->
<div class="flex w-full max-w-80 flex-col gap-1.5">
  <Label for="signup-email">Email address</Label>
  <Input id="signup-email" type="email" autocomplete="email" placeholder="maya@northwind.example" />
</div>
```

### Setting beside a switch

React · `beside-a-switch.tsx`

```tsx
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";

/* a settings row: the label names the switch (a click on it toggles), the description sits under it */
export default function BesideASwitch() {
  return (
    <div className="flex w-full max-w-80 items-start justify-between gap-4">
      <div className="flex min-w-0 flex-col gap-0.5">
        <Label htmlFor="preview-deploys">Preview deploys</Label>
        <p id="preview-deploys-description" className="m-0 text-sm text-ink-2">Build every pull request on its own URL.</p>
      </div>
      <Switch id="preview-deploys" aria-describedby="preview-deploys-description" defaultChecked />
    </div>
  );
}
```

Vue · `beside-a-switch.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";

const previews = ref(true);
</script>

<!-- a settings row: the label names the switch (a click on it toggles), the description sits under it -->
<template>
  <div class="flex w-full max-w-80 items-start justify-between gap-4">
    <div class="flex min-w-0 flex-col gap-0.5">
      <Label for="preview-deploys">Preview deploys</Label>
      <p id="preview-deploys-description" class="m-0 text-sm text-ink-2">Build every pull request on its own URL.</p>
    </div>
    <Switch id="preview-deploys" v-model:checked="previews" aria-describedby="preview-deploys-description" />
  </div>
</template>
```

Svelte · `beside-a-switch.svelte`

```svelte
<script lang="ts">
  import { Label } from "$lib/components/ui/label";
  import { Switch } from "$lib/components/ui/switch";

  let previews = $state(true);
</script>

<!-- a settings row: the label names the switch (a click on it toggles), the description sits under it -->
<div class="flex w-full max-w-80 items-start justify-between gap-4">
  <div class="flex min-w-0 flex-col gap-0.5">
    <Label for="preview-deploys">Preview deploys</Label>
    <p id="preview-deploys-description" class="m-0 text-sm text-ink-2">Build every pull request on its own URL.</p>
  </div>
  <Switch id="preview-deploys" bind:checked={previews} aria-describedby="preview-deploys-description" />
</div>
```

### Optional field

React · `optional-field.tsx`

```tsx
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

/* when most fields are required, mark the few optional ones instead of every required one */
export default function OptionalField() {
  return (
    <div className="flex w-full max-w-80 flex-col gap-1.5">
      <Label htmlFor="vat-number">
        VAT number <span className="font-normal text-ink-2">(optional)</span>
      </Label>
      <Input id="vat-number" mono placeholder="DE123456789" />
    </div>
  );
}
```

Vue · `optional-field.vue`

```vue
<script setup lang="ts">
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
</script>

<!-- when most fields are required, mark the few optional ones instead of every required one -->
<template>
  <div class="flex w-full max-w-80 flex-col gap-1.5">
    <Label for="vat-number">
      VAT number <span class="font-normal text-ink-2">(optional)</span>
    </Label>
    <Input id="vat-number" mono placeholder="DE123456789" />
  </div>
</template>
```

Svelte · `optional-field.svelte`

```svelte
<script lang="ts">
  import { Input } from "$lib/components/ui/input";
  import { Label } from "$lib/components/ui/label";
</script>

<!-- when most fields are required, mark the few optional ones instead of every required one -->
<div class="flex w-full max-w-80 flex-col gap-1.5">
  <Label for="vat-number">
    VAT number <span class="font-normal text-ink-2">(optional)</span>
  </Label>
  <Input id="vat-number" mono placeholder="DE123456789" />
</div>
```

## Recipe

`label.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* label — the name of a form control: the label of Input's former field wrapper (text-sm,
 * font-medium, ink), as a component of its own. Ark UI's Field.Label: inside a Field it is wired
 * to the control (for / id) and carries data-disabled / data-invalid / data-required; on its own
 * it is a plain <label> (give it htmlFor).
 *
 * Author text keeps its own direction and wraps rather than being cut (a name is never shortened);
 * an unbreakable one breaks anywhere. A disabled field's label fades with its control. */
export const label = tv({
  base: [
    "text-sm font-medium text-ink wrap-anywhere [unicode-bidi:plaintext] [text-align:match-parent]",
    "data-disabled:opacity-50",
  ],
});

export type LabelRecipeProps = VariantProps<typeof label>;
```
