Label
Visible name for a control. A sibling, not a compound part. htmlFor is required and must equal the control id. For a Field that is the Field.Root id. Not a control.
On this page
Server Components can import it from @tounsoo/todae/label. That entry has no 'use client' and no hooks; the main @tounsoo/todae entry is a client module.
With Field
Label carries no margin. Put the label, the control and any Description or ErrorMessage in a Field.Group, which puts the label field.label.gap (0.4rem) above the control.
import { Field, Label } from '@tounsoo/todae';
import { useId, useState } from 'react';
export default function LabelSibling() {
const id = useId();
const [value, setValue] = useState('');
return (
<Field.Group>
<Label.Root htmlFor={id}>Email</Label.Root>
<Field.Root id={id}>
<Field.TextInput value={value} onChange={(event) => setValue(event.target.value)} />
</Field.Root>
</Field.Group>
);
}
One Field.Group per field. The order is Label, control, then Description and ErrorMessage. When ErrorMessage follows Description, the gap between them is field.messages.gap (0.2rem). Do not put several fields in one group. Put groups inside Stack.Root: stack.gap is the density gap between fields, not between a label and its shell.
States
Disabled and invalid live on Field.Root. Label does not repaint. A required mark is authored in the label text.
import { Field, Label } from '@tounsoo/todae';
import { useId, useState } from 'react';
export default function FieldLabelStates() {
const idleId = useId();
const disabledId = useId();
const invalidId = useId();
const [email, setEmail] = useState('');
return (
<>
<Field.Group>
<Label.Root htmlFor={idleId}>Email</Label.Root>
<Field.Root id={idleId}>
<Field.TextInput
value={email}
onChange={(event) => setEmail(event.target.value)}
placeholder="you@example.com"
/>
</Field.Root>
</Field.Group>
<Field.Group>
<Label.Root htmlFor={disabledId}>Email</Label.Root>
<Field.Root id={disabledId} disabled>
<Field.TextInput value="Disabled" onChange={() => undefined} />
</Field.Root>
</Field.Group>
<Field.Group>
<Label.Root htmlFor={invalidId}>Email</Label.Root>
<Field.Root id={invalidId} invalid>
<Field.TextInput value="Invalid" onChange={() => undefined} />
</Field.Root>
</Field.Group>
</>
);
}
Accessible composition
Todae handles: Label.Root renders a native <label> and requires htmlFor in its type. The browser does the rest: the text becomes the control's accessible name, and a click on it focuses or toggles the control. Label adds no ARIA, no required mark and no disabled or invalid style.
You supply: The control's id in htmlFor. For a Field that is the Field.Root id, which Root passes to its input. Write a required mark in the text, and set required on the input itself: on a Field, that is the input part, such as Field.TextInput, not Field.Root. To hide the label visually, put its text in a VisuallyHidden inside it, so it still names the control.
Watch for: htmlFor names only labelable elements, such as an input, select or textarea. A role="radiogroup" div or a useField control with its own role gets no name from it; use aria-labelledby there. Checkbox, Radio and Switch already wrap their children in a label, so don't add a Label for them. Placeholder text is not a label.
When to use
When: Visible name for a control, placed as a sibling. htmlFor = control id (the Field.Root id for a Field). Required mark goes in the text.
When not: Inside Field.Root. Checkbox / Radio / Switch (children are the label). Group caption → RadioGroup aria-label / aria-labelledby. Hint text → Description.
Props
| Name | Type | Description |
|---|---|---|
htmlFor Required | string | Id of the labelled control. Must match Field.Root id when captioning a Field. |
children | React.ReactNode | Label text. A required mark is authored here when needed. |
className | string | Optional class on the native label. |