Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Label for React Aria

A form label for React on React Aria that names an input, a checkbox or a switch, and focuses or toggles it when its text is clicked.

On this page9 sections

Label is a <label> for one form control. Its text becomes the name a screen reader reads for the control, and clicking it focuses an input or toggles a checkbox. Link it with htmlFor set to the control's id, or put the control inside it.

Use it on its own for a checkbox row or a quick one-off field. When the field also needs help text or an error message, build it with Field, whose FieldLabel sits in the same layout as both.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Label anatomy: label, control, text

Usage

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

The label's htmlFor must match the input's id, and each id must be unique on the page.

<div className="grid max-w-xs gap-2">
  <Label htmlFor="email">Email</Label>
  <Input id="email" type="email" />
</div>

Accessibility

Label renders a native <label>, so the browser does the work: its text names the control for a screen reader, and clicking it focuses or toggles the control. That happens only for a native form element it is linked to. React Aria's Checkbox and Switch take the id you pass onto the element a label can point at, so htmlFor works with them. For a custom control that isn't a form element, set aria-labelledby to the label's id.

Demos

RTL

The terms checkbox row, in Arabic, with dir set to rtl on the row. The row is a flex container that runs in the reading direction, so the checkbox moves to the right and the label follows it. Clicking the Arabic text toggles the checkbox, through the label's htmlFor.

Preview width

FAQ

How do I associate a Label with an input in React?

Give the input an id and set the Label's htmlFor to the same value, as the Usage example does. Clicking the label then focuses the input, and a screen reader reads the label's text as the input's name. The id has to be unique on the page, or the label points at the first element that has it.

Inside a React Aria component that has a label slot of its own, such as a Select, leave htmlFor off. Label then takes its id from that component, which points its own aria-labelledby at the label.

How do I label a Checkbox or a Switch with Label?

Give the Checkbox an id and point the Label's htmlFor at it, as the preview above does. React Aria's Checkbox and Switch pass the id to the hidden <input> inside them, so the label names that input and clicking it toggles the control.

Can a Label wrap its control instead of using an id?

Yes, for an Input, which a label that contains it links to without any ids. Don't wrap a Checkbox or a Switch, though. React Aria renders their root as a <label> already, and a label inside another label isn't valid HTML, so give them an id and point htmlFor at it.

How do I use a Label for a custom control that isn't an input?

Give the Label an id and set aria-labelledby on the control to the same value. A <label> only links to a native form element such as an <input>, a <textarea>, a <select> or a <button>. A div with a role, such as a custom slider or a color swatch, gets no name from it, and clicking the label does nothing.

To make the click work too, focus the control from the label's click handler. Inside a React Aria component with a label slot, such as a Select, Label does all of this for you when you leave htmlFor off.

Why doesn't my Label dim when its control is disabled?

It dims only when the control comes before it in the same parent, has the peer class and carries data-disabled, as React Aria's Checkbox and Switch do when placed first. A label above an Input comes before the input, so the peer styles can't reach it from there.

What works with every control is a wrapper: give the row's container the group class and data-disabled set to true, and the Label fades and stops taking clicks.

How do I mark a Label as required or optional?

Put required on the control, which is what makes the browser enforce it and a screen reader announce it. Then show it in the label, such as an asterisk in a <span> with aria-hidden, so the star isn't read as part of the name. On a form where most fields are required, mark the few optional ones with the word Optional instead.

Say what the asterisk means once, near the top of the form, for people who don't know the convention.

When should I use Field instead of a Label?

Use Label for a caption and a control with nothing else around them, such as a checkbox row or a search box beside its label. Use Field once the field also has a description or an error message, or sits among other fields in a form.

FieldLabel is a label in that layout, and Field adds the spacing, the disabled and invalid styles, and a place for a FieldError. Mixing the two works, but a form reads more evenly built from one.

API Reference

Label

PropType
elementType?string

The element it renders, such as span.

render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.