Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

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…

Read the release notes

← All tutorials

Tutorial: Contact form

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:

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

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.

What changed

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.

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

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

Check

What you learned

Try it yourself

  1. 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.
  2. 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.
  3. 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.
↑