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

Form for Astro

Compose Astro forms with labels, inputs, validation messages, and buttons. See examples and install the source with the shadcn CLI.

Form is intentionally small. It is just a styled <form> element with vertical spacing so you can compose regular Astro forms with Field, Input, Textarea, Checkbox, NativeSelect, and Button.

Usage

import { Form } from "@/components/ui/form"
---
import { Button } from "@/components/ui/button"
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
} from "@/components/ui/field"
import { Form } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
---

<Form method="post">
  <FieldGroup>
    <Field>
      <FieldLabel for="name">Name</FieldLabel>
      <Input id="name" name="name" />
    </Field>
    <Field>
      <FieldLabel for="message">Message</FieldLabel>
      <Textarea id="message" name="message" />
      <FieldDescription>Tell us what you are building.</FieldDescription>
    </Field>
  </FieldGroup>

  <Button type="submit">Send</Button>
</Form>

Example

These are regular Astro form controls.

Installation

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

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

No extra packages are required.

Copy the files

src/components/ui/form/form.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"form">

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

<form data-slot="form" class={cn("flex flex-col gap-6", className)} {...props}>
  <slot />
</form>
src/components/ui/form/index.ts
export { default as Form } from "./form.astro"

Notes

  • Use plain Astro form attributes like method, action, and netlify directly on Form.
  • Use Field and FieldGroup for structure instead of generated form schemas.
  • If you do not need the default spacing, use a plain <form> element instead.

API Reference

Read the installed source for the complete prop types.

When to use Form

Form renders a real form. Set its method and action for your server endpoint or Astro action, then compose Field and native controls inside it. Give submitted controls names and choose an explicit submit button.

Keyboard and behavior

The wrapper does not validate business rules, send requests, or manage pending state. Native browser constraints still apply. Return field errors from the server and retain entered values when a submission fails.

Theme and layout

Spacing comes from the wrapper; controls use their own shared tokens. Avoid nested forms. A form can contain several fieldsets without changing how values are submitted.