# Label

> Label associates text with a form control. Wrap an Input or Checkbox to make the label clickable, or use htmlFor when the control sits outside.

Source: https://vaneui.com/docs/form-components/label

Import: `import { Label, Card, Checkbox, Code, Col, Field, Input, Row, Text } from "@vaneui/ui"`

Label gives a form control its visible name and renders a native `<label>` element. It links no help text or error message to the control; [Field](/docs/form-components/field) adds both. Text that names no control, such as a hint under a heading, belongs in [Text](/docs/typography-components/text).

## Basic usage

A `Label` defaults to `sm` size and `inheritAppearance` appearance, so it picks up text color from its surroundings.

```tsx demo
<Label>Email address</Label>
```

## Sizes

Labels come in five sizes: `xs`, `sm` (default), `md`, `lg`, `xl`. Nested `Input` and `Checkbox` controls inherit the size.

```tsx demo
<Col>
  <Label xs>xs label <Input placeholder="xs input" /></Label>
  <Label>sm label (default) <Input placeholder="sm input" /></Label>
  <Label md>md label <Input placeholder="md input" /></Label>
  <Label lg>lg label <Input placeholder="lg input" /></Label>
  <Label xl>xl label <Input placeholder="xl input" /></Label>
</Col>
```

## Appearances

Labels default to `inheritAppearance`: they take their color from the surrounding text. Set an explicit appearance to override.

```tsx demo
<Row flexWrap>
  <Label>inheritAppearance label (default)</Label>
  <Label primary>primary label</Label>
  <Label accent>accent label</Label>
  <Label secondary>secondary label</Label>
  <Label tertiary>tertiary label</Label>
  <Label success>success label</Label>
  <Label danger>danger label</Label>
  <Label warning>warning label</Label>
  <Label info>info label</Label>
</Row>
```

## Variants

Label never paints a surface, so the variant only picks which colour of the appearance the text is painted in. `outline`, the default, uses the appearance's own colour. `filled` uses the "on this fill" colour, for a label sitting on a filled surface of the same appearance. For a filled pill beside a field, reach for `Badge` or `Chip` instead.

```tsx demo
<Col>
  <Row flexWrap>
    <Label success>outline success</Label>
    <Label danger>outline danger</Label>
  </Row>
  <Card success filled wFit>
    <Label success filled>filled success, on a matching filled Card</Label>
  </Card>
</Col>
```

## Disabled

`disabled` dims the label, and a Label containing a disabled control dims with it, so you only have to disable the field itself.

```tsx demo
<Col>
  <Label disabled>Explicitly disabled label</Label>
  <Label>
    Email
    <Input disabled placeholder="you@example.com" />
  </Label>
  <Label row itemsCenter>
    <Checkbox disabled />
    Subscribe to product updates
  </Label>
</Col>
```

The dimming applies once: a disabled control inside a dimmed Label is not faded a second time.

## With Input

Wrap an `Input` inside a `Label` to make the label clickable and associate them implicitly. No `htmlFor`/`id` wiring required.

```tsx demo
<Col noGap>
  <Label>
    Email
    <Input placeholder="you@example.com" />
  </Label>
</Col>
```

## With Checkbox

Place a `Checkbox` inside a `Label` so the label text expands the toggle's hit area. `Label` defaults to a stacked column layout, so add `row itemsCenter` to sit the checkbox inline with its text.

```tsx demo
<Label row itemsCenter>
  <Checkbox />
  Subscribe to product updates
</Label>
```

## htmlFor association

When the control lives outside the label, use the standard `htmlFor` attribute pointing at the input's `id`.

```tsx demo
<Col noGap>
  <Label htmlFor="username">Username</Label>
  <Input id="username" placeholder="Choose a username" />
</Col>
```

## Font weights

The default weight is `fontMedium`. Override with any weight prop from `fontThin` to `fontBlack`.

```tsx demo
<Col>
  <Label fontThin>thin weight</Label>
  <Label fontExtralight>extralight weight</Label>
  <Label fontLight>light weight</Label>
  <Label fontNormal>normal weight</Label>
  <Label>medium weight (default)</Label>
  <Label fontSemibold>semibold weight</Label>
  <Label fontBold>bold weight</Label>
  <Label fontExtrabold>extrabold weight</Label>
  <Label fontBlack>black weight</Label>
</Col>
```

## Text decorations

Apply text decorations as needed.

```tsx demo
<Row flexWrap>
  <Label underline>underline text</Label>
  <Label lineThrough>lineThrough text</Label>
  <Label overline>overline text</Label>
</Row>
```

## Size propagation

When an `Input` or `Checkbox` is nested inside a `Label`, it automatically picks up the label's size. Set it once and everything scales together. Pass a size on the child to opt out for that one element.

```tsx demo
<Col>
  <Label xl row itemsCenter>
    <Checkbox />
    Extra-large checkbox option
  </Label>
  <Label lg row itemsCenter>
    <Checkbox xs />
    Explicit <Code>xs</Code> checkbox overrides the <Code>lg</Code> label
  </Label>
</Col>
```

