0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Build a contact form that validates, explains its errors and confirms a send. You start with a native form that already works with no JavaScript, and each step adds one layer on top without taking that away.
About 15 minutes · Uses all four layers: semantic HTML, layout attributes, one custom element (<form-field>) and one web component (<toast-msg>).
The finished form. Press Send message while it is empty, then fill it in and send again. Nothing is posted anywhere.
Before you start
Create a folder with one file in it, index.html, and paste in this starter. Open it in a browser, and reload after each step.
That is the stylesheet and one line of CSS, which keeps the form off the edge of the window using a Vanilla Breeze spacing token. It is the only CSS you will write. The script tag is added in step 4, the only step that needs it. For npm and self-hosted installs, see the Quick Start.
Step 1 — Semantic HTML: a form the browser can validate
HTML already covers most of what a contact form needs. Each control gets a <label> tied to it with for and id, so clicking the label focuses the control and a screen reader announces its name. Each control also gets a name, which labels its value for the server.
The validation rules are attributes too. required blocks an empty submit, type="email" rejects text that is not an address (and brings up the email keyboard on a phone), and minlength="20" asks for more than "hi". The first <option> has an empty value, which is what lets required work on the <select>. The two autocomplete values let the browser fill in the name and email.
Replace the comment in <body> with this:
Step 1: the whole form
<main><formaria-labelledby="contact-title"><header><h1id="contact-title">Get in touch</h1><p>Questions about an order, a return or anything else.</p></header><p><labelfor="contact-name">Full name</label><inputid="contact-name"name="name"type="text"autocomplete="name"required/></p><p><labelfor="contact-email">Email</label><inputid="contact-email"name="email"type="email"autocomplete="email"required/></p><p><labelfor="contact-topic">Topic</label><selectid="contact-topic"name="topic"required><optionvalue="">Choose a topic…</option><optionvalue="order">An order</option><optionvalue="return">A return</option><optionvalue="other">Something else</option></select></p><p><labelfor="contact-message">Message</label><textareaid="contact-message"name="message"rows="5"minlength="20"required></textarea></p><p><label><inputtype="checkbox"name="consent"required/> I agree to my details being stored so you can reply</label></p><footer><buttontype="submit">Send message</button><p><small>We reply within one business day.</small></p></footer></form></main>
The <form> has no action or method yet. That is deliberate, so the examples on this page have nowhere to post: a valid submit only reloads the frame with the values in the address. When you have a server endpoint, add it, for example action="/contact" method="post".
Check
Press Send message with the form empty. The browser stops the submit, focuses Full name and shows its own message there.
Type ada in Email, fill in the rest and submit. The browser asks for a full address.
Click a label. Its control takes focus.
It works, but the form runs the full width of the window and the spacing is uneven. That is step 2.
Step 2 — Layout attributes: one readable column
The decision here is to keep the form in a single column. Two fields side by side save a little height, but people fill forms in from top to bottom, and one column reads the same on a phone and on a desktop. So the layout work is small: limit the width and even out the vertical spacing. Attributes on four elements you already have do that. No element is added or removed.
Step 2: the four changed opening tags
<maindata-layout="center"data-layout-max="narrow"><formaria-labelledby="contact-title"data-layout="stack"data-layout-gap="m"><headerdata-layout="stack"data-layout-gap="xs">…</header><!-- the five <p> groups, unchanged --><footerdata-layout="cluster"data-layout-gap="m"data-layout-align="center">…</footer></form></main>
center with data-layout-max="narrow" turns <main> into a centered column that stops growing at a comfortable width.
stack on the <form> spaces its children evenly. The gap now comes from one place, data-layout-gap, instead of each element's own margins.
stack with a smaller gap on the <header> keeps the heading and its sentence together.
cluster on the <footer> puts the button and the note on one line and lets the note wrap underneath when there is no room.
What changed
Make the window narrow: the column shrinks with it, and the note drops below the button.
Validation is exactly as it was. Layout attributes are CSS only.
Step 3 — Custom elements: <form-field> for hints and errors
The browser's own error message appears on one field at a time, so a reader with three mistakes finds them one submit after another. And you cannot choose the words.
<form-field> is the Vanilla Breeze element for one label, one control and its messages. At this step it is CSS only. Replace each <p> wrapper from step 1 with a <form-field> and add the messages as <output> elements: class="hint" for help that is always visible, class="error" for text that stays hidden until the control is invalid. Those two classes are part of <form-field>'s markup; they are the only classes in this form.
Step 3: three of the five fields
<form-field><labelfor="contact-email">Email</label><inputid="contact-email"name="email"type="email"autocomplete="email"requiredaria-describedby="contact-email-hint contact-email-error"/><outputid="contact-email-hint"class="hint"for="contact-email">We only use it to reply to you.</output><outputid="contact-email-error"class="error"for="contact-email"aria-live="polite">Enter an email address, like [email protected].</output></form-field><form-field><labelfor="contact-message">Message</label><textareaid="contact-message"name="message"rows="5"minlength="20"requiredaria-describedby="contact-message-hint contact-message-error"></textarea><outputid="contact-message-hint"class="hint"for="contact-message">At least 20 characters.</output><outputid="contact-message-error"class="error"for="contact-message"aria-live="polite">Write a message of at least 20 characters.</output></form-field><form-fielddata-type="checkbox"><label><inputid="contact-consent"type="checkbox"name="consent"requiredaria-describedby="contact-consent-error"/> I agree to my details being stored so you can reply</label><outputid="contact-consent-error"class="error"for="contact-consent"aria-live="polite">Tick the box so we can reply.</output></form-field>
Full name and Topic follow the same pattern with an error and no hint: "Enter your name." and "Choose a topic." The checkbox gains an id for its error to point at, and data-type="checkbox" lays the label out beside the box.
aria-describedby on the control lists the ids of its hint and error, so a screen reader reads them along with the label. aria-live="polite" on the error announces it when it appears. The error is shown with the :user-invalid selector, which only matches after the reader has edited the control or tried to submit, so an untouched form shows no errors.
Check
Required fields now have a red asterisk after the label. You did not write it: <form-field> adds it wherever it finds a required control.
Submit the empty form. Every field shows its own message at once. The browser's message still appears on the first field as well; step 4 takes over from it.
Type ada in Email and press Tab. The error shows as you leave the field. Finish the address and the field turns green and its hint folds away.
There is still no script on the page. One limit follows from that: each field has a single error text, so Email says the same thing whether it is empty or mistyped.
Step 4 — Web components: <toast-msg> confirms the send
The test for adding a component is whether the page needs behaviour that HTML and CSS cannot provide. This form does, twice: confirming a send without leaving the page, and choosing an error message by the kind of mistake. So this is the step where the script goes in. Add it to your <head>, under the stylesheet:
Then make three changes to the page: two attributes on the <form>, specific messages on two controls, and a <toast-msg> with a few lines of script after </main>.
Step 4: the changes
<formaria-labelledby="contact-title"data-layout="stack"data-layout-gap="m"data-validatedata-submit="event"><!-- Email: two more attributes on the input --><inputid="contact-email"name="email"type="email"autocomplete="email"requiredaria-describedby="contact-email-hint contact-email-error"data-message-valuemissing="Enter your email address."data-message-typemismatch="That does not look like an email address. Check for a missing @."/><!-- Message: two more attributes on the textarea --><textareaid="contact-message"name="message"rows="5"minlength="20"requiredaria-describedby="contact-message-hint contact-message-error"data-message-valuemissing="Write your message."data-message-tooshort="Add a little more detail: at least 20 characters."></textarea></form><!-- After </main> --><toast-msgid="contact-toast"position="bottom-end"></toast-msg><scripttype="module">const form = document.querySelector('form');const toast = document.querySelector('#contact-toast'); form.addEventListener('vb:submit', (event) => {// event.detail is the validated FormData: send it to your server here. toast.show({ message: 'Message sent. We reply within one business day.', variant: 'success' }); form.reset(); });</script>
data-validate hands validation to Vanilla Breeze. On submit it checks every field, shows all the errors and moves focus to the first invalid control. The browser's own message bubbles are switched off.
data-submit="event" changes what a valid submit does. Nothing is sent. The form fires a vb:submit event whose detail is the form's FormData. Here the handler only shows the toast; in your project, the comment marks where a fetch() call goes.
data-message-valuemissing, data-message-typemismatch and data-message-tooshort each name one kind of validation failure. With the script loaded, <form-field> puts the matching text in the error <output>. Other controls keep their step 3 text.
<toast-msg> is an empty container fixed to a corner of the window. Calling show() with a message and a variant adds a toast. The container is a polite live region, so a screen reader announces the message. The toast leaves after five seconds, and waits while the pointer or keyboard focus is on it.
form.reset() empties the controls, and each <form-field> returns to its neutral look.
Check
Submit the empty form. All five errors appear, focus lands on Full name, and the browser's bubble is gone. Email now says "Enter your email address."
Type ada in Email. The message changes to the one about a missing @.
Fill in everything and send. A toast appears in the bottom corner of the frame and the form clears. The address of the frame does not change: nothing was submitted.
Turn JavaScript off in your browser's developer tools and reload. You are back at step 3: the asterisks, hints and step 3 error texts all work, and a valid submit goes to the server the ordinary way. That is why a real form still needs an action and a server page that says the message arrived.
What you learned
Labels, required, type, minlength and autocomplete give you a validating form with no script.
data-layout on elements you already have lays the form out without adding one.
<form-field> with <output class="hint"> and <output class="error"> shows help and errors beside each control using CSS alone.
With the script, data-validate and data-message-* choose a message for each kind of mistake, and data-submit="event" hands you the validated data.
<toast-msg> confirms the send, and the form still submits when the script does not run.
Try it yourself
Add an optional phone field. Use type="tel" and autocomplete="tel", leave out required, and give it a hint. Read: form-field and the autocomplete attribute.
Add an error summary. A list of the errors at the top of the form, each linking to its field. Vanilla Breeze fills it in when it finds the summary markup inside a data-validate form. Read: the validation section of form-field and the form element.
Keep the confirmation on the page. A toast disappears after a few seconds. Put an empty <section role="status"> above the form and, in the vb:submit handler, fill it with a <status-message data-variant="success"> instead of calling toast.show(). Read: status-message and toast-msg.