Label
A label that follows its control, dimming when it's disabled and marking it required or optional on its own.
pnpm dlx shadcn@latest add https://hextaui.com/r/label.jsonAdds the component, the HextaUI theme tokens and any HextaUI components it depends on.
Add the theme tokens to your global CSS, if you haven’t yet.
Install the dependencies.
pnpm add @base-ui/react cnCopy and paste the following code into your project.
components/ui/label.tsx Update the import paths to match your project setup.
Point htmlFor at a control's id, or wrap the control. Either way the label finds it and follows its state. Inside a <Field />, use <FieldLabel />, which wires up the id for you.
Follows its control
The label dims and shows a not-allowed cursor while its control is disabled, and exposes data-required, data-invalid and data-readonly for your own styles. It keeps up when the control changes.
Required and optional
indicator="optional" tags controls without required, and indicator="required" adds an asterisk to controls with it. The mark is hidden from screen readers, which already announce required fields.
Checkbox
Wrap a checkbox so the whole label toggles it, or place the label beside it with htmlFor. A disabled checkbox dims its label either way.
Icon
Icons inside a label are sized and muted to sit next to the text.
Long content
Long labels wrap, and unbroken strings break instead of widening the layout.
Right to left
Gaps and the indicator follow the reading direction.
- Clicking the label focuses or toggles its control, so it's a bigger target than the control alone.
- Double-clicking the label text doesn't select it. Double-clicks on a control inside the label work as usual.
- Every control needs a name. When there's no visible label, use
aria-labelon the control instead.
Renders a <label> and accepts its attributes.
| Prop | Type | Default |
|---|---|---|
htmlForThe id of the control. Leave it out when the label wraps the control. | string | – |
indicatorMark the label from the control's required state. Off by default. | "required" | "optional" | – |
optionalText | ReactNode | "Optional" |
render | ReactElement | (props, state) => ReactElement | <label> |
| Attribute | Description |
|---|---|
data-slot="label" | Target labels in CSS. |
data-disabled | Present while the control is disabled. |
data-required | Present while the control is required. |
data-invalid | Present while the control is invalid, after the user has interacted or when aria-invalid is set. |
data-readonly | Present while the control is read-only. |
data-slot="label-indicator" | The required or optional mark. |