FormsForm
Searchastronomic.dev# Form
To build your own form that posts with no JavaScript: hidden fields, a spam trap, the send button and an optional in-page status. MessageForm and SignupForm are built on it.
Interactive: inline script.
## Usage
```astro
import Form from "@astronomic-dev/ui/components/Form.astro";
```
## Examples
### A plain post
Name and email Fields in the default slot and a privacy line in the note slot; the button and its status come after the fields. It posts to # here. The form is generic: any endpoint that takes a form post works through action, the field names and hiddenFields, with no provider built in. Buttondown, Mailchimp and Formspree are checked examples. A plain post is the safe default.
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Form from "@astronomic-dev/ui/components/Form.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
---
<Form class="max-w-md" action="#" submitLabel="Send">
<Field label="Name" for="form-main-name" required>
<Input id="form-main-name" name="name" autocomplete="name" required />
</Field>
<Field label="Email" for="form-main-email" required>
<Input id="form-main-email" name="email" type="email" autocomplete="email" spellcheck="false" required />
</Field>
<p slot="note" class="m-0">We only use your email to answer you. No newsletter, no sharing.</p>
</Form>
```
### Sent in the page
Sends in the page to the directory's test endpoint, which exists only in development and in tests; on this site, with no endpoint behind it, the preview answers in the page, so it says it was sent and nothing is kept. A real endpoint must answer JSON.
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Form from "@astronomic-dev/ui/components/Form.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
const nameError = "Enter your name.";
const emailError = "Enter an email address, like name@example.com.";
---
<Form class="max-w-md" action="/__astronomic/form-echo?delay=600" enhance="fetch" submitLabel="Send">
<Field label="Name" for="form-enhanced-name" required errorSlot>
<Input id="form-enhanced-name" name="name" autocomplete="name" required data-error={nameError} />
</Field>
<Field label="Email" for="form-enhanced-email" required errorSlot>
<Input
id="form-enhanced-email"
name="email"
type="email"
autocomplete="email"
spellcheck="false"
required
data-error={emailError}
/>
</Field>
<p slot="note" class="m-0">We only use your email to answer you. No newsletter, no sharing.</p>
</Form>
```
## Props
### Form
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `action` | `string` | None | Required. The URL the form posts to. Any endpoint that takes a form post works; copy it from your provider's embed code as it is (Mailchimp's keeps its query, u, id and f_id). |
| `method` | `"post" \| "get"` | `"post"` | How the form sends. Only post is enhanced. |
| `enctype` | `"application/x-www-form-urlencoded" \| "multipart/form-data"` | None | How a post is encoded: URL-encoded (the browser's default) or multipart, for files. Not used with get. |
| `hiddenFields` | `HiddenField[]` | `[]` | Extra fields the endpoint wants, sent as <input type="hidden">: { name, value }[], such as Buttondown's embed=1 and a tag. They come first in the body, in order; a name may repeat. Not `hidden`, which is the HTML attribute. |
| `honeypot` | `Honeypot` | None | The spam trap, off by default: { name }. One empty text field real people never see; the endpoint rejects posts that fill it. Use the provider's own name: Mailchimp's b_<u>_<id>, Formspree's _gotcha. Avoid names autofill knows (email, name, url). |
| `submitLabel` | `string` | `"Send"` | The button text. The button is always a submit button. |
| `submitVariant` | `"primary" \| "secondary" \| "ghost"` | `"primary"` | The submit Button's variant, Button's own. Keep primary when the form is the main action; use secondary when another action near it is the main one, such as a sign-in form under a paywall's Subscribe Button. It takes the form's tone. |
| `submitIconTile` | `IconComponent` | None | Off by default. The submit Button's iconTile: a tile at the end of the pill that holds your icon component (an arrow), the inverted pair of the button's colors, its corners nested in the pill's (--ui-radius-pill). The icon is decoration and moves on hover and focus where motion is allowed (submitTileMotion). |
| `submitTileMotion` | `"turn" \| "slide"` | `"turn"` | The submit Button's tileMotion, with submitIconTile: turn turns the icon on hover and focus; slide moves it out of its tile while a copy moves in. Still under reduced motion or the theme's calm setting. |
| `submitRoll` | `boolean` | `false` | The submit Button's roll: on hover and keyboard focus the label rolls up and the same words roll in from below, read once. Off under reduced motion or the theme's calm setting. |
| `enhance` | `"none" \| "fetch"` | `"none"` | none: a plain post, the safe default for any endpoint. fetch: Form's script sends in the page and says the result in the FormStatus. fetch needs a same-origin or CORS-enabled endpoint that answers JSON with a real status code (Formspree does, with Accept: application/json). A cross-origin endpoint without CORS still receives the post, but the page cannot read the answer, so it shows the error text. Success is a 2xx JSON answer, or 204 No Content; a provider that answers an HTML page, even with 200, reads as an error. Buttondown and Mailchimp must use a plain post. Only method="post" is enhanced. |
| `sendingText` | `string` | None | FormStatus's text while sending (enhance="fetch"). Default "Sending…". |
| `successText` | `string` | None | FormStatus's text on a 2xx JSON answer or 204 (enhance="fetch"). Default "Thanks. It has been sent." |
| `errorText` | `string` | None | FormStatus's text on any other answer or no network (enhance="fetch"). Default "It did not send. Check your connection and try again." |
| `target` | `string` | None | Passed to <form>, such as _blank to open the provider's own page in a new tab. |
| `tone` | `"page" \| "band"` | `"page"` | The surface: page, or band for a surface-band area, where the Button, the status and every focus ring take the band's colors. Give each Field the same tone. |
| `fieldsClass` | `string` | None | For form parts. Classes for the box that holds the fields, the extra slot and the button row, such as an inline layout. |
| `actionClass` | `string` | None | For form parts. Classes for the button row (the Button and the FormStatus). |
| `buttonClass` | `string` | None | For form parts. Classes for the submit Button, such as a top margin that sets it level with a labeled field in an inline row (mt-field-label). |
| `statusClass` | `string` | None | For form parts. Classes for the FormStatus under the Button, such as a full-width line under an inline row (with the button row as contents). |
| `class` | `string` | None | More classes for the <form>, merged with cn(), so yours win on conflicts. |
| Slot | Description |
| --- | --- |
| `default` | The fields: Fields with their controls, each with its name, autocomplete and, for fetch, errorSlot and data-error. |
| `extra` | After the fields, before the button row: more fields or a widget, such as a Turnstile check. A submit button here comes before the main one, so Enter submits with it: keep extra buttons out of it. |
| `note` | Under the form, small and muted: a privacy line or consent text. |
Any other attribute goes on the <form>, such as id or aria-label. A data-enhance passed in is dropped: only the enhance prop turns the script on.
## Accessibility
| Key | Action |
| --- | --- |
| Enter | In a field, submits the form |
| Tab | Moves through the fields, the button, then any links in the note, then out of the form; the status is skipped |
- A plain <form>: with no script, or before it loads, it posts to action and the browser checks required and type=email fields itself.
- The button is always a submit button (type="submit"), so Enter in a field sends the form.
- The honeypot sits in a box hidden from screen readers (aria-hidden), visually hidden, out of the tab order (tabindex=-1), with autocomplete off: nobody reaches it.
- With enhance="fetch", give each Field errorSlot and each control its error text in data-error. On a bad field the error goes into that Field's error node, the control gets aria-invalid and the error id in aria-describedby, and focus moves to the first bad field, which announces it. Nothing shows before the first submit.
- With enhance="fetch", the FormStatus beside the button is a polite live region: it says sending, then success or the error. On success focus moves to it, the form resets and every error state clears; on an error the text the user typed stays.
- The button is never disabled: while sending it has aria-busy, and a second click or Enter is ignored.
- Give every field a visible label (Field), never a placeholder as the label, and its autocomplete value.
Form
FreeTo build your own form that posts with no JavaScript: hidden fields, a spam trap, the send button and an optional in-page status. MessageForm and SignupForm are built on it.
- File
- Form.astro
- Group
- Forms
- Kind
- Interactive: inline script
- Examples
- 2
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
A plain post
Name and email Fields in the default slot and a privacy line in the note slot; the button and its status come after the fields. It posts to # here. The form is generic: any endpoint that takes a form post works through action, the field names and hiddenFields, with no provider built in. Buttondown, Mailchimp and Formspree are checked examples. A plain post is the safe default.
This preview did not load.
examples/form/main.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Form from "@astronomic-dev/ui/components/Form.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
---
<Form class="max-w-md" action="#" submitLabel="Send">
<Field label="Name" for="form-main-name" required>
<Input id="form-main-name" name="name" autocomplete="name" required />
</Field>
<Field label="Email" for="form-main-email" required>
<Input id="form-main-email" name="email" type="email" autocomplete="email" spellcheck="false" required />
</Field>
<p slot="note" class="m-0">We only use your email to answer you. No newsletter, no sharing.</p>
</Form>Sent in the page
Sends in the page to the directory's test endpoint, which exists only in development and in tests; on this site, with no endpoint behind it, the preview answers in the page, so it says it was sent and nothing is kept. A real endpoint must answer JSON.
This preview did not load.
examples/form/enhanced.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Form from "@astronomic-dev/ui/components/Form.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
const nameError = "Enter your name.";
const emailError = "Enter an email address, like name@example.com.";
---
<Form class="max-w-md" action="/__astronomic/form-echo?delay=600" enhance="fetch" submitLabel="Send">
<Field label="Name" for="form-enhanced-name" required errorSlot>
<Input id="form-enhanced-name" name="name" autocomplete="name" required data-error={nameError} />
</Field>
<Field label="Email" for="form-enhanced-email" required errorSlot>
<Input
id="form-enhanced-email"
name="email"
type="email"
autocomplete="email"
spellcheck="false"
required
data-error={emailError}
/>
</Field>
<p slot="note" class="m-0">We only use your email to answer you. No newsletter, no sharing.</p>
</Form>