Skip to content
Sveltier
Mode
Forms

Field

The label, box, hint and error of every boxed field, around a control of your own.

Usage

Field draws everything around a control: a TextField is a Field with an input in it. Wrap any other control the same way. The children snippet receives the id, the ARIA links and the state; spread them onto your control, and it takes the field's size, variant and focus accent.

The first payment goes out on this day

In your time zone

Schedule.svelte
<script>
  import { Field } from 'sveltier';
  let start = $state('');
</script>

<Field label="Start date" hint="The first payment goes out on this day" required>
  {#snippet children(attributes)}
    <input type="date" name="start" {...attributes} bind:value={start} />
  {/snippet}
</Field>

What the control receives

NameTypeDescription
idstringThe id the label points at.
classstringsv-field__control: fills the box and takes its text style.
aria-describedbystring | undefinedThe hint or the error, whichever is shown.
aria-invalidtrue | undefinedSet while the field shows an error.
requiredboolean
disabledboolean
readonlyboolean

Errors

Field shows the error you give it, and does not check a value itself. TextField and the other form controls do, as described under TextField validation.

Accessibility

  • The label points at the control's id, and the hint or error describes it, so the control needs only the attributes it receives.
  • A press on the box around the control focuses it, except on a button inside the box.

API

NameTypeDefaultDescription
variant'outlined' | 'tonal' | 'elevated' | 'text' | 'plain''outlined'How the box is drawn.
colorColor'primary'The accent of the focused field. An invalid field uses error.
size'xs' | 'sm' | 'md' | 'lg' | 'xl''md'Height and text size, matching a Button of the same size.
density'compact' | 'default' | 'comfortable''default'Multiplies the height and padding, never the text.
roundedboolean | 'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full'trueCorner rounding. true keeps the theme's radius.
disabledbooleanfalseMutes the field and disables its control.
labelstring–The visible label, linked to the control. Without it, name the control with aria-label.
hintstring–Help under the field. An error replaces it.
errorFieldError–An error from outside the field, such as the server's answer: a message, a list of which the first is shown, or true for an invalid state described elsewhere.
prependSnippet–Content at the start of the box, usually an icon.
appendSnippet–Content at the end of the box: an icon, a unit or a button.
idstring | null–The id of the control. Generated when not given.
requiredbooleanfalseAdds an asterisk to the label. The control carries required itself.
readonlybooleanfalseThe control can be read and focused but not changed.
childrenSnippet<[FieldControlAttributes]>–The control. Spread the attributes it receives onto it.
classClassValue | null–Classes added after the component's own: a string, an array or an object.
refHTMLDivElement | null–The root element, bindable with bind:ref.