Field for Astro
Astro field layouts that connect labels, controls, descriptions, and errors.
Installation
bunx --bun shadcn@latest add @gnostic/fieldnpx shadcn@latest add @gnostic/fieldpnpm dlx shadcn@latest add @gnostic/fieldCopy the files
src/components/ui/field/field-content.astrosrc/components/ui/field/field-description.astrosrc/components/ui/field/field-error.astrosrc/components/ui/field/field-group.astrosrc/components/ui/field/field-label.astrosrc/components/ui/field/field-legend.astrosrc/components/ui/field/field-separator.astrosrc/components/ui/field/field-set.astrosrc/components/ui/field/field-title.astrosrc/components/ui/field/field.astrosrc/components/ui/field/field-variants.tssrc/components/ui/field/types.tssrc/components/ui/field/index.ts
Usage
import {
Field,
FieldContent,
FieldDescription,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSeparator,
FieldSet,
FieldTitle,
} from "@/components/ui/field"
<FieldSet>
<FieldLegend>Profile</FieldLegend>
<FieldGroup>
<Field>
<FieldLabel for="name">Full name</FieldLabel>
<Input id="name" placeholder="Morgan Reed" />
<FieldDescription>Optional helper text.</FieldDescription>
</Field>
</FieldGroup>
</FieldSet>
Composition
Use the following composition for a single field with a label, control, helper text, and validation:
Field
├── FieldLabel
├── Input / Textarea / Switch / Select
├── FieldDescription
└── FieldError
Use FieldGroup to group related fields. Add FieldSeparator between sections when needed.
FieldGroup
├── Field
│ ├── FieldLabel
│ ├── Input / Textarea / Switch / Select
│ ├── FieldDescription
│ └── FieldError
├── FieldSeparator
└── Field
├── FieldLabel
└── Input / Textarea / Switch / Select
Use FieldSet and FieldLegend for semantic grouping:
FieldSet
├── FieldLegend
├── FieldDescription
└── FieldGroup
├── Field
│ ├── FieldLabel
│ ├── Input / Textarea / Switch / Select
│ ├── FieldDescription
│ └── FieldError
└── Field
├── FieldLabel
└── Input / Textarea / Switch / Select
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, Checkbox, etc. -->
<FieldDescription>Optional helper text.</FieldDescription>
</Field>
- Put one control and its label in a
Field. - Add
FieldContentwhen the label needs a supporting description. - Use
FieldGroupto arrange several fields. UseFieldSetandFieldLegendwhen those fields form a named group.
Examples
Field Group
Place each field inside FieldGroup. A FieldSeparator can mark a change between sets of related inputs.
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
Add data-invalid="true" to Field to switch the entire block into an error state, and add aria-invalid="true" on the input itself for assistive technologies.
API Reference
Field
The root component for the Field family.
FieldProps
orientation?"vertical" | "horizontal" | "responsive" | null | undefined
"vertical" | "horizontal" | "responsive" | null | undefinedAccessibility
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.
When to use Field
Use Field to keep a label, control, help text, and error together. Match label for to the control ID and connect description and error IDs with aria-describedby. FieldSet and FieldLegend group related controls using native fieldset markup.
Keyboard and behavior
These parts arrange form content; they do not run Base UI validation. Native constraints and your server or client validation determine the error. Set aria-invalid on the control and show an error that tells the user how to correct the value.
Theme and layout
Use the existing orientation variants to place labels beside or above controls. Check long help text in narrow columns. Shared error and muted-text tokens keep the state consistent with Input and Textarea.