Label
The caption of one control. It is the native label, so a press on it moves the focus to the control and screen readers read it as the control's name. Inside a field use FieldLabel; over a whole group use the fieldset's legend.
Install
npx shadcn@latest add https://thrumui.dev/r/label.jsonAdds components/ui/label.tsx. Nothing else in your project changes.
import { Label } from "@/components/ui/label";Props
| Prop | Type | Default | Description |
|---|---|---|---|
htmlFor | string | – | The id of the control this label names. Leave it out when the label wraps the control. |
required | boolean | Default false | Adds an asterisk after the text and the hidden word required. The control still needs its own required attribute. |
className | string | – | Extra classes, merged last. |
...props | ComponentProps<"label"> | – | Every other label attribute and event handler is passed through. |
Motion and sound
Motion
- The label does not move.
Sound
Silent. This component makes no sound.
Accessibility
- It is a native label element. Linked with htmlFor, or wrapped around the control, it gives the control its name, and a press on it focuses or toggles the control.
- The asterisk is hidden from screen readers. They hear the word required after the label's text instead.
- A disabled label is dimmed to 50%. It dims when the control placed before it has the peer class and is disabled, or when an ancestor with the group class carries data-disabled.
- The text is 14 pixels, medium weight, in the foreground colour: 4.5 to 1 against the page in both themes.
- A long label wraps. Nothing is cut off at 160 pixels wide.
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
export type LabelProps = ComponentProps<"label"> & {
/** Adds an asterisk after the text, and the word "required" for screen readers. The control still needs its own `required` attribute. */
required?: boolean;
};
/**
* The caption of a control. It is a native `<label>`: link it with `htmlFor` and the control's `id`, or wrap the control in it.
* It dims when the control before it is a disabled `peer`, or when an ancestor with the `group` class carries `data-disabled`.
*/
export function Label({ required = false, className, children, ...props }: LabelProps) {
return (
<label
data-slot="label"
className={cn(
"text-sm leading-5 font-medium text-foreground [overflow-wrap:anywhere]",
"peer-disabled:cursor-not-allowed peer-disabled:opacity-50 group-data-disabled:cursor-not-allowed group-data-disabled:opacity-50",
className,
)}
{...props}
>
{children}
{required && (
<>
{/* The asterisk is for the eye and the word is for the ear: a screen reader hears "required", never "star". */}
<span aria-hidden="true" className="ms-0.5 text-destructive">
*
</span>
<span className="sr-only"> required</span>
</>
)}
</label>
);
}Files and dependencies
- Adds
components/ui/label.tsx- Packages
- None
- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.