Label
A label that follows its control, dimming when it's disabled and marking it required or optional on its own.
- Form components
- 1 parts
- 7 examples
@base-ui/react
See it in a real product: Team
View blockpnpm 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.
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.
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.
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.
Icons inside a label are sized and muted to sit next to the text.
Long labels wrap, and unbroken strings break instead of widening the layout.
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. |
- CalendarA date grid for single, range and multiple selection, with sliding months, range previews and touch-sized days.
- CheckboxA checkbox whose check draws in, with indeterminate parents, groups and labels that share its hover.
- ComboboxA filterable select with chips, groups and async results, in a popup that resizes as you type.
- Date pickerA button that opens a calendar in a popover, or a bottom sheet on phones, for single dates and ranges.
- FieldLabels, descriptions and errors wired to their control, with validation states and layouts for forms.
- InputA text input with three sizes, invalid and read-only states, native validation styling and a 16px touch font so phones never zoom in.
Used in Pro blocks
Blocks that build on Label.
Every block comes with HextaUI Pro, and Prompt Input is free.