Form Field
Pure structure, no state machine. It stacks a label, an optional description, the control you pass as children, and an optional error message. The component derives the ids that tie them together for screen reader wiring.
Interactive Preview
Section titled “Interactive Preview”import { FormField, Input } from "@/components/ui/form-field";
<FormField id="full-name" label="Full name" description="As it appears on your ID."> <Input id="full-name" aria-describedby="full-name-desc" placeholder="Full name" /></FormField>
<FormField id="email" label="Email" error="Enter a valid email address."> <Input id="email" invalid aria-describedby="email-error" /></FormField><script lang="ts"> import FormField from "@/components/ui/form-field.svelte"; import Input from "@/components/ui/input.svelte";</script>
<FormField id="full-name" label="Full name" description="As it appears on your ID."> <Input id="full-name" aria-describedby="full-name-desc" placeholder="Full name" /></FormField>
<FormField id="email" label="Email" error="Enter a valid email address."> <Input id="email" invalid aria-describedby="email-error" /></FormField>import { FormField, Input } from "@/components/ui/form-field";
<FormField id="full-name" label="Full name" description="As it appears on your ID."> <Input id="full-name" aria-describedby="full-name-desc" placeholder="Full name" /></FormField>
<FormField id="email" label="Email" error="Enter a valid email address."> <Input id="email" invalid aria-describedby="email-error" /></FormField>Installation
Section titled “Installation”npx @glaceon/cli add form-field --framework react # or svelte, solidFormField is CSS-only. No Zag machine is needed.
bun add clsx tailwind-mergeThen copy the source for your framework:
import type { ReactNode } from "react"import { cn } from "@/lib/cn"
export interface FormFieldProps { /** Shared with the control. The field renders the label against it and derives `-desc` / `-error` ids from it. */ id: string label?: string description?: string error?: string children?: ReactNode className?: string}
const rootClass = "flex flex-col gap-1.5"const labelClass = "text-xs font-bold text-muted"const descriptionClass = "text-xs text-muted"const errorClass = "text-xs font-bold text-danger"
export function FormField({ id, label, description, error, children, className,}: FormFieldProps) { return ( <div className={cn(rootClass, className)}> {label ? ( <label htmlFor={id} className={labelClass}> {label} </label> ) : null} {description ? ( <span id={`${id}-desc`} className={descriptionClass}> {description} </span> ) : null} {children} {error ? ( <span id={`${id}-error`} role="alert" className={errorClass}> {error} </span> ) : null} </div> )}<script lang="ts"> import type { Snippet } from "svelte" import { cn } from "@/lib/cn"
interface Props { id: string label?: string description?: string error?: string children?: Snippet class?: string }
const { id, label = undefined, description = undefined, error = undefined, children, class: className, }: Props = $props()
const rootClass = "flex flex-col gap-1.5" const labelClass = "text-xs font-bold text-muted" const descriptionClass = "text-xs text-muted" const errorClass = "text-xs font-bold text-danger"</script>
<div class={cn(rootClass, className)}> {#if label} <label for={id} class={labelClass}>{label}</label> {/if} {#if description} <span id={`${id}-desc`} class={descriptionClass}>{description}</span> {/if} {@render children?.()} {#if error} <span id={`${id}-error`} role="alert" class={errorClass}>{error}</span> {/if}</div>import { Show, type JSX } from "solid-js"import { cn } from "@/lib/cn"
export interface FormFieldProps { /** Shared with the control. The field renders the label against it and derives `-desc` / `-error` ids from it. */ id: string label?: string description?: string error?: string children?: JSX.Element class?: string}
const rootClass = "flex flex-col gap-1.5"const labelClass = "text-xs font-bold text-muted"const descriptionClass = "text-xs text-muted"const errorClass = "text-xs font-bold text-danger"
export function FormField(props: FormFieldProps) { return ( <div class={cn(rootClass, props.class)}> <Show when={props.label}> <label for={props.id} class={labelClass}> {props.label} </label> </Show> <Show when={props.description}> <span id={`${props.id}-desc`} class={descriptionClass}> {props.description} </span> </Show> {props.children} <Show when={props.error}> <span id={`${props.id}-error`} role="alert" class={errorClass}> {props.error} </span> </Show> </div> )}The id contract
Section titled “The id contract”The wrapper cannot reach into your control, so three ids have to line up. The component owns two of them and derives them from the id prop you pass.
| Element | Attribute | Value |
|---|---|---|
| label | for |
the id you pass |
| description | id |
${id}-desc |
| error | id |
${id}-error |
| error | role |
alert, announced when it appears |
Your control must use the same id, and point its aria-describedby at -desc and -error when those render. This is the one thing the component cannot do for you.
API Reference (Props)
Section titled “API Reference (Props)”| Prop | Type | Default | Description |
|---|---|---|---|
id |
string |
required | Base id, shared with the control. |
label |
string |
undefined |
Renders a <label for={id}>. |
description |
string |
undefined |
Helper text, id ${id}-desc. |
error |
string |
undefined |
Error text in --danger, id ${id}-error, role="alert". |
class / className |
string |
undefined |
Extra classes, appended last. |
Accessibility
Section titled “Accessibility”The error is a live region, so a message that appears after a failed submit is announced without moving focus. Each part renders only when it has content, which keeps the described-by targets honest: aria-describedby never points at an element that is not there.