liveBook · Manning

ElementInternals

As explained in Web Component Development with Modern Libraries and Tooling

A browser-provided interface exposing internal capabilities that let a custom element integrate with platform features such as forms.

ElementInternals exposes browser capabilities that let a custom element participate in form submission. A component opts into form association with a static declaration, obtains its internals with attachInternals(), and publishes values through setFormValue(). section 3.9

A form-associated temperature gauge

A vanilla custom element can submit its current value like this: static formAssociated = true; #internals = this.attachInternals(); connectedCallback() { this.#internals.setFormValue(this.value); } With <temperature-gauge name="temperature"> inside a form, the submitted key is temperature. Call setFormValue() again whenever the component value changes. section 3.9

Use ElementInternals for these form behaviors:

  • Submit multiple values by constructing FormData, setting entries such as color and size, and passing it to setFormValue(). section 3.9
  • Support reset behavior by saving initial submit-able properties in formAssociatedCallback() and restoring them in formResetCallback(). section 3.9
  • In Stencil, enable form association in the component configuration, inject internals with @AttachInternals(), and update the value during componentWillLoad() and on changes. section 6.12
const formData = new FormData();
formData.set("color", someColor);
formData.set("size", someSize);
this.#internals.setFormValue(formData);

Inspect the FormData pattern for submitting several values from one custom element. section 3.9

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Explains declarations, internals attachment, single and multiple values, updates, and reset callbacks without detailing browser internals.
Examples
Uses temperature-gauge for one value and radio-group for changing single or multiple submitted values.
What it assumes
Assumes custom-element classes or Stencil components, HTML forms, component lifecycle methods, and basic FormData usage.
What you can do after
Apply it to build custom elements that submit single or multiple values through native forms.
How it connects
Connects custom elements with HTML forms, FormData, lifecycle callbacks, and Stencil components.

Also explained in

Compare how 2 books explain ElementInternals →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.