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

form

Container for interactive form controls that collects and submits user data. The form element provides structure, validation, and submission handling.

When to Use

  • Collecting user input (contact forms, login, registration)
  • Search functionality
  • Any interaction that submits data to a server
  • Client-side form validation with HTML5 attributes

When Not to Use

  • For layout or grouping unrelated content (use <section> or <fieldset>)
  • For links styled as buttons (use <a> directly)
  • For toggling UI state without server submission (use JavaScript event handlers)

Basic Usage

A minimal form with form.stacked layout, <form-field> wrappers, and a <footer class="actions"> for buttons.

Layout Variants

VB provides three layout classes on <form> that handle spacing and direction without extra wrappers.

form.stacked

Fields arranged vertically with consistent spacing. Best for general-purpose forms.

form.inline

Fields arranged horizontally with wrapping. Best for search bars and compact forms.

form.grid

Labels on the left, inputs on the right in a two-column grid. Best for data entry forms.

Class Display Best for
form.stacked Vertical flex column General-purpose forms, contact forms
form.inline Horizontal flex with wrap Search bars, filters, compact forms
form.grid Two-column grid Data entry, settings panels

Form Attributes

Attribute Purpose Example
action URL to submit data to action="/api/contact"
method HTTP method (GET or POST) method="POST"
enctype Encoding type for POST data enctype="multipart/form-data"
novalidate Disable browser validation novalidate
autocomplete Enable/disable autofill autocomplete="on"
name Form name for scripting name="contact-form"

Using form-field

The <form-field> web component wraps a label, input, and optional help/error text into a single accessible unit. It wires aria-describedby from the control to its hint and error <output> elements for you (authors no longer need to write it by hand), shows validation state, and adds required indicators. See the form-field page for its full API.

Form Actions

Use <footer class="actions"> for the submit/cancel button row at the bottom of a form. The .end modifier right-aligns buttons; .between pushes groups apart.

Class Alignment
.actions Start-aligned (default)
.actions.end End-aligned
.actions.between Space between groups

Help and Error Text

Inside a <form-field>, use <output class="hint"> for persistent help and an empty <output class="error"> for validation messages. <form-field> fills the error output (from data-message-* or the browser message), toggles aria-invalid, and wires both outputs into the control’s aria-describedby.

Outside <form-field> (small static forms, server-rendered errors), <p class="help"> and <p class="error"> are styled inside a form; connect them yourself with aria-describedby and set aria-invalid="true" on the control.

Validation and Submission (data-validate)

Add data-validate to a <form> to let the form coordinator own validation UI. It sets novalidate (no browser bubbles), validates every <form-field> on submit, runs cross-field (data-match) and checkbox-group (data-min-checked/data-max-checked) checks, marks invalid bare controls with aria-invalid="true", and then either focuses the error summary or the first invalid control. Without JavaScript the form still submits with native validation.

Error summary

Author a summary region inside the form. On an invalid submit it is un-hidden, filled with one link per error (radio groups appear once; checkbox groups link their first box; messages match what the field shows) and focused. After that it updates when a field loses focus or changes — never per keystroke — and only when the set of errors changes. Once every error is fixed it collapses and gets hidden again, so stale links are neither focusable nor announced.

An element with data-summary-heading is rewritten with a count (“Please fix 2 errors:”); omit the attribute to keep your own heading. The older <output class="error-summary" role="alert" tabindex="-1"> pattern is still filled the same way. data-validate="summary" hides the inline field errors and shows only the summary.

Submit modes (data-submit)

Value After validation passes
absent / native Submits natively with requestSubmit(submitter), so the clicked button’s name/value, formaction and formmethod apply.
event Dispatches a cancelable, bubbling vb:submit event whose detail is the FormData (including the submitter). Nothing is sent.
fetch Sends with fetch(). Method is the submitter’s formmethod, else the form’s method attribute, else POST; GET builds a query string. URL honours formaction. Sets data-submitting while in flight, then data-success or data-error.

Events

  • vb:beforesubmit — cancelable, bubbling, fires after validation passes and before any mode runs; detail is { form, submitter }. Cancel it to stop the submission (bot protection, confirmation).
  • vb:submit — data-submit="event" only; detail is the FormData.

Fetch results

  • Success: data-success on the form hides everything except [data-form-success], which is shown and focused (it gets tabindex="-1" and role="status" if you did not set them).
  • Server field errors: a non-OK JSON response with { "errors": { "fieldName": "message" } } puts each message on its field (via form-field’s setError()) and in the summary — always as text, never HTML.
  • Network error: data-error="network" on the form and the [data-form-error] element is shown. Author one (with role="alert") to control the copy and placement; otherwise a <p data-form-error role="alert"> is created at the top of the form.
  • The next submit clears data-success/data-error and re-hides those elements.

Loading buttons (data-loading)

A submit button with data-loading shows a spinner and aria-busy only once a submission actually proceeds: an invalid submit, a cancelled vb:beforesubmit or any preventDefault() on the native submit leaves it alone. In fetch mode it reverts when data-submitting is cleared; in event mode clear it yourself with delete button.dataset.loadingActive when your handler finishes (or let data-loading-timeout, default 10 s, revert it).

Translating built-in text

Every string the coordinator writes can be overridden on the <form> (fieldset- and field-level attributes win where noted).

Attribute Default
data-label-summary, data-label-summary-one, -other, -few… “Please fix {count} error(s):” — plural form chosen with Intl.PluralRules for the form’s lang
data-message-min-checked / data-message-max-checked “Select at least {min}” / “Select no more than {max}” (also on the fieldset)
data-message-match “Values do not match” (also on the field)
data-label-group / data-message-invalid-selection “Group” / “Invalid selection” — fallbacks for a fieldset entry without a legend or message
data-message-network “Something went wrong. Please try again.” (generated [data-form-error] only)

Complete Contact Form

A full example combining form.stacked, <form-field>, <fieldset>, and <footer class="actions">.

Conditional Fields (data-show-when)

Use data-show-when or data-hide-when on any element inside a form to conditionally display sections based on another field’s value. Hidden sections get hidden and inert, and their controls are disabled — so a hidden required field never blocks submit and its value is not sent. Showing the section re-enables only the controls it disabled; anything you marked disabled yourself stays disabled. See data-show-when for the full condition syntax, checkbox groups and reset behaviour.

Show When

Show a section when a field has a specific value.

Hide When

Hide a section when a condition is met — the inverse of data-show-when.

Attribute Format Description
data-show-when name=value Show when the named field equals the value.
data-hide-when name=value Hide when the named field equals the value.

Works with <select>, <input type="radio">, <input type="checkbox">, and text inputs. The name refers to the name attribute of the controlling field within the same <form>.

Form Autosave (data-autosave)

Add data-autosave="key" to a <form> to automatically save its data to localStorage as the user types. On page reload, the draft is restored and a “Draft restored” toast appears. Drafts are cleared on submit or reset and expire after 24 hours. Passwords, hidden inputs, buttons, card numbers and one-time codes are never stored. Full reference, including what is stored: data-autosave.

Attribute Description
data-autosave Storage key for this form’s draft. Must be unique per form.

Behavior

  • Save: Debounced (500ms) on every input and change event
  • Restore: On page load if a non-expired draft exists
  • Clear: On form submit or reset
  • Skip: Password, hidden and file inputs, buttons, autocomplete="off", card and one-time-code fields, and anything marked data-autosave-ignore are never saved or restored
  • Expire: Drafts older than 24 hours are discarded

Multi-step Forms (data-wizard)

form[data-wizard] splits a form into fieldset[data-wizard-step] steps with a <progress data-wizard-progress> bar and a nav[data-wizard-nav] of previous/next/submit buttons. Without JavaScript every step shows and the form submits normally. The data-wizard page documents every option (conditional steps, persistence, history, <progress-tracker>); the wizard pattern shows a full flow.

The visible “Step 2 of 4” counter is decorative (step changes are announced through a visually hidden status region). Translate its words with custom properties:

Accessibility Notes

  • Label all inputs: Every form control needs an associated label
  • Use fieldsets: Group related fields with fieldset and legend
  • Error identification: Invalid controls carry aria-invalid="true" and point at their message with aria-describedby — <form-field> and data-validate do this for you; wire it by hand only for bare controls in unenhanced forms
  • Error summary: For long forms, add a [data-form-summary] so keyboard and screen reader users land on a list of links to every error
  • Autocomplete: Add appropriate autocomplete attributes for autofill
  • Focus management: Use autofocus on the first field (one per page)
  • Submit button: Always include a visible submit button, not just Enter key handling
  • Status changes: Fetch success moves focus to [data-form-success] (a role="status" region); network failures use a role="alert" [data-form-error] element, not CSS-generated text

CSS Reference

Related

  • input - Text inputs, checkboxes, radios, and more
  • button - Form submission and reset buttons
  • select - Dropdown selection menus
  • textarea - Multi-line text input
  • fieldset - Group related form fields
  • label - Accessible input labels
  • legend - Caption for fieldset groups
  • output - Calculation result or action output
  • form-field - Field wrapper: validation timing, messages, aria-describedby wiring
  • datalist - Autocomplete suggestions for inputs
  • dialog - Modal forms with method="dialog"
  • form - Controls outside the form element that still belong to it
  • novalidate - Skip submit-time validation for a form or one button
  • formaction - Per-button overrides: formaction, formmethod, formnovalidate and more
  • autocomplete - Autofill tokens for names, addresses, passwords and codes

Form enhancement attributes

↑