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-Associated Custom Elements

Guide to making web components participate in native HTML form submission using ElementInternals.

Make web components participate in native HTML form submission, validation, and reset using the ElementInternals API.

What Are Form-Associated Custom Elements?

By default, custom elements are invisible to HTML forms. A <form> only collects data from native form controls (<input>, <select>, <textarea>). Form-Associated Custom Elements (FACE) let your web components behave like native form controls:

  • Submit values via FormData without hidden inputs
  • Participate in native constraint validation (:valid, :invalid)
  • Respond to form reset and browser history restoration
  • Work with <label> and <fieldset>

Complete Template

Here is a complete annotated template showing all the pieces needed for a form-associated web component:

Step-by-Step Checklist

The boilerplate is intentionally small — roughly 5 lines to opt in. The rest is component-specific logic.

1. static formAssociated = true

Tells the browser this element participates in forms. Without this, attachInternals() won't provide form-related methods.

2. attachInternals()

Returns an ElementInternals object. Call this in the constructor(). It provides setFormValue(), setValidity(), and ARIA reflection.

3. setFormValue(value)

Sets the value submitted with the form. Pass null to exclude the element from form data. The optional second argument is a "state" string used by formStateRestoreCallback.

4. setValidity()

Sets constraint validation state. Pass an empty object {} to mark as valid.

5. Lifecycle Callbacks

formResetCallback() fires when the parent form resets. formStateRestoreCallback(state) fires when the browser restores form state from navigation history.

Progressive Enhancement

For components that need a no-JS fallback, include a hidden input or native control that works before JavaScript loads:

VB components like <combo-box> use a different strategy: the light-DOM input and list are usable before JS, and JS enhances them with filtering, keyboard navigation, and form association.

Validation

The setValidity() method accepts the same constraint flags as native inputs:

CSS Validation Selectors

VB's :user-valid and :user-invalid pseudo-classes work automatically with form-associated custom elements:

Firefox ARIA Caveat

Firefox does not yet support ARIA reflection on ElementInternals. When you set internals.role or internals.ariaLabel, Firefox silently ignores it. VB provides a small utility to handle this cross-browser:

These helpers check for support and fall back to setting attributes on the host element. See src/utils/form-internals.js.

VB Examples

Two VB components use this pattern:

  • <star-rating> — Star rating input with radio buttons internally, form value via ElementInternals
  • <combo-box> — Autocomplete combobox with full keyboard navigation, filtering, and form association

Browser Support

Form-Associated Custom Elements are supported in all modern browsers. ElementInternals shipped in Chrome 77, Firefox 93, and Safari 16.4. The ARIA reflection part of ElementInternals is not yet supported in Firefox (use the VB utility above).

Further Reading

↑