Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Field for Astro

Astro field layouts that connect labels, controls, descriptions, and errors.

Profile

Enter your profile information below.

This appears on invoices and emails.

Installation

bunx --bun shadcn@latest add @gnostic/field
npx shadcn@latest add @gnostic/field
pnpm dlx shadcn@latest add @gnostic/field

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/field/field-content.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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
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
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
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"

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

Use the address where you want to receive project updates.

Write a short bio about yourself.

Select your country of residence.

Personal Information

Enter your personal details below.

Include the current review notes with your project summary.

Notifications

Choose how you want to be notified.

Field Group

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

Your primary email address.

Or

Your phone number for SMS updates.

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.

Help improve the app by sharing anonymous usage data.

Receive emails about new products and features.

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.

Enter a valid email address.

This email will be used for account notifications.

API Reference

Field

The root component for the Field family.

FieldProps

PropType
orientation?"vertical" | "horizontal" | "responsive" | null | undefined
Type"vertical" | "horizontal" | "responsive" | null | undefined

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.