Components
Form Field
Pairs a label, control, hint and error message with consistent spacing.
Installation
$ npx veil-ui@latest add form-fieldUsage
import {
FormField,
FormFieldLabel,
FormFieldDescription,
FormFieldError,
} from "@/components/ui/form-field";
import { Input } from "@/components/ui/input";
<FormField>
<FormFieldLabel htmlFor="email">Email</FormFieldLabel>
<Input id="email" type="email" />
<FormFieldDescription>We never share it.</FormFieldDescription>
<FormFieldError>Enter a valid address.</FormFieldError>
</FormField>;Examples
Basic
With description
Shown on your public profile. Markdown is supported.
With error
Enter a valid email address.
Set aria-invalid and aria-describedby on the control so the error is
announced with the field.
API reference
FormField
| Prop | Type | Default | Description |
|---|---|---|---|
| ...props | React.ComponentProps<"div"> | — | Native div attributes. Renders a two-row grid. |
FormFieldLabel
| Prop | Type | Default | Description |
|---|---|---|---|
| ...props | React.ComponentProps<typeof Label> | — | Pass htmlFor to associate the label with its control. |
FormFieldError
| Prop | Type | Default | Description |
|---|---|---|---|
| children | React.ReactNode | — | Message text. Nothing renders when empty. |
Accessibility
FormFieldErrorrendersrole="alert"and disappears when there is no message, so the region never sits empty.- Always connect the label with
htmlForand the input with a matchingid. - Link the error through
aria-describedbyand setaria-invalidon the control.
Vue and plain HTML
This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add form-field --framework vue or switch the framework in components.json.
<script setup lang="ts">
import { computed, provide, ref, useId } from "vue";
import { cn } from "@/lib/utils";
import { FORM_FIELD_KEY } from "./context";
const props = withDefaults(
defineProps<{
class?: string;
/** Id applied to the control; generated when omitted. */
id?: string;
required?: boolean;
optional?: boolean;
invalid?: boolean;
}>(),
{ required: false, optional: false, invalid: false },
);
const generatedId = useId();
const controlId = computed(() => props.id ?? `field-${generatedId}`);
const descriptionId = computed(() => `${controlId.value}-description`);
const errorId = computed(() => `${controlId.value}-error`);
const hasDescription = ref(false);
const hasError = ref(false);
const required = computed(() => props.required);
const optional = computed(() => props.optional);
const invalid = computed(() => props.invalid);
const describedBy = computed(() => {
const ids: string[] = [];
if (hasDescription.value) ids.push(descriptionId.value);
if (hasError.value) ids.push(errorId.value);
return ids.length > 0 ? ids.join(" ") : undefined;
});
provide(FORM_FIELD_KEY, {
controlId,
descriptionId,
errorId,
required,
optional,
invalid,
describedBy,
hasDescription,
hasError,
});
</script>
<template>
<div
data-slot="form-field"
:data-invalid="invalid ? true : undefined"
:data-required="required ? true : undefined"
:class="cn('grid gap-2', props.class)"
>
<slot />
</div>
</template>