---
title: Field for Astro
description: "Astro field layouts that connect labels, controls, descriptions, and errors."
seo:
    title: Field for Astro | Shadcn & Tailwind CSS
related:
    - /components/form
    - /components/label
    - /components/input-group
sidebar:
    label: "Field"
---

```astro
---
import { Checkbox } from "@/components/ui/checkbox"
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
  FieldLegend,
  FieldSet,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
---

<FieldSet>
  <FieldLegend>Profile</FieldLegend>
  <FieldDescription>Enter your profile information below.</FieldDescription>
  <FieldGroup>
    <Field>
      <FieldLabel for="name">Full name</FieldLabel>
      <Input type="text" id="name" placeholder="Morgan Reed" />
      <FieldDescription>This appears on invoices and emails.</FieldDescription>
    </Field>
    <Field>
      <FieldLabel for="username">Username</FieldLabel>
      <Input type="text" id="username" placeholder="morganreed" />
    </Field>
    <Field orientation="horizontal">
      <Checkbox id="newsletter" />
      <FieldLabel for="newsletter">Subscribe to the newsletter</FieldLabel>
    </Field>
  </FieldGroup>
</FieldSet>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/field
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/field/field-content.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="field-content"
  class={cn("flex flex-1 flex-col gap-1 leading-snug group/field-content", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/field/field-description.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"p">

const { class: className, ...props } = Astro.props
---

<p
  data-slot="field-description"
  class={cn("text-muted-foreground text-xs [&_a]:underline [&_a]:underline-offset-4", className)}
  {...props}
>
  <slot />
</p>
```

#### src/components/ui/field/field-error.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  errors?: Array<{ message?: string } | undefined>
}

const { class: className, errors, ...props } = Astro.props

const messages = [
  ...new Set(
    (errors ?? [])
      .map((error) => error?.message?.trim())
      .filter((message): message is string => Boolean(message)),
  ),
]

const hasDefaultSlot = Astro.slots.has("default")
const shouldRender = hasDefaultSlot || messages.length > 0
---

{shouldRender && (
  <div
    role="alert"
    data-slot="field-error"
    class={cn("text-destructive-foreground text-xs", className)}
    {...props}
  >
    {hasDefaultSlot ? (
      <slot />
    ) : messages.length === 1 ? (
      messages[0]
    ) : (
      <ul class="ml-4 flex list-disc flex-col gap-1">
        {messages.map((message) => (
          <li>{message}</li>
        ))}
      </ul>
    )}
  </div>
)}
```

#### src/components/ui/field/field-group.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="field-group"
  class={cn("@container/field-group flex w-full flex-col gap-7 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4 group/field-group", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/field/field-label.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"label">

const { class: className, ...props } = Astro.props
---

<label
  data-slot="field-label"
  class={cn("text-foreground has-data-checked:bg-primary/5 has-data-checked:border-primary/30 dark:has-data-checked:border-primary/20 dark:has-data-checked:bg-primary/10 has-[>[data-slot=field]]:not-has-[:disabled,[data-disabled]]:hover:bg-muted/50 has-[>[data-slot=field]]:has-[:focus-visible]:border-ring has-[>[data-slot=field]]:has-[:focus-visible]:ring-ring/50 flex w-fit items-center gap-2 text-base/4.5 font-medium group-data-[disabled=true]/field:opacity-64 has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col has-[>[data-slot=field]]:rounded-md has-[>[data-slot=field]]:border has-[>[data-slot=field]]:has-[:focus-visible]:ring-3 *:data-[slot=field]:p-3 sm:text-sm/4 group/field-label peer/field-label", className)}
  {...props}
>
  <slot />
</label>
```

#### src/components/ui/field/field-legend.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"legend"> & {
  variant?: "legend" | "label"
}

const { class: className, variant = "legend", ...props } = Astro.props
---

<legend
  data-slot="field-legend"
  data-variant={variant}
  class={cn("text-foreground mb-3 font-semibold data-[variant=label]:text-sm data-[variant=legend]:text-base", className)}
  {...props}
>
  <slot />
</legend>
```

#### src/components/ui/field/field-separator.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import { Separator } from "../separator/index.ts"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
const hasContent = Astro.slots.has("default")
---

<div
  data-slot="field-separator"
  data-content={hasContent}
  class={cn("relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2", className)}
  {...props}
>
  <Separator class="absolute inset-0 top-1/2" />
  {hasContent && (
    <span
      data-slot="field-separator-content"
      class="text-muted-foreground bg-background relative mx-auto block w-fit px-2"
    >
      <slot />
    </span>
  )}
</div>
```

#### src/components/ui/field/field-set.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"fieldset">

const { class: className, ...props } = Astro.props
---

<fieldset
  data-slot="field-set"
  class={cn("flex flex-col gap-6 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3", className)}
  {...props}
>
  <slot />
</fieldset>
```

#### src/components/ui/field/field-title.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div data-slot="field-label" class={cn("flex w-fit items-center gap-2 text-sm font-medium group-data-[disabled=true]/field:opacity-50", className)} {...props}>
  <slot />
</div>
```

#### src/components/ui/field/field.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import type { FieldProps } from "./types"
import { cn } from "cn"
import { fieldVariants } from "./field-variants"

type Props = HTMLAttributes<"div"> & FieldProps

const { class: className, orientation = "vertical", ...props } = Astro.props
---

<div
  role="group"
  data-slot="field"
  data-orientation={orientation}
  class={cn(fieldVariants({ orientation }), className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/field/field-variants.ts

```ts
import { cva } from 'class-variance-authority'

export const fieldVariants = cva("font-sans data-[invalid=true]:text-destructive-foreground flex w-full gap-2 group/field", {
    variants: {
        orientation: {
            vertical:
                'flex-col [&>.sr-only]:w-auto [&>:not([role=checkbox]):not([role=radio])]:w-full',
            horizontal:
                'flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px',
            responsive:
                'flex-col @md/field-group:flex-row @md/field-group:items-center @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto [&>:not([role=checkbox]):not([role=radio])]:w-full @md/field-group:[&>:not([role=checkbox]):not([role=radio])]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px',
        },
    },
    defaultVariants: {
        orientation: 'vertical',
    },
})
```

#### src/components/ui/field/types.ts

```ts
import { type VariantProps } from 'class-variance-authority'

import type { fieldVariants } from './field-variants'

export type FieldProps = VariantProps<typeof fieldVariants>
```

#### src/components/ui/field/index.ts

```ts
export { default as Field } from "./field.astro"
export { default as FieldContent } from "./field-content.astro"
export { default as FieldDescription } from "./field-description.astro"
export { default as FieldError } from "./field-error.astro"
export { default as FieldGroup } from "./field-group.astro"
export { default as FieldLabel } from "./field-label.astro"
export { default as FieldLegend } from "./field-legend.astro"
export { default as FieldSeparator } from "./field-separator.astro"
export { default as FieldSet } from "./field-set.astro"
export { default as FieldTitle } from "./field-title.astro"
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import {
    Field,
    FieldContent,
    FieldDescription,
    FieldGroup,
    FieldLabel,
    FieldLegend,
    FieldSeparator,
    FieldSet,
    FieldTitle,
} from "@/components/ui/field"
```

```astro
<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:

```text
Field
├── FieldLabel
├── Input / Textarea / Switch / Select
├── FieldDescription
└── FieldError
```

Use `FieldGroup` to group related fields. Add `FieldSeparator` between sections when needed.

```text
FieldGroup
├── Field
│   ├── FieldLabel
│   ├── Input / Textarea / Switch / Select
│   ├── FieldDescription
│   └── FieldError
├── FieldSeparator
└── Field
    ├── FieldLabel
    └── Input / Textarea / Switch / Select
```

Use `FieldSet` and `FieldLegend` for semantic grouping:

```text
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:

```astro
<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 `FieldContent` when the label needs a supporting description.
- Use `FieldGroup` to arrange several fields. Use `FieldSet` and `FieldLegend` when those fields form a named group.

## Examples

```astro
---
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
---

<div class="w-full max-w-sm">
  <Field>
    <FieldLabel for="field-email">Email</FieldLabel>
    <Input id="field-email" type="email" placeholder="email@example.com" />
    <FieldDescription>
      Use the address where you want to receive project updates.
    </FieldDescription>
  </Field>
</div>
```

```astro
---
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import { Textarea } from "@/components/ui/textarea"
---

<div class="w-full max-w-sm">
  <Field>
    <FieldLabel for="bio">Bio</FieldLabel>
    <Textarea id="bio" placeholder="Tell us about yourself" />
    <FieldDescription>Write a short bio about yourself.</FieldDescription>
  </Field>
</div>
```

```astro
---
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select"
---

<div class="w-full max-w-sm">
  <Field>
    <FieldLabel for="country">Country</FieldLabel>
    <NativeSelect id="country">
      <NativeSelectOption value="">Select a country</NativeSelectOption>
      <NativeSelectOption value="us">United States</NativeSelectOption>
      <NativeSelectOption value="uk">United Kingdom</NativeSelectOption>
      <NativeSelectOption value="ca">Canada</NativeSelectOption>
    </NativeSelect>
    <FieldDescription>Select your country of residence.</FieldDescription>
  </Field>
</div>
```

```astro
---
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
  FieldLegend,
  FieldSet,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
---

<div class="w-full max-w-sm">
  <FieldSet>
    <FieldLegend>Personal Information</FieldLegend>
    <FieldDescription>Enter your personal details below.</FieldDescription>
    <FieldGroup>
      <Field>
        <FieldLabel for="first-name">First name</FieldLabel>
        <Input id="first-name" placeholder="Leila" />
      </Field>
      <Field>
        <FieldLabel for="last-name">Last name</FieldLabel>
        <Input id="last-name" placeholder="Moreno" />
      </Field>
    </FieldGroup>
  </FieldSet>
</div>
```

```astro
---
import { Checkbox } from "@/components/ui/checkbox"
import {
  Field,
  FieldContent,
  FieldDescription,
  FieldLabel,
} from "@/components/ui/field"
---

<div class="w-full max-w-sm">
  <Field orientation="horizontal">
    <Checkbox id="terms" />
    <FieldContent>
      <FieldLabel for="terms">Include review notes</FieldLabel>
      <FieldDescription>
        Include the current review notes with your project summary.
      </FieldDescription>
    </FieldContent>
  </Field>
</div>
```

```astro
---
import {
  Field,
  FieldDescription,
  FieldLabel,
  FieldLegend,
  FieldSet,
} from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---

<div class="w-full max-w-sm">
  <FieldSet>
    <FieldLegend>Notifications</FieldLegend>
    <FieldDescription>Choose how you want to be notified.</FieldDescription>
    <RadioGroup defaultValue="email" name="notifications">
      <Field orientation="horizontal">
        <RadioGroupItem value="email" id="field-notification-email" />
        <FieldLabel for="field-notification-email">Email</FieldLabel>
      </Field>
      <Field orientation="horizontal">
        <RadioGroupItem value="sms" id="field-notification-sms" />
        <FieldLabel for="field-notification-sms">SMS</FieldLabel>
      </Field>
      <Field orientation="horizontal">
        <RadioGroupItem value="push" id="field-notification-push" />
        <FieldLabel for="field-notification-push">Push notification</FieldLabel>
      </Field>
    </RadioGroup>
  </FieldSet>
</div>
```

### Field Group

Place each field inside `FieldGroup`. A `FieldSeparator` can mark a change between sets of related inputs.

```astro
---
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
  FieldSeparator,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
---

<div class="w-full max-w-sm">
  <FieldGroup>
    <Field>
      <FieldLabel for="email-group">Email</FieldLabel>
      <Input id="email-group" type="email" placeholder="email@example.com" />
      <FieldDescription>Your primary email address.</FieldDescription>
    </Field>
    <FieldSeparator>Or</FieldSeparator>
    <Field>
      <FieldLabel for="phone">Phone</FieldLabel>
      <Input id="phone" type="tel" placeholder="+1 (555) 000-0000" />
      <FieldDescription>Your phone number for SMS updates.</FieldDescription>
    </Field>
  </FieldGroup>
</div>
```

## Responsive Layout

- **Vertical fields:** Default orientation stacks label, control, and helper text—ideal for mobile-first layouts.
- **Horizontal fields:** Set `orientation="horizontal"` on `Field` to align the label and control side-by-side. Pair with `FieldContent` to keep descriptions aligned.
- **Responsive fields:** Set `orientation="responsive"` for automatic column layouts inside container-aware parents. Apply `@container/field-group` classes on `FieldGroup` to switch orientations at specific breakpoints.

```astro
---
import { Checkbox } from "@/components/ui/checkbox"
import {
  Field,
  FieldContent,
  FieldDescription,
  FieldGroup,
  FieldLabel,
} from "@/components/ui/field"
---

<div class="w-full max-w-2xl">
  <FieldGroup class="@container/field-group">
    <Field orientation="responsive">
      <Checkbox id="responsive-checkbox" />
      <FieldContent>
        <FieldLabel for="responsive-checkbox">
          Analytics and performance
        </FieldLabel>
        <FieldDescription>
          Help improve the app by sharing anonymous usage data.
        </FieldDescription>
      </FieldContent>
    </Field>
    <Field orientation="responsive">
      <Checkbox id="responsive-marketing" />
      <FieldContent>
        <FieldLabel for="responsive-marketing">Marketing emails</FieldLabel>
        <FieldDescription>
          Receive emails about new products and features.
        </FieldDescription>
      </FieldContent>
    </Field>
  </FieldGroup>
</div>
```

## 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.

```astro
---
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
---

<div class="w-full max-w-sm">
  <Field data-invalid="true">
    <FieldLabel for="error-email">Email</FieldLabel>
    <Input id="error-email" type="email" aria-invalid="true" />
    <FieldDescription class="text-destructive">
      Enter a valid email address.
    </FieldDescription>
    <FieldDescription>
      This email will be used for account notifications.
    </FieldDescription>
  </Field>
</div>
```

## API Reference

### Field

The root component for the Field family.

### FieldProps

<AutoTypeTable path="../../packages/ui/src/components/ui/field/types.ts" name="FieldProps" />
## Accessibility

- `FieldSet` and `FieldLegend` keep related controls grouped for keyboard and assistive tech users.
- `Field` outputs `role="group"` so nested controls inherit labeling from `FieldLabel` and `FieldLegend` when combined.
- Apply `FieldSeparator` sparingly 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.
