Field
A control with its label, a hint and its error, wired for assistive technology.
As on the invoice
Installation
pnpm dlx @human-kit/design add fieldnpx @human-kit/design add fieldyarn dlx @human-kit/design add fieldbunx @human-kit/design add fieldThe command copies the files to src/lib/components/ui/field. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Field from '$lib/components/ui/field';
import * as Input from '$lib/components/ui/input';
let supplier = $state('');
</script>
<Field.Root label="Supplier" description="The legal name, as on the invoice.">
{#snippet children(control)}
<Input.Root {...control} bind:value={supplier} />
{/snippet}
</Field.Root> Examples
Invalid
Give error the message while the value is wrong, and nothing once it is right. The message takes the place of the hint, so the form does not jump, and the label and the control turn red with it.
The tax ID needs 9 digits.
The tax ID needs 9 digits.Horizontal
orientation="horizontal" puts the label in a column beside the control, on its first line, with the hint under the control. The column is as wide as the label. In a form, set --field-label-width once so every label lines up.
As printed on the vial.
The parts by hand
For a layout Field.Root does not draw, such as a count on the label’s line, use Field.Label, Field.Description and Field.Error on their own. The ids are then yours to wire: for on the label, aria-describedby on the control.
0/40
Guidelines
- Wrap every control of a form in a
Field.Root, rather than a loose<label>: it wires the ids, so the label names the control and the hint is read with it. - Around a
CheckboxGroup.Rootor aRadioGroup.Root, addgroup: the label is the question of the set, and the error turns every option red. - Spread
controlon the control,<Input.Root {...control} />. It carries the id,invalid,required,disabledand the ARIA links in one go. - Use
descriptionfor what the field takes, before it goes wrong: a format, a limit, where to find the value. - Say in
errorwhat to do, not only what failed: "The tax ID needs 9 digits", not "Invalid". - Mark the fields that have to be filled with
required, not the optional ones. If most fields are optional, mark the few that are not.
Accessibility
- The label is a native
<label>whoseforis the control's id, so a click on it focuses the control. - A control that is not an
<input>, such as the trigger of a select, is named througharia-labelledby, whichcontrolalso carries. - The hint or the error is linked with
aria-describedby, so a screen reader reads it after the name. - The error is a
role="alert": it is announced the moment it appears, without moving the focus. - The required mark is drawn only. The control says it to assistive technology with
aria-required.
API reference
Field.Root
| Prop | Type | Default |
|---|---|---|
label | string The name over the control. | — |
description | string A hint under the control. An error takes its place while there is one. | — |
error | string What is wrong with the value. Turns the label and the control red. | — |
required | boolean Marks the label and passes | false |
disabled | boolean Fades the label and passes | false |
orientation | 'vertical' | 'horizontal' The label over the control, or in a column beside it, as wide as the label, or | 'vertical' |
group | boolean The control is a set, a checkbox or a radio group: the label names it through | false |
id | string The control's id. One is made up when none is given. | — |
class | string Classes for the wrapping | — |
children | Snippet<[FieldControlProps]> The control. It receives | — |
It renders a <div> around the label, the control and the line under it. It takes no other attributes.
Field.Label
| Prop | Type | Default |
|---|---|---|
invalid | boolean Red with the control while what it holds is wrong. | false |
disabled | boolean Faded with the control while it is disabled. | false |
required | boolean Ends the name with the mark of a field that has to be filled. | false |
Plus every attribute of a native <label>.
Field.Description
| Prop | Type | Default |
|---|---|---|
children | Snippet The hint. | — |
A <p> that takes every attribute of a paragraph. Give its id to the control's aria-describedby.
Field.Error
| Prop | Type | Default |
|---|---|---|
children | Snippet What is wrong, and what to do. | — |
A <p role="alert"> that takes every attribute of a paragraph. Give its id to the control's aria-describedby.