import { Button } from "~/components/ui/button"
import { Checkbox } from "~/components/ui/checkbox"
import {
Field,
FieldDescription,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSeparator,
FieldSet
} from "~/components/ui/field"
import { Input } from "~/components/ui/input"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from "~/components/ui/select"
import { Textarea } from "~/components/ui/textarea"
export function FieldDemo() {
return (
<div class="w-full max-w-md">
<form>
<FieldGroup>
<FieldSet>
<FieldLegend>Payment Method</FieldLegend>
<FieldDescription>All transactions are secure and encrypted</FieldDescription>
<FieldGroup>
<Field>
<FieldLabel for="checkout-7j9-card-name-43j">Name on Card</FieldLabel>
<Input id="checkout-7j9-card-name-43j" placeholder="Evil Rabbit" required />
</Field>
<Field>
<FieldLabel for="checkout-7j9-card-number-uw1">Card Number</FieldLabel>
<Input
id="checkout-7j9-card-number-uw1"
placeholder="1234 5678 9012 3456"
required
/>
<FieldDescription>Enter your 16-digit card number</FieldDescription>
</Field>
<div class="grid grid-cols-3 gap-4">
<Field>
<FieldLabel for="checkout-exp-month-ts6">Month</FieldLabel>
<Select
defaultValue=""
itemComponent={(props) => (
<SelectItem item={props.item}>{props.item.rawValue}</SelectItem>
)}
options={[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
placeholder="MM"
>
<SelectTrigger id="checkout-exp-month-ts6">
<SelectValue<string>>{(state) => state.selectedOption()}</SelectValue>
</SelectTrigger>
<SelectContent />
</Select>
</Field>
<Field>
<FieldLabel for="checkout-7j9-exp-year-f59">Year</FieldLabel>
<Select
defaultValue=""
itemComponent={(props) => (
<SelectItem item={props.item}>{props.item.rawValue}</SelectItem>
)}
options={[2024, 2025, 2026, 2027, 2028, 2029]}
placeholder="YYYY"
>
<SelectTrigger id="checkout-7j9-exp-year-f59">
<SelectValue<string>>{(state) => state.selectedOption()}</SelectValue>
</SelectTrigger>
<SelectContent />
</Select>
</Field>
<Field>
<FieldLabel for="checkout-7j9-cvv">CVV</FieldLabel>
<Input id="checkout-7j9-cvv" placeholder="123" required />
</Field>
</div>
</FieldGroup>
</FieldSet>
<FieldSeparator />
<FieldSet>
<FieldLegend>Billing Address</FieldLegend>
<FieldDescription>
The billing address associated with your payment method
</FieldDescription>
<FieldGroup>
<Field orientation="horizontal">
<Checkbox defaultChecked id="checkout-7j9-same-as-shipping-wgm" />
<FieldLabel class="font-normal" for="checkout-7j9-same-as-shipping-wgm">
Same as shipping address
</FieldLabel>
</Field>
</FieldGroup>
</FieldSet>
<FieldSet>
<FieldGroup>
<Field>
<FieldLabel for="checkout-7j9-optional-comments">Comments</FieldLabel>
<Textarea
class="resize-none"
id="checkout-7j9-optional-comments"
placeholder="Add any additional comments"
/>
</Field>
</FieldGroup>
</FieldSet>
<Field orientation="horizontal">
<Button type="submit">Submit</Button>
<Button type="button" variant="outline">
Cancel
</Button>
</Field>
</FieldGroup>
</form>
</div>
)
}
Usage
import { Field, FieldContent, FieldDescription, FieldError, FieldGroup, FieldLabel, FieldLegend, FieldSeparator, FieldSet, FieldTitle,} from "~/components/ui/field";<FieldSet> <FieldLegend>Profile</FieldLegend> <FieldDescription>This appears on invoices and emails.</FieldDescription> <FieldGroup> <Field> <FieldLabel for="name">Full name</FieldLabel> <Input id="name" autoComplete="off" placeholder="Evil Rabbit" /> <FieldDescription>This appears on invoices and emails.</FieldDescription> </Field> <Field> <FieldLabel for="username">Username</FieldLabel> <Input id="username" autoComplete="off" aria-invalid /> <FieldError>Choose another username.</FieldError> </Field> <Field orientation="horizontal"> <Switch id="newsletter" /> <FieldLabel for="newsletter">Subscribe to the newsletter</FieldLabel> </Field> </FieldGroup></FieldSet>Anatomy
The Field family is designed for composing accessible forms. A typical field is structured as follows:
<Field> <FieldLabel for="input-id">Label</FieldLabel> {/* Input, Select, Switch, etc. */} <FieldDescription>Optional helper text.</FieldDescription> <FieldError>Validation message.</FieldError></Field>Fieldis the core wrapper for a single field.FieldContentis a flex column that groups label and description. Not required if you have no description.- Wrap related fields with
FieldGroup, and useFieldSetwithFieldLegendfor semantic grouping.
Examples
Input
Textarea
Select
Slider
Fieldset
Checkbox
Radio
Switch
Choice Card
Field Group
Responsive Layout
- Vertical fields: Default orientation stacks label, control, and helper text—ideal for mobile-first layouts.
- Horizontal fields: Set
orientation="horizontal"onFieldto align the label and control side-by-side. Pair withFieldContentto keep descriptions aligned. - Responsive fields: Set
orientation="responsive"for automatic column layouts inside container-aware parents. Apply@container/field-groupclasses onFieldGroupto switch orientations at specific breakpoints.
Validation and Errors
- Add
data-invalidtoFieldto switch the entire block into an error state. - Add
aria-invalidon the input itself for assistive technologies. - Render
FieldErrorimmediately after the control or insideFieldContentto keep error messages aligned with the field.
<Field data-invalid> <FieldLabel htmlFor="email">Email</FieldLabel> <Input id="email" type="email" aria-invalid /> <FieldError>Enter a valid email address.</FieldError></Field>Accessibility
FieldSetandFieldLegendkeep related controls grouped for keyboard and assistive tech users.Fieldoutputsrole="group"so nested controls inherit labeling fromFieldLabelandFieldLegendwhen combined.- Apply
FieldSeparatorsparingly to ensure screen readers encounter clear section boundaries.
API Reference
FieldSet
Container that renders a semantic fieldset with spacing presets.
<FieldSet> <FieldLegend>Delivery</FieldLegend> <FieldGroup>{/* Fields */}</FieldGroup></FieldSet>FieldLegend
Legend element for a FieldSet. Switch to the label variant to align with label sizing.
<FieldLegend variant="label">Notification Preferences</FieldLegend>The FieldLegend has two variants: legend and label. The label variant applies label sizing and alignment. Handy if you have nested FieldSet.
FieldGroup
Layout wrapper that stacks Field components and enables container queries for responsive orientations.
<FieldGroup className="@container/field-group flex flex-col gap-6"> <Field>{/* ... */}</Field> <Field>{/* ... */}</Field></FieldGroup>Field
The core wrapper for a single field. Provides orientation control, invalid state styling, and spacing.
<Field orientation="horizontal"> <FieldLabel htmlFor="remember">Remember me</FieldLabel> <Switch id="remember" /></Field>FieldContent
Flex column that groups control and descriptions when the label sits beside the control. Not required if you have no description.
<Field> <Checkbox id="notifications" /> <FieldContent> <FieldLabel htmlFor="notifications">Notifications</FieldLabel> <FieldDescription>Email, SMS, and push options.</FieldDescription> </FieldContent></Field>FieldLabel
Label styled for both direct inputs and nested Field children.
<FieldLabel htmlFor="email">Email</FieldLabel>FieldTitle
Renders a title with label styling inside FieldContent.
<FieldContent> <FieldTitle>Enable Touch ID</FieldTitle> <FieldDescription>Unlock your device faster.</FieldDescription></FieldContent>FieldDescription
Helper text slot that automatically balances long lines in horizontal layouts.
<FieldDescription>We never share your email with anyone.</FieldDescription>FieldSeparator
Visual divider to separate sections inside a FieldGroup. Accepts optional inline content.
<FieldSeparator>Or continue with</FieldSeparator>FieldError
Accessible error container that accepts children or an errors array (e.g., from formisch).
<FieldError errors={errors.username} />When the errors array contains multiple messages, the component renders a list automatically.
FieldError also accepts issues produced by any validator that implements Standard Schema, including Zod, Valibot, and ArkType. Pass the issues array from the schema result directly to render a unified error list across libraries.