Veil UI
Components

Form Field

Pairs a label, control, hint and error message with consistent spacing.

Installation

$ npx veil-ui@latest add form-field

Usage

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

Set aria-invalid and aria-describedby on the control so the error is announced with the field.

API reference

FormField

PropTypeDefaultDescription
...propsReact.ComponentProps<"div">—Native div attributes. Renders a two-row grid.

FormFieldLabel

PropTypeDefaultDescription
...propsReact.ComponentProps<typeof Label>—Pass htmlFor to associate the label with its control.

FormFieldError

PropTypeDefaultDescription
childrenReact.ReactNode—Message text. Nothing renders when empty.

Accessibility

  • FormFieldError renders role="alert" and disappears when there is no message, so the region never sits empty.
  • Always connect the label with htmlFor and the input with a matching id.
  • Link the error through aria-describedby and set aria-invalid on 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>

On this page