# Label

A label in the hand, that dims with the control it names.

Docs: https://ballpoint.st9wd.com/docs/label

```bash
npx shadcn@latest add @ballpoint/label
```

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

<Label htmlFor="email">Email</Label>
```

Props:

- `htmlFor`: `string`. The id of the control it names. Or wrap the control instead: the label lays it out beside the text.
- `className`: `string`. A plain <label>: every prop goes to it.
- `required`: `boolean` (default `false`). Marks the label with a red-pen asterisk. The control still needs its own required.
- `optional`: `boolean` (default `false`). Writes (optional) after the label, in lighter ink.

## Example

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

export default function LabelDemo() {
  return (
    <Label>
      <Checkbox seed="label-terms" />
      Accept the terms
    </Label>
  );
}
```

## With controls

Wrap a switch, radio or checkbox so the whole row is clickable; mark required fields; disabled controls dim their label.

```tsx
import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { Switch } from "@/components/ui/switch";

export default function LabelControls() {
  return (
    <div className="grid w-full max-w-md gap-7">
      <div className="grid gap-2">
        <Label htmlFor="lc-name">
          Name <span className="text-destructive">*</span>
        </Label>
        <Input id="lc-name" required placeholder="Required" seed="lc-name" />
      </div>
      <Label>
        <Switch defaultChecked seed="lc-switch" />
        Wrap the label around the control
      </Label>
      <RadioGroup defaultValue="a" aria-label="Choice" className="flex flex-wrap gap-6">
        <Label>
          <RadioGroupItem value="a" seed="lc-a" />
          Option A
        </Label>
        <Label>
          <RadioGroupItem value="b" seed="lc-b" />
          Option B
        </Label>
      </RadioGroup>
      <Label>
        <Checkbox disabled seed="lc-disabled" />
        Dims with its disabled control
      </Label>
    </div>
  );
}
```

## Required and optional

A red-pen asterisk for required fields, a quiet note for optional ones.

```tsx
"use client";

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

export default function LabelRequired() {
  return (
    <form className="grid w-full max-w-sm gap-5" onSubmit={(event) => event.preventDefault()}>
      <div className="grid gap-2">
        <Label htmlFor="lr-name" required>
          Name
        </Label>
        <Input id="lr-name" required placeholder="Ada Lovelace" seed="lr-name" />
      </div>
      <div className="grid gap-2">
        <Label htmlFor="lr-email" required>
          Email
        </Label>
        <Input id="lr-email" type="email" required placeholder="ada@example.com" seed="lr-email" />
      </div>
      <div className="grid gap-2">
        <Label htmlFor="lr-note" optional>
          Anything we should know
        </Label>
        <Input id="lr-note" placeholder="Left-handed, say" seed="lr-note" />
      </div>
    </form>
  );
}
```