## Required and status

Use appearances to indicate field status: `danger` for errors, `success` for valid, `secondary` for hints.

```tsx demo
<Col>
  <Field label={<>Username <Text tag="span" danger inheritSize>*</Text></>}>
    <Input placeholder="Choose a username" required />
  </Field>
  <Label success fontSemibold>
    Email verified
    <Input placeholder="verified@example.com" />
  </Label>
  <Text sm secondary>All fields marked with * are required.</Text>
</Col>
```

## Frequently asked questions

### Which controls take their size from a Label?

`Input`, `Textarea`, `Select`, `Checkbox`, `Radio` and `Switch` inside a Label take the Label's size, which is `sm` unless you set another. A size prop on the control still wins. Other children, such as a `Text` hint or a `Button`, keep their own sizes. A [Field](/docs/form-components/field#sizes) passes its size to the control inside it in the same way.

### How do I label a group of checkboxes?

A Label names one control, so a set of checkboxes needs a group name of its own. Wrap the set in a native `<fieldset>` with a `<legend>`, or in a `Col` with `role="group"` and `aria-labelledby` set to the id of the text that names the group. For one choice out of several, use a `RadioGroup` inside a [Field](/docs/form-components/field#radio-groups), which labels the group for you.

### Why does clicking a Label not focus my custom control?

A `<label>` passes its clicks and its text only to labelable native elements, such as `<input>`, `<select>`, `<textarea>` and `<button>`. A control built from a `div` with a role gets neither. Give that control `aria-labelledby` set to the Label's `id` for its name, and focus the control from the Label's `onClick` if a click on the text should reach it.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance |  | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance |  | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance | yes | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `disabled` | Disabled |  | Disable the component — reduces opacity, changes cursor to not-allowed, and prevents interaction |
| `flex1` | Flex |  | Take up remaining space (= `flex-1`, i.e. `flex: 1 1 0%`) |
| `flexAuto` | Flex |  | Grow but respect intrinsic size (= `flex-auto`, i.e. `flex: 1 1 auto`) |
| `flexNone` | Flex |  | Don't grow and don't shrink (= `flex-none`, i.e. `flex: none`) |
| `fontSans` | Font Family | yes | Sans-serif font family — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family |  | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `italic` | Font Style |  | Italic font style |
| `notItalic` | Font Style |  | Not italic (normal) font style |
| `fontThin` | Font Weight |  | Thin font weight (100) — emits font-thin |
| `fontExtralight` | Font Weight |  | Extra light font weight (200) — emits font-extralight |
| `fontLight` | Font Weight |  | Light font weight (300) — emits font-light |
| `fontNormal` | Font Weight |  | Normal font weight (400) — emits font-normal |
| `fontMedium` | Font Weight | yes | Medium font weight (500) — emits font-medium |
| `fontSemibold` | Font Weight |  | Semibold font weight (600) — emits font-semibold |
| `fontBold` | Font Weight |  | Bold font weight (700) — emits font-bold |
| `fontExtrabold` | Font Weight |  | Extra bold font weight (800) — emits font-extrabold |
| `fontBlack` | Font Weight |  | Black font weight (900) — emits font-black |
| `noShrink` | Shrink |  | Prevent the flex item from shrinking below its content size (= `shrink-0`) |
| `xs` | Size |  | Extra small size |
| `sm` | Size | yes | Small size |
| `md` | Size |  | Medium size |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |
| `srOnly` | Sr Only |  | Hide visually and keep it for screen readers (sr-only). On a Label that wraps its control, only the label text is hidden |
| `textLeft` | Text Align |  | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `underline` | Text Decoration |  | Add underline decoration below text |
| `lineThrough` | Text Decoration |  | Add strikethrough/line-through decoration across text |
| `noUnderline` | Text Decoration |  | Remove ALL text-decoration — underline, line-through, and overline (text-decoration: none) |
| `overline` | Text Decoration |  | Add overline decoration above text |
| `uppercase` | Text Transform |  | Transform text to uppercase |
| `lowercase` | Text Transform |  | Transform text to lowercase |
| `capitalize` | Text Transform |  | Capitalize first letter of each word |
| `normalCase` | Text Transform |  | Normal text case (no transformation) |
| `truncate` | Truncate |  | Single line truncation with ellipsis |
| `lineClamp2` | Truncate |  | Truncate at 2 lines with ellipsis |
| `lineClamp3` | Truncate |  | Truncate at 3 lines with ellipsis |
| `lineClamp4` | Truncate |  | Truncate at 4 lines with ellipsis |
| `lineClamp5` | Truncate |  | Truncate at 5 lines with ellipsis |
| `noTruncate` | Truncate |  | Remove truncation |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |

Layout and utility props (Border, Cursor, Display, Flex Direction, Gap, Height, Hide, Items, Justify, Overflow, Position, Responsive Sizing, Responsive Width, Ring, Shadow, Show, Width, Wrap) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
