Skip to content

Field ​

A group of parts that gives a form control a label and validation.

Visible on your profile

vue
<script setup lang="ts">
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import styles from './index.module.css';
</script>

<template>
  <Field.Root :class="styles.Field">
    <Field.Label :class="styles.Label">Name</Field.Label>
    <Input required placeholder="Required" :class="styles.Input" />

    <Field.Error :class="styles.Error" match="valueMissing">Please enter your name</Field.Error>

    <Field.Description :class="styles.Description">Visible on your profile</Field.Description>
  </Field.Root>
</template>
css
.Field {
  display: flex;
  flex-direction: column;
  align-items: start;
  gap: 0.25rem;
  width: 100%;
  max-width: 16rem;
}

.Label {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 100%;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Error {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(50.5% 0.213 27.518deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.4% 0.191 22.216deg);
  }
}

.Description {
  margin: 0;
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(43.9% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
vue
<script setup lang="ts">
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
</script>

<template>
  <Field.Root class="flex w-full max-w-64 flex-col items-start gap-1">
    <Field.Label class="text-sm font-bold text-neutral-950 dark:text-white">Name</Field.Label>
    <Input
      required
      placeholder="Required"
      class="h-8 self-stretch border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
    />

    <Field.Error class="text-sm text-red-700 dark:text-red-400" match="valueMissing"
      >Please enter your name</Field.Error
    >

    <Field.Description class="text-sm text-neutral-600 dark:text-neutral-400"
      >Visible on your profile</Field.Description
    >
  </Field.Root>
</template>

Anatomy ​

Import the components and assemble their parts:

vue
<script setup lang="ts">
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
</script>

<template>
  <Field.Root>
    <Field.Label />
    <Input />
    <Field.Description />
    <Field.Error />
  </Field.Root>
</template>

Examples ​

Show a message for one failure ​

The default slot of Field.Root gives the validity, the errors and the value of the field. Use them in the content of a Field.Error, so that the control names the message in aria-describedby:

vue
<script setup lang="ts">
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
</script>

<template>
  <Field.Root v-slot="{ value }" name="email">
    <Field.Label>Email</Field.Label>
    <Input type="email" required />
    <Field.Error match="typeMismatch">{{ value }} is not an email address.</Field.Error>
  </Field.Root>
</template>

API reference ​

Field.Root ​

Groups all parts of the field and its control, such as an Input, a Checkbox or a RadioGroup. Renders a <div> element.

Props

PropTypeDefaultDescription
dirtyboolean | undefinedThe value differs from its value at the mount or at the last native reset of the form. Bind it with v-model:dirty. With no dirty, the field finds the state itself.
disabledboolean | undefinedfalseDisables the field and its control.
invalidboolean | undefinedfalseMakes the field invalid, for a check outside the field.
namestring | undefinedThe name of the field in the data and the values of the form. It wins over the name of the control.
touchedboolean | undefinedThe focus left the control one time or more. Bind it with v-model:touched. With no touched, the field finds the state itself.
validate((value: unknown, formValues: FormValues) => FieldValidateResult | Promise<FieldValidateResult>) | undefinedChecks the value after the native check passes. Return the error messages: a string or an array of strings. Return nothing, null, an empty string or an empty array for a valid value. formValues holds the values of the fields of the form, by name, and is empty with no form. The form does not wait for a promise.
validationDebounceTimenumber | undefined0The time in milliseconds that the field waits after the last change before it validates.
validationModeFormValidationMode | undefinedThe moments when the field validates its control. With 'submit', the field validates at each submit of the form, and at each change after the first submit. With 'blur', it validates when the focus leaves the control. With 'change', it validates at each change. With no validationMode, the field takes the mode of its form, or else 'submit'.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: FieldRootState
validity: FieldValidity
errors: string[]
value: unknown
initialValue: unknown
The content of the field. validity has a flag for each failure and valid, and errors has the messages. value is the value of the control. initialValue is its value at the mount or at the last native reset of the form.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: FieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Events

EventArgumentsDescription
update:dirty[dirty: boolean]The dirty state changes. v-model:dirty uses it.
update:touched[touched: boolean]The touched state changes. v-model:touched uses it.

Exposed

MemberTypeDescription
elementHTMLDivElement | nullThe element of the part, or null while it is not mounted.
validate() => voidValidates the field at once. A required control with no value is invalid, also when the user did not change it. It does not wait for a promise of validate.

Selectors

SelectorDescription
[data-disabled]The field is disabled.
[data-valid]The field is valid.
[data-invalid]The field is invalid.
[data-touched]The focus left the control.
[data-dirty]The value differs from the first value.
[data-filled]The control has a value.

Field.Label ​

An accessible label of the control of the field. A <label> names a native control with for. Another element, and the label of a group such as a RadioGroup, name the control with aria-labelledby. A click on such a label moves the focus to the control. Renders a <label> element.

Props

PropTypeDefaultDescription
idstring | undefinedThe id of the label. With no id, the label has an id of its own.

Slots

SlotSlot propsDescription
default
state: FieldRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: FieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLLabelElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-disabled]The field is disabled.
[data-valid]The field is valid.
[data-invalid]The field is invalid.
[data-touched]The focus left the control.
[data-dirty]The value differs from the first value.
[data-filled]The control has a value.

Field.Description ​

A paragraph with more information about the field. The control names it in aria-describedby. Renders a <p> element.

Props

PropTypeDefaultDescription
idstring | undefinedThe id of the description. With no id, the description has an id of its own.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: FieldRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: FieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLParagraphElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-disabled]The field is disabled.
[data-valid]The field is valid.
[data-invalid]The field is invalid.
[data-touched]The focus left the control.
[data-dirty]The value differs from the first value.
[data-filled]The control has a value.

Field.Item ​

Groups an item of a checkbox group or a radio group with its label and description. A Field.Label in the item labels the control of the item, and a Field.Description describes it. Renders a <div> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseDisables the control of the item. A disabled field, group or fieldset also disables it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: FieldRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: FieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLDivElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-disabled]The item is disabled.
[data-valid]The field is valid.
[data-invalid]The field is invalid.
[data-touched]The focus left the control.
[data-dirty]The value differs from the first value.
[data-filled]The control has a value.

Field.Error ​

An error message that shows while the field is invalid. The control names it in aria-describedby while it shows. Renders a <div> element.

Props

PropTypeDefaultDescription
idstring | undefinedThe id of the error. With no id, the error has an id of its own.
match"badInput" | "customError" | "patternMismatch" | "rangeOverflow" | "rangeUnderflow" | "stepMismatch" | "tooLong" | "tooShort" | "typeMismatch" | "valueMissing" | undefinedShows the error only while the field has this failure, such as valueMissing. A failure of validate is customError. With no match, the error shows while the field is invalid.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: FieldRootState
The content of the error. With no content, the error shows the messages of the field: one message as text, and more as a list.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: FieldRootState
messages: string[]
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLDivElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-disabled]The field is disabled.
[data-valid]The field is valid.
[data-invalid]The field is invalid.
[data-touched]The focus left the control.
[data-dirty]The value differs from the first value.
[data-filled]The control has a value.

Differences from Base UI ​

Vase UI ports Field from Base UI 1.9.0.

API ​

Base UIVase UI
Field.Rootv-model:dirty and v-model:touched bind the dirty and touched states. CSS selects :focus-within, so the state has no focused. Vue gives an absent Boolean prop the value false, so disabled needs no default.
Field.Root prop validationModeThe modes are 'submit', 'blur' and 'change'. The prop has no default: with no prop, the field takes the mode of its form, or else 'submit', as in Base UI. A default would hide that the prop is absent.
Field.LabelThe label reads its element after the mount, so it needs no nativeLabel: a <label> names the control with for, and another element with aria-labelledby. CSS selects :focus-within on the field, so the state has no focused.
Field.ControlInput is the control of a field, and it also works with no field.
Field.DescriptionCSS selects :focus-within on the field, so the state has no focused.
Field.ItemCSS selects :focus-within on the field, so the state has no focused. Vue gives an absent Boolean prop the value false, so disabled needs no default.
Field.Error<Transition> around Field.Error animates it, so the state has no transitionStatus, and the error has no data-starting-style and no data-ending-style. match takes the name of one failure. The invalid prop of Field.Root shows the error with no failure, and the default slot of Field.Root has validity.valid. CSS selects :focus-within on the field, so the state has no focused.
Field.ValidityThe default slot of Field.Root gives the validity, the errors, the value and the first value.

Added in Vase UI ​

PartMemberWhy
Field.Rootupdate:dirty eventv-model:dirty binds the dirty state. Base UI takes the dirty prop and has no event for the change.
Field.Rootupdate:touched eventv-model:touched binds the touched state. Base UI takes the touched prop and has no event for the change.

Behavior ​

Vase UIEvidence
The server HTML of a control in a field has the id of the label in aria-labelledby and the ids of the messages in aria-describedby, in each order of the parts. A control with no label has no aria-labelledby. Base UI adds these attributes after the page hydrates.
A native form reset removes the error of the field. Each control of a field does this after each reset that it follows, also when its value does not change. The control cancels the pending validation and removes the message that the field put on the input. It also clears the validity of the field and the error of the form for the field. The change of a reset starts no validation. Base UI keeps the error that the field showed before the reset. Its controls do not listen for a reset, and a reset sends no input event.
Each Field.Label has the id of its id prop, or an id of its own, and the id follows the prop. A <label> names the control with for. Base UI gives each label the id of the last label that registered. Two labels of one field then have the same id, and a change of the id prop has no effect.
A field with more than one input, such as a radio group or a checkbox group, takes the first invalid input in the document, or else the first eligible input in the document. The field reads the validity of that input. A form that blocks the submit or gives an error to the field focuses the part of that input. Inputs in two trees, such as two shadow roots, keep the order of the registrations. Base UI takes the first input in the order of the registrations. That order differs from the document after a keyed move, after an input mounts before the other inputs, and for an input outside a branch that Activity shows again. A form then focuses an input that is not the first one, while Base UI's Form focuses the first invalid field by document position.
In a field, the element slot can give the element of the control, of the label or of a message another id than the part gives. After the mount, the for of the label names the id of the control element, and a click on a label that is not native focuses that element. The control names the id of the label element in aria-labelledby and the id of each message element in aria-describedby. Base UI keeps the id of the part in each reference, and the id of the element of the developer wins on the element. The label then names no element, and a click on it does not reach the control.
A disabled field has no validity from each source: valid is null, no part has data-invalid, the control has no aria-invalid, and no Field.Error shows. This holds for a disabled Field.Root, for a field in a disabled fieldset and for a control that its own disabled prop disables, also with the invalid prop and with an error of a Form. A disabled control gives its form no value, and :invalid of CSS never matches it, so one style rule covers the two states. The browser and validate do not validate a read-only control, because the HTML Standard bars it from constraint validation. A read-only control shows the invalid prop and an error of a form, because the form submits its value. Base UI keeps data-invalid on a disabled field for the invalid prop and for an error of a form, and it shows the error of a form in a field whose control is disabled.
A field counts for a Form while the browser lists an input of its control in the elements of the <form> (element.form). A control that a Teleport moves out of the <form> counts when it has the form attribute with the id of the form, as in HTML. A group with no mounted input does not count. So the fields that the form validates and the values that the form submits (FormData) are the same set. Base UI counts each field below the Form in the React tree, also a control in a portal and a group with no input.
A radio group and a checkbox group are one control each. The field becomes touched when the focus leaves the group. A move of the focus from one radio to another in the group, also by an arrow key that selects, is not a leave, so a field in blur mode does not validate in the middle of a selection by keyboard. Base UI marks the field touched at a move in the group.
Enter has no meaning of its own in a field. Enter in a text control submits its form as the browser does, and the submit validates the field one time. Where the browser does not submit (a field with no form, a form whose submit button is disabled), nothing validates. A developer validates at a key with validate() of Field.Root. Base UI validates the field at Enter also when no submit occurs.
The required rule counts from the first edit of the user until a native reset, also when a listener puts the first value back. A field in blur mode validates the value that the control has after the blur listeners of the developer ran, so a required control that such a listener emptied shows valueMissing. :user-invalid of CSS matches in the same case. Base UI shows no error when a blur listener puts the first value back.
A control in a field keeps the id of the developer in the server HTML when no Field.Label of the field renders before it, and a label that renders after the control has that id in for. When a label renders before the control, the control has the generated id of the field until the hydration, because that label names the generated id. Then the control takes its id, and the label follows. In each order, the label names the control in the server HTML. Base UI gives each control in a field the generated id until the hydration, also where no label needs it.

The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.