Label
Names a control outside a Field. A press on the label reaches the control, whatever kind it is.
Usage
import { Label } from 'nyctoui/label'
<Label htmlFor="email">Email</Label>
<Input id="email" />Point htmlFor at the control’s id. A native <label> only names inputs, buttons and selects; Label names any control, such as a segmented control or a radio group, and a press on it focuses, checks or clicks the control as a native label would.
Reach for the closest part that fits. In a Field, use FieldLabel, which also ties the description and errors to the control. To set a checkbox or switch beside its text, use CheckboxLabel. A slider has SliderLabel. Use Label for the rest: a control in a toolbar, a settings row, or a form laid out by hand.
Good for
- Naming a segmented control or a radio group in a hand-built settings row.
- A label beside a switch in a preferences panel.
- A form laid out by hand, outside the Field part.
- Naming a slider or a color picker in an editor’s sidebar.
Examples
Any control
A segmented control is a group, which a native label can’t name. Press Density: focus moves to the selected segment, and a screen reader names the group Density.
Inline
Beside a control in a row, as in a toolbar. The label has no margin or layout of its own, so the row decides the spacing.
Disabled
A label can’t see the state of the control it points at, so pass disabled to dim it with the control.
In a panel row
Render a PanelItemTitle as a label with render={<Label htmlFor="…" />} when the row holds a single control.
Best practices
- Point
htmlForat the control’sid, so a press on the label reaches the control. - Use a
Fieldinstead when the control also has a description or an error. - Write the label as a noun or a short statement, like “Theme” or “Email me on mentions”.
- Use a label as a heading for a group of fields. Use a heading or a fieldset legend.
- Leave a control without a label because its neighbors make the purpose clear. Screen readers don’t see the neighbors.
API reference
Label
Renders a <label>. Names the control its htmlFor points at, or the control inside it.
| Prop | Type | Default |
|---|---|---|
htmlForThe id of the control to name. | string | — |
disabledDims the label to match a disabled control. | boolean | false |
renderRenders a different element, such as a PanelItemTitle. It still names its control. | ReactElement | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |