Skip to the content
human-kit design
07 — Form

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 field
npx @human-kit/design add field
yarn dlx @human-kit/design add field
bunx @human-kit/design add field

The 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.Root or a RadioGroup.Root, add group: the label is the question of the set, and the error turns every option red.
  • Spread control on the control, <Input.Root {...control} />. It carries the id, invalid, required, disabled and the ARIA links in one go.
  • Use description for what the field takes, before it goes wrong: a format, a limit, where to find the value.
  • Say in error what 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> whose for is 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 through aria-labelledby, which control also 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

PropTypeDefault
labelstring

The name over the control.

—
descriptionstring

A hint under the control. An error takes its place while there is one.

—
errorstring

What is wrong with the value. Turns the label and the control red.

—
requiredboolean

Marks the label and passes required to the control.

false
disabledboolean

Fades the label and passes disabled to the control.

false
orientation'vertical' | 'horizontal'

The label over the control, or in a column beside it, as wide as the label, or --field-label-width.

'vertical'
groupboolean

The control is a set, a checkbox or a radio group: the label names it through aria-labelledby instead of pointing at one input.

false
idstring

The control's id. One is made up when none is given.

—
classstring

Classes for the wrapping <div>.

—
childrenSnippet<[FieldControlProps]>

The control. It receives id, invalid, required, disabled, aria-labelledby and aria-describedby, to spread on it.

—

It renders a <div> around the label, the control and the line under it. It takes no other attributes.

Field.Label

PropTypeDefault
invalidboolean

Red with the control while what it holds is wrong.

false
disabledboolean

Faded with the control while it is disabled.

false
requiredboolean

Ends the name with the mark of a field that has to be filled.

false

Plus every attribute of a native <label>.

Field.Description

PropTypeDefault
childrenSnippet

The hint.

—

A <p> that takes every attribute of a paragraph. Give its id to the control's aria-describedby.

Field.Error

PropTypeDefault
childrenSnippet

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.