FormsMessageForm
Searchastronomic.dev# MessageForm
For a message form that posts to your endpoint with no JavaScript: name, email and message, plus company, phone or a topic if you ask.
Interactive: inline script.
## Usage
```astro
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
```
## Examples
### A plain post
Name, email and message, each with a visible label, then the button, and a privacy line in the note slot. It posts to # here: set action to your endpoint. A plain post is the safe default and works with any endpoint that takes a form post, with no JavaScript.
```astro
---
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
---
<MessageForm class="max-w-xl" action="#">
<p slot="note" class="m-0">We only use your details to answer you. No newsletter, no sharing.</p>
</MessageForm>
```
### Company, phone and topics
company and phone add two optional fields after the email; topics adds a native select before the message, which opens on "Choose a topic" (topicPlaceholder) so no topic is sent until the reader picks one.
```astro
---
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
---
<MessageForm class="max-w-xl" action="#" company phone topics={["A new site", "A redesign", "Support for a site we built"]} messageRows={6} />
```
### Sent in the page
enhance="fetch": the form checks its fields when sent, shows each error under its field and moves focus to the first, then sends in the page and says the result under the button. Here it posts 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 MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
---
<MessageForm class="max-w-xl" action="/__astronomic/form-echo?delay=600" enhance="fetch" topics={["A new site", "A redesign", "Something else"]}>
<p slot="note" class="m-0">We only use your details to answer you. No newsletter, no sharing.</p>
</MessageForm>
```
### Formspree, sent in the page
Formspree's setup: action is your form's endpoint, https://formspree.io/f/<id> (copy the id from your Formspree form; xyzabcde here is made up), and the honeypot is Formspree's own spam trap, _gotcha. Formspree answers JSON to the script (Accept: application/json, with CORS), so enhance="fetch" sends in the page and the status under the button says it was sent. Without the script it is a plain post, and Formspree shows its own thank-you page. On this site the made-up id does not exist, so sending shows the error text.
```astro
---
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
// Your Formspree form's endpoint: https://formspree.io/f/<id>, with the id copied from Formspree. _gotcha is its spam trap.
---
<MessageForm class="max-w-xl" action="https://formspree.io/f/xyzabcde" honeypot={{ name: "_gotcha" }} enhance="fetch">
<p slot="note" class="m-0">We only use your details to answer you. No newsletter, no sharing.</p>
</MessageForm>
```
## Props
### MessageForm
| 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 message"` | 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` | `"Thanks. We got your message and will answer soon."` | FormStatus's text on a 2xx JSON answer or 204 (enhance="fetch"). |
| `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. |
| `nameName` | `string` | `"name"` | The name field's name, as your endpoint wants it. |
| `emailName` | `string` | `"email"` | The email field's name, such as EMAIL for Mailchimp. |
| `messageName` | `string` | `"message"` | The message field's name. |
| `companyName` | `string` | `"company"` | The company field's name (with company). |
| `phoneName` | `string` | `"phone"` | The phone field's name (with phone). |
| `topicName` | `string` | `"topic"` | The topic select's name (with topics). |
| `nameLabel` | `string` | `"Name"` | The name field's visible label. |
| `emailLabel` | `string` | `"Email"` | The email field's visible label. |
| `messageLabel` | `string` | `"Message"` | The message field's visible label. |
| `companyLabel` | `string` | `"Company"` | The company field's visible label. |
| `phoneLabel` | `string` | `"Phone"` | The phone field's visible label. |
| `topicLabel` | `string` | `"Topic"` | The topic select's visible label. |
| `topicPlaceholder` | `string` | `"Choose a topic"` | The topic select's first option, empty and selected until the reader picks a topic. A reader who never picks one sends an empty topic, not the first in your list. The topic stays optional. |
| `company` | `boolean` | `false` | Ask for a company, after the email (optional to fill, autocomplete organization). |
| `phone` | `boolean` | `false` | Ask for a phone number, after the email (optional to fill, type tel, autocomplete tel). |
| `topics` | `string[]` | `[]` | The topics for a native select, before the message, such as ["Sales", "Support"]. Without them, no select. |
| `messageRows` | `number` | `5` | The message box's height in lines. |
| `fieldErrors` | `{ name?: string; email?: string; message?: string }` | `{}` | The text shown under a required field that is empty or wrong, with enhance="fetch", in your page's language. Defaults: "Enter your name.", "Enter an email address, like name@example.com." and "Enter a message.". Each goes on its control as data-error; with no script the browser shows its own message. |
| `class` | `string` | None | More classes for the <form>, merged with cn(), so yours win on conflicts, such as a reading width (max-w-xl). |
| Slot | Description |
| --- | --- |
| `extra` | After the fields, before the button: more fields or a widget, such as a Turnstile check. Keep submit 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.
## Accessibility
| Key | Action |
| --- | --- |
| Enter | In a single-line field, submits the form; in the message box, starts a new line |
| Tab | Moves through the fields, the button, then any links in the note, then out of the form; the status is skipped |
- Every field has a visible label (Field) and its autocomplete value (name, email, organization, tel), so the browser can fill it. Name, email and message are required and marked so.
- With no script, or before it loads, the form posts to its action and the browser checks the required fields and the email itself.
- The topic is a native <select>: its first option is empty and selected, so a reader who never picks one sends no topic. It works with the keyboard and screen readers as any select does.
- With enhance="fetch", a bad field gets its error text under it, aria-invalid and the error id in aria-describedby, and focus moves to the first bad field. The FormStatus under the button says sending, then success or the error.
- The button is never disabled: while sending it has aria-busy, and a second click or Enter is ignored.
MessageForm
FreeFor a message form that posts to your endpoint with no JavaScript: name, email and message, plus company, phone or a topic if you ask.
- File
- MessageForm.astro
- Group
- Forms
- Kind
- Interactive: inline script
- Examples
- 4
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
A plain post
Name, email and message, each with a visible label, then the button, and a privacy line in the note slot. It posts to # here: set action to your endpoint. A plain post is the safe default and works with any endpoint that takes a form post, with no JavaScript.
This preview did not load.
examples/messageform/main.astro
---
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
---
<MessageForm class="max-w-xl" action="#">
<p slot="note" class="m-0">We only use your details to answer you. No newsletter, no sharing.</p>
</MessageForm>Company, phone and topics
company and phone add two optional fields after the email; topics adds a native select before the message, which opens on "Choose a topic" (topicPlaceholder) so no topic is sent until the reader picks one.
This preview did not load.
examples/messageform/topics.astro
---
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
---
<MessageForm class="max-w-xl" action="#" company phone topics={["A new site", "A redesign", "Support for a site we built"]} messageRows={6} />Sent in the page
enhance="fetch": the form checks its fields when sent, shows each error under its field and moves focus to the first, then sends in the page and says the result under the button. Here it posts 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/messageform/enhanced.astro
---
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
---
<MessageForm class="max-w-xl" action="/__astronomic/form-echo?delay=600" enhance="fetch" topics={["A new site", "A redesign", "Something else"]}>
<p slot="note" class="m-0">We only use your details to answer you. No newsletter, no sharing.</p>
</MessageForm>Formspree, sent in the page
Formspree's setup: action is your form's endpoint, https://formspree.io/f/<id> (copy the id from your Formspree form; xyzabcde here is made up), and the honeypot is Formspree's own spam trap, _gotcha. Formspree answers JSON to the script (Accept: application/json, with CORS), so enhance="fetch" sends in the page and the status under the button says it was sent. Without the script it is a plain post, and Formspree shows its own thank-you page. On this site the made-up id does not exist, so sending shows the error text.
This preview did not load.
examples/messageform/formspree.astro
---
import MessageForm from "@astronomic-dev/ui/components/MessageForm.astro";
// Your Formspree form's endpoint: https://formspree.io/f/<id>, with the id copied from Formspree. _gotcha is its spam trap.
---
<MessageForm class="max-w-xl" action="https://formspree.io/f/xyzabcde" honeypot={{ name: "_gotcha" }} enhance="fetch">
<p slot="note" class="m-0">We only use your details to answer you. No newsletter, no sharing.</p>
</MessageForm>