Field
A group of parts that gives a form control a label and validation.
Visible on your profile
<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>.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);
}
}<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:
<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:
<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
| Prop | Type | Default | Description |
|---|---|---|---|
dirty | boolean | undefined | The 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. | |
disabled | boolean | undefined | false | Disables the field and its control. |
invalid | boolean | undefined | false | Makes the field invalid, for a check outside the field. |
name | string | undefined | The name of the field in the data and the values of the form. It wins over the name of the
control. | |
touched | boolean | undefined | The 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>) | undefined | Checks 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. | |
validationDebounceTime | number | undefined | 0 | The time in milliseconds that the field waits after the last change before it validates. |
validationMode | FormValidationMode | undefined | The 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'. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: FieldRootStatevalidity: FieldValidityerrors: string[]value: unknowninitialValue: 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) => voidstate: FieldRootState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
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
| Member | Type | Description |
|---|---|---|
element | HTMLDivElement | null | The element of the part, or null while it is not mounted. |
validate | () => void | Validates 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
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | The id of the label. With no id, the label has an id of its own. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: FieldRootState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: FieldRootState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLLabelElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | The id of the description. With no id, the description has an id of its own. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: FieldRootState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: FieldRootState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLParagraphElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | Disables the control of the item. A disabled field, group or fieldset also disables it. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: FieldRootState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: FieldRootState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLDivElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | The 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" | undefined | Shows 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. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
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) => voidstate: FieldRootStatemessages: string[] | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLDivElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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 UI | Vase UI |
|---|---|
Field.Root | v-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 validationMode | The 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.Label | The 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.Control | Input is the control of a field, and it also works with no field. |
Field.Description | CSS selects :focus-within on the field, so the state has no focused. |
Field.Item | CSS 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.Validity | The default slot of Field.Root gives the validity, the errors, the value and the first value. |
Added in Vase UI
| Part | Member | Why |
|---|---|---|
Field.Root | update:dirty event | v-model:dirty binds the dirty state. Base UI takes the dirty prop and has no event for the change. |
Field.Root | update:touched event | v-model:touched binds the touched state. Base UI takes the touched prop and has no event for the change. |
Behavior
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.