skip section navigation

Field, FieldLabel and FieldDescription

arrange a control and its explanation, then connect them with explicit IDs. the pieces work with Input, Selector or native HTML without owning your form state.

a short name makes the shelf easier to find.

usage

svelte
<script lang="ts">
	import { Field, FieldLabel, FieldDescription, Input } from '@ciabi/loafkit-ui';
	let shelf = $state('weekend');
</script>

<Field>
	<FieldLabel htmlFor="shelf-name">shelf name</FieldLabel>
	<Input placeholder="weekend reading" id="shelf-name" bind:value={shelf} aria-describedby="shelf-help" />
	<FieldDescription id="shelf-help">a short name makes the shelf easier to find.</FieldDescription>
</Field>

composition

one useful arrangement, not a required hierarchy. the control can be any component that supports the relationships you need.

  • Field
    • FieldLabel
    • Input (or another control)
    • FieldDescription(optional)

match htmlFor to the control’s id. without htmlFor, FieldLabel renders a span; its id can name a group that supports aria-labelledby. these pieces don’t forward arbitrary attributes.

hint and error together

this becomes part of the shelf link, like garden-notes.

svelte
<script lang="ts">
	import { Field, FieldLabel, FieldDescription, Input } from '@ciabi/loafkit-ui';
	let address = $state('');
	let touched = $state(false);
	let addressError = $derived(touched && !/^[a-z]+(?:-[a-z]+)*$/.test(address)
		? 'use lowercase letters with a hyphen between words.' : '');
</script>

<Field>
	<FieldLabel htmlFor="shelf-address">shelf address</FieldLabel>
	<Input placeholder="garden-notes" id="shelf-address" bind:value={address} onValueBlur={() => touched = true}
		aria-invalid={addressError ? 'true' : undefined}
		aria-describedby={addressError ? 'address-hint address-error' : 'address-hint'} />
	<FieldDescription id="address-hint">this becomes part of the shelf link, like garden-notes.</FieldDescription>
	<div aria-live="polite">
		{#if addressError}
			<FieldDescription id="address-error" class="field__error">{addressError}</FieldDescription>
		{/if}
	</div>
</Field>

validation, aria-invalid and announcements belong to your app. FieldDescription doesn’t announce a change automatically; this example adds a polite live region.

API reference

Field

Component
children

the label, control and supporting content to group.

class
Default: ''

extra classes on the outer group.

style

inline styles on the outer group.

FieldLabel

Component
children

the visible name for a control or group.

htmlFor

the control ID. renders a native label when supplied; otherwise renders a span.

id

an ID for this label, including group naming through aria-labelledby.

class
Default: ''

extra classes on the label or span.

FieldDescription

Component
children

the hint or error text.

id

the ID a control can include in aria-describedby.

class
Default: ''

extra classes on the description paragraph.