Label
Names a form control in words, so that people and assistive technology know what it asks for.
Installation
pnpm dlx shadcn@latest add @silkware/labelTo copy the component by hand, install its dependencies:
pnpm add react-aria-components cnThen copy the source into components/ui/label.tsx:
"use client"
import * as React from "react"import { cn } from "cn"import { LabelContext, Label as LabelPrimitive, type LabelProps,} from "react-aria-components"
function Label({ className, htmlFor, slot, ...props }: LabelProps) { const label = ( <LabelPrimitive data-slot="label" className={cn( "flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50 peer-data-disabled:opacity-50", className )} {...props} htmlFor={htmlFor} slot={slot} /> )
if (htmlFor && slot === undefined) { return <LabelContext.Provider value={null}>{label}</LabelContext.Provider> }
return label}
export { Label }Usage
import { Label } from "@/components/ui/label"<Label htmlFor="site">Receiving site</Label><Input id="site" />Match htmlFor to the id of the control. When you pass htmlFor and no slot, Label ignores the label context of a React Aria parent and renders a plain <label for>, so a click on the text focuses or toggles that control.
Inside a form, use FieldLabel from Field. It is this label with the form layout classes.
Examples
With a checkbox
The label sits next to a checkbox. Clicking the text toggles the box.
Required
Add an asterisk to a required field, hidden from screen readers with aria-hidden. The control announces that it is required, so the label does not have to say it.
Disabled
A label dims when its control is disabled, as long as the control comes before the label in the same parent. This works with the checkbox, whose root carries the peer class and a data-disabled attribute.
Accessibility
| Key | Action |
|---|---|
| Tab | Does not stop on the label. Focus goes to the control it names. |
The browser provides:
- The programmatic link from the label to the control through
forandid, so that a screen reader announces the label when the control takes focus. - A larger click target: pressing the label text focuses a text field or toggles a checkbox.
You provide:
- One label for every control, with an
htmlForthat matches a realid. - Text that names the information asked for, not the action.
- An
aria-labeloraria-labelledbyinstead, when the design has no visible text. The visible label is always better.
Guidelines
When to use
- Next to every input, textarea, checkbox, switch, radio and select.
- As
FieldLabel, for every control inside aField.
When not to use
- To title a group of controls. Use
FieldLegendinside aFieldSet, see Field. - To describe the control or explain a rule. Use
FieldDescription. - As a heading or a caption. Use text that is not a label.
Content
- Use a short noun phrase in sentence case: "Delivery date", "Contact person".
- Do not end with a colon or a full stop.
- Do not repeat the page title or the button text.
- Put the extra detail in the description, not in the label.
Length
Name what is asked in two or three words.
Don’t write a sentence with an instruction and an exclamation mark.
API reference
Label
Renders a <label> through the React Aria Label. It accepts every React Aria Label prop and the props below.
| Prop | Type | Default | Description |
|---|---|---|---|
htmlFor? | string | None | The id of the control the label names. When set without a slot, the label is a plain native label and ignores React Aria label context. |
slot? | string | null | None | Joins the label to a slot of a React Aria parent. Leave it out for a label with htmlFor. |
elementType? | string | "label" | The HTML element to render. |
className? | string | None | Classes merged after the base classes. |