A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.html 67KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. <!doctype html><!-- This is a valid HTML5 document. -->
  2. <!-- Screen readers, SEO, extensions and so on. -->
  3. <html lang="en">
  4. <!-- Has to be within the first 1024 bytes, hence before the `title` element
  5. See: https://www.w3.org/TR/2012/CR-html5-20121217/document-metadata.html#charset -->
  6. <meta charset="utf-8">
  7. <!-- Why no `X-UA-Compatible` meta: https://stackoverflow.com/a/6771584 -->
  8. <!-- The viewport meta is quite crowded and we are responsible for that.
  9. See: https://codepen.io/tigt/post/meta-viewport-for-2015 -->
  10. <meta name="viewport" content="width=device-width,initial-scale=1">
  11. <!-- Required to make a valid HTML5 document. -->
  12. <title>Custom Forms with Web Components and "ElementInternals" (archive) — David Larlet</title>
  13. <meta name="description" content="Publication mise en cache pour en conserver une trace.">
  14. <!-- That good ol' feed, subscribe :). -->
  15. <link rel="alternate" type="application/atom+xml" title="Feed" href="/david/log/">
  16. <!-- Generated from https://realfavicongenerator.net/ such a mess. -->
  17. <link rel="apple-touch-icon" sizes="180x180" href="/static/david/icons2/apple-touch-icon.png">
  18. <link rel="icon" type="image/png" sizes="32x32" href="/static/david/icons2/favicon-32x32.png">
  19. <link rel="icon" type="image/png" sizes="16x16" href="/static/david/icons2/favicon-16x16.png">
  20. <link rel="manifest" href="/static/david/icons2/site.webmanifest">
  21. <link rel="mask-icon" href="/static/david/icons2/safari-pinned-tab.svg" color="#07486c">
  22. <link rel="shortcut icon" href="/static/david/icons2/favicon.ico">
  23. <meta name="msapplication-TileColor" content="#f7f7f7">
  24. <meta name="msapplication-config" content="/static/david/icons2/browserconfig.xml">
  25. <meta name="theme-color" content="#f7f7f7" media="(prefers-color-scheme: light)">
  26. <meta name="theme-color" content="#272727" media="(prefers-color-scheme: dark)">
  27. <!-- Is that even respected? Retrospectively? What a shAItshow…
  28. https://neil-clarke.com/block-the-bots-that-feed-ai-models-by-scraping-your-website/ -->
  29. <meta name="robots" content="noai, noimageai">
  30. <!-- Documented, feel free to shoot an email. -->
  31. <link rel="stylesheet" href="/static/david/css/style_2021-01-20.css">
  32. <!-- See https://www.zachleat.com/web/comprehensive-webfonts/ for the trade-off. -->
  33. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  34. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  35. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  36. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  37. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  38. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  39. <script>
  40. function toggleTheme(themeName) {
  41. document.documentElement.classList.toggle(
  42. 'forced-dark',
  43. themeName === 'dark'
  44. )
  45. document.documentElement.classList.toggle(
  46. 'forced-light',
  47. themeName === 'light'
  48. )
  49. }
  50. const selectedTheme = localStorage.getItem('theme')
  51. if (selectedTheme !== 'undefined') {
  52. toggleTheme(selectedTheme)
  53. }
  54. </script>
  55. <meta name="robots" content="noindex, nofollow">
  56. <meta content="origin-when-cross-origin" name="referrer">
  57. <!-- Canonical URL for SEO purposes -->
  58. <link rel="canonical" href="https://dev.to/stuffbreaker/custom-forms-with-web-components-and-elementinternals-4jaj">
  59. <body class="remarkdown h1-underline h2-underline h3-underline em-underscore hr-center ul-star pre-tick" data-instant-intensity="viewport-all">
  60. <article>
  61. <header>
  62. <h1>Custom Forms with Web Components and "ElementInternals"</h1>
  63. </header>
  64. <nav>
  65. <p class="center">
  66. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  67. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  68. </svg> Accueil</a> •
  69. <a href="https://dev.to/stuffbreaker/custom-forms-with-web-components-and-elementinternals-4jaj" title="Lien vers le contenu original">Source originale</a>
  70. <br>
  71. Mis en cache le 2024-02-23
  72. </p>
  73. </nav>
  74. <hr>
  75. <p>With the release of Safari 16.4 in March 2023, web components reached a milestone in their capability to interact with the <code>&lt;form&gt;</code> element using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals"><code>ElementInternals</code> API</a>. Prior to this, input elements located within a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Components/Using_shadow_DOM">Shadow DOM</a> were not discoverable by forms, which means they would not validate on form submission and their data would not be included in the <code>FormData</code> object. The <code>ElementInterals</code> API allows us to create Form-Associated Custom Elements (FACE for short). Our custom elements can now behave like native input elements and take advantage of form APIs like <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation">form constraint validation</a>.</p>
  76. <p>I wanted to explore the capabilities of the API so we could implement it in our component library at work, but as I began working on it, I couldn’t always see a clear path forward, so I decided to write this article for anyone else running into the same problems. I will also use TypeScript in the examples to help define the APIs.</p>
  77. <h2>
  78. <a name="getting-started" href="#getting-started">
  79. </a>
  80. Getting Started
  81. </h2>
  82. <p>If you already have components set up or if you are not following along on your own, please free to jump to the “Associating Custom Elements with Forms” section below. There are CodePens available to help you skip ahead. If not, <a href="https://codepen.io/break-stuff/pen/mdQpRqx?editors=1010">here is a CodePen</a> you can get started with.</p>
  83. <h3>
  84. <a name="setting-up-the-component" href="#setting-up-the-component">
  85. </a>
  86. Setting Up the Component
  87. </h3>
  88. <p>For the purposes of demonstrating the <code>ElementInternals</code> API, our component setup will be <em>very</em> simple with only two attributes initially - <code>value</code> and <code>required</code>. This example will be using “vanilla” web components, so your component setup will likely differ if you are using a library or framework to build your components. Be sure to follow those best practices when setting up your components. The good news is that implementing the <code>ElementInternals</code> API seems to be fairly consistent regardless of the tools you are using.</p>
  89. <h3>
  90. <a name="add-input" href="#add-input">
  91. </a>
  92. Add Input
  93. </h3>
  94. <p>The first thing we are going to do is use the component’s <code>connectedCallback</code> lifecycle hook to add a shadow root to our component and insert an input and label in it.<br>
  95. </p>
  96. <div class="highlight js-code-highlight">
  97. <pre class="highlight tsx"><code><span class="nx">customElements</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="dl">'</span><span class="s1">my-input</span><span class="dl">'</span><span class="p">,</span> <span class="kd">class</span> <span class="kd">extends</span> <span class="nx">HTMLElement</span> <span class="p">{</span>
  98. <span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  99. <span class="k">this</span><span class="p">.</span><span class="nx">attachShadow</span><span class="p">({</span> <span class="na">mode</span><span class="p">:</span> <span class="dl">'</span><span class="s1">open</span><span class="dl">'</span> <span class="p">}).</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="s2">`
  100. &lt;label&gt;
  101. My Input
  102. &lt;input type="text" /&gt;
  103. &lt;/label&gt;`</span><span class="p">;</span>
  104. <span class="p">}</span>
  105. <span class="p">});</span>
  106. </code></pre>
  107. <div class="highlight__panel js-actions-panel">
  108. <div class="highlight__panel-action js-fullscreen-code-action">
  109. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  110. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  111. </svg>
  112. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  113. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  114. </svg>
  115. </div>
  116. </div>
  117. </div>
  118. <p>Let’s grab a reference to our input element. We can do that by creating a property at the root of our class (I am choosing to prefix this with a <code>$</code> to distinguish properties referencing HTML elements from other properties).<br>
  119. </p>
  120. <div class="highlight js-code-highlight">
  121. <pre class="highlight tsx"><code><span class="k">private</span> <span class="nx">$input</span><span class="p">:</span> <span class="nx">HTMLInputElement</span><span class="p">;</span>
  122. </code></pre>
  123. <div class="highlight__panel js-actions-panel">
  124. <div class="highlight__panel-action js-fullscreen-code-action">
  125. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  126. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  127. </svg>
  128. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  129. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  130. </svg>
  131. </div>
  132. </div>
  133. </div>
  134. <p>In our <code>connectedCallback</code> method, we can query the shadow DOM contents for our input element.<br>
  135. </p>
  136. <div class="highlight js-code-highlight">
  137. <pre class="highlight tsx"><code><span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  138. <span class="p">...</span>
  139. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">shadowRoot</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="dl">'</span><span class="s1">input</span><span class="dl">'</span><span class="p">);</span>
  140. <span class="p">}</span>
  141. </code></pre>
  142. <div class="highlight__panel js-actions-panel">
  143. <div class="highlight__panel-action js-fullscreen-code-action">
  144. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  145. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  146. </svg>
  147. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  148. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  149. </svg>
  150. </div>
  151. </div>
  152. </div>
  153. <h3>
  154. <a name="add-attributes" href="#add-attributes">
  155. </a>
  156. Add Attributes
  157. </h3>
  158. <p>Let’s add <code>value</code> and <code>required</code> attributes so we can set them on our custom element’s tag and pass them to our internal element (<code>&lt;my-input value=”abc” required&gt;&lt;/my-input</code>). We will do this by adding the <code>observedAttributes</code> property and returning an array of the attribute names.<br>
  159. </p>
  160. <div class="highlight js-code-highlight">
  161. <pre class="highlight tsx"><code><span class="k">static</span> <span class="kd">get</span> <span class="nx">observedAttributes</span><span class="p">()</span> <span class="p">{</span>
  162. <span class="k">return</span> <span class="p">[</span><span class="dl">"</span><span class="s2">required</span><span class="dl">"</span><span class="p">,</span> <span class="dl">"</span><span class="s2">value</span><span class="dl">"</span><span class="p">];</span>
  163. <span class="p">}</span>
  164. </code></pre>
  165. <div class="highlight__panel js-actions-panel">
  166. <div class="highlight__panel-action js-fullscreen-code-action">
  167. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  168. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  169. </svg>
  170. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  171. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  172. </svg>
  173. </div>
  174. </div>
  175. </div>
  176. <p>Now we need to update the input properties when the attributes change using the <code>attributeChangedCallback</code> lifecycle hook, but we will have a timing issue. The <code>attributeChangedCallback</code> method will run before our internal input has had a chance to render and for our query selector to assign it to our <code>$input</code> variable. To get around this, we will create a component variable to capture the attributes and values and we will update our internal input when it is ready.</p>
  177. <p>First, let’s add a private property called <code>_attrs</code> to our component and set the value to an empty object.<br>
  178. </p>
  179. <div class="highlight js-code-highlight">
  180. <pre class="highlight tsx"><code><span class="k">private</span> <span class="nx">_attrs</span> <span class="o">=</span> <span class="p">{};</span>
  181. </code></pre>
  182. <div class="highlight__panel js-actions-panel">
  183. <div class="highlight__panel-action js-fullscreen-code-action">
  184. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  185. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  186. </svg>
  187. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  188. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  189. </svg>
  190. </div>
  191. </div>
  192. </div>
  193. <p>In our <code>attributeChangeCallback</code> method, let’s assign any attribute changes to that object where <code>name</code> is the attribute being changed and <code>next</code> is the new value.<br>
  194. </p>
  195. <div class="highlight js-code-highlight">
  196. <pre class="highlight tsx"><code><span class="nx">attributeChangedCallback</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">prev</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
  197. <span class="k">this</span><span class="p">.</span><span class="nx">_attrs</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">next</span><span class="p">;</span>
  198. <span class="p">}</span>
  199. </code></pre>
  200. <div class="highlight__panel js-actions-panel">
  201. <div class="highlight__panel-action js-fullscreen-code-action">
  202. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  203. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  204. </svg>
  205. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  206. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  207. </svg>
  208. </div>
  209. </div>
  210. </div>
  211. <p>Now let’s create a private method that will use our <code>_attrs</code> values to update our input element.<br>
  212. </p>
  213. <div class="highlight js-code-highlight">
  214. <pre class="highlight tsx"><code><span class="k">private</span> <span class="nx">setProps</span><span class="p">()</span> <span class="p">{</span>
  215. <span class="c1">// prevent any errors in case the input isn't set</span>
  216. <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">)</span> <span class="p">{</span>
  217. <span class="k">return</span><span class="p">;</span>
  218. <span class="p">}</span>
  219. <span class="c1">// loop over the properties and apply them to the input</span>
  220. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">prop</span> <span class="k">in</span> <span class="k">this</span><span class="p">.</span><span class="nx">_attrs</span><span class="p">)</span> <span class="p">{</span>
  221. <span class="k">switch</span> <span class="p">(</span><span class="nx">prop</span><span class="p">)</span> <span class="p">{</span>
  222. <span class="k">case</span> <span class="dl">"</span><span class="s2">value</span><span class="dl">"</span><span class="p">:</span>
  223. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_attrs</span><span class="p">[</span><span class="nx">prop</span><span class="p">];</span>
  224. <span class="k">break</span><span class="p">;</span>
  225. <span class="k">case</span> <span class="dl">"</span><span class="s2">required</span><span class="dl">"</span><span class="p">:</span>
  226. <span class="kd">const</span> <span class="nx">required</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_attrs</span><span class="p">[</span><span class="nx">prop</span><span class="p">];</span>
  227. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">toggleAttribute</span><span class="p">(</span>
  228. <span class="dl">"</span><span class="s2">required</span><span class="dl">"</span><span class="p">,</span>
  229. <span class="nx">required</span> <span class="o">===</span> <span class="dl">"</span><span class="s2">true</span><span class="dl">"</span> <span class="o">||</span> <span class="nx">required</span> <span class="o">===</span> <span class="dl">""</span>
  230. <span class="p">);</span>
  231. <span class="k">break</span><span class="p">;</span>
  232. <span class="p">}</span>
  233. <span class="p">}</span>
  234. <span class="c1">// reset the attributes to prevent unwanted changes later</span>
  235. <span class="k">this</span><span class="p">.</span><span class="nx">_attrs</span> <span class="o">=</span> <span class="p">{};</span>
  236. <span class="p">}</span>
  237. </code></pre>
  238. <div class="highlight__panel js-actions-panel">
  239. <div class="highlight__panel-action js-fullscreen-code-action">
  240. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  241. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  242. </svg>
  243. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  244. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  245. </svg>
  246. </div>
  247. </div>
  248. </div>
  249. <p>We can now add this to the <code>connectedCallback</code> method to update our input element with any changes that happened before it was rendered.<br>
  250. </p>
  251. <div class="highlight js-code-highlight">
  252. <pre class="highlight tsx"><code><span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  253. <span class="p">...</span>
  254. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span> <span class="o">=</span> <span class="nx">shadowRoot</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="dl">'</span><span class="s1">input</span><span class="dl">'</span><span class="p">);</span>
  255. <span class="k">this</span><span class="p">.</span><span class="nx">setProps</span><span class="p">();</span>
  256. <span class="p">}</span>
  257. </code></pre>
  258. <div class="highlight__panel js-actions-panel">
  259. <div class="highlight__panel-action js-fullscreen-code-action">
  260. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  261. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  262. </svg>
  263. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  264. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  265. </svg>
  266. </div>
  267. </div>
  268. </div>
  269. <p>We can also add this to the <code>attributeChangedCallback</code> method so any attribute changes that occur after the <code>connectedCallback</code> method gets called are applied to the input element.<br>
  270. </p>
  271. <div class="highlight js-code-highlight">
  272. <pre class="highlight tsx"><code><span class="nx">attributeChangedCallback</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">prev</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
  273. <span class="k">this</span><span class="p">.</span><span class="nx">_attrs</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">next</span><span class="p">;</span>
  274. <span class="k">this</span><span class="p">.</span><span class="nx">setProps</span><span class="p">();</span>
  275. <span class="p">}</span>
  276. </code></pre>
  277. <div class="highlight__panel js-actions-panel">
  278. <div class="highlight__panel-action js-fullscreen-code-action">
  279. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  280. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  281. </svg>
  282. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  283. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  284. </svg>
  285. </div>
  286. </div>
  287. </div>
  288. <p>Now we should be able to add attributes to our element tag and have them pass down to the internal input element being rendered.</p>
  289. <p>
  290. </p>
  291. <h2>
  292. <a name="associating-custom-elements-with-forms" href="#associating-custom-elements-with-forms">
  293. </a>
  294. Associating Custom Elements with Forms
  295. </h2>
  296. <p>Associating your custom element is surprisingly straightforward and can be done in 2 steps:</p>
  297. <ol>
  298. <li><p>set the <code>formAssociated</code> static property to <code>true</code></p></li>
  299. <li><p>Expose the <code>ElementInternals</code> API by calling <code>this._internals = this.attachInternals();</code> in the component’s constructor.<br>
  300. </p></li>
  301. </ol>
  302. <div class="highlight js-code-highlight">
  303. <pre class="highlight tsx"><code><span class="k">static</span> <span class="nx">formAssociated</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
  304. <span class="k">private</span> <span class="nx">_internals</span><span class="p">:</span> <span class="nx">ElementInternals</span><span class="p">;</span>
  305. <span class="kd">constructor</span><span class="p">()</span> <span class="p">{</span>
  306. <span class="k">super</span><span class="p">();</span>
  307. <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attachInternals</span><span class="p">();</span>
  308. <span class="p">}</span>
  309. </code></pre>
  310. <div class="highlight__panel js-actions-panel">
  311. <div class="highlight__panel-action js-fullscreen-code-action">
  312. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  313. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  314. </svg>
  315. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  316. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  317. </svg>
  318. </div>
  319. </div>
  320. </div>
  321. <p>With those two changes, our custom element can now see the parent form! As a quick test try adding <code>console.log(this._internals.form);</code> to the <code>connectedCallback</code> method and you should see the parent logged in the console.</p>
  322. <h3>
  323. <a name="using-labels" href="#using-labels">
  324. </a>
  325. Using Labels
  326. </h3>
  327. <p>By making this a form-associated custom element, the browser now sees it as an input element which means we can move the label out of our component.<br>
  328. </p>
  329. <div class="highlight js-code-highlight">
  330. <pre class="highlight tsx"><code><span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  331. <span class="p">...</span>
  332. <span class="nx">shadowRoot</span><span class="p">.</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="s2">`&lt;input type="text" /&gt;`</span><span class="p">;</span>
  333. <span class="p">}</span>
  334. </code></pre>
  335. <div class="highlight__panel js-actions-panel">
  336. <div class="highlight__panel-action js-fullscreen-code-action">
  337. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  338. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  339. </svg>
  340. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  341. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  342. </svg>
  343. </div>
  344. </div>
  345. </div>
  346. <p>We can label our custom input element like a standard input element. Let’s use a <code>&lt;label&gt;</code> and reference it using an <code>id</code> on the custom element and a <code>for</code> attribute on the label.<br>
  347. </p>
  348. <div class="highlight js-code-highlight">
  349. <pre class="highlight html"><code><span class="nt">&lt;form</span> <span class="na">id=</span><span class="s">"my-form"</span><span class="nt">&gt;</span>
  350. <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">"input"</span><span class="nt">&gt;</span>My Input<span class="nt">&lt;/label&gt;</span>
  351. <span class="nt">&lt;my-input</span> <span class="na">id=</span><span class="s">"input"</span><span class="nt">&gt;&lt;/my-input&gt;</span>
  352. <span class="nt">&lt;/form&gt;</span>
  353. </code></pre>
  354. <div class="highlight__panel js-actions-panel">
  355. <div class="highlight__panel-action js-fullscreen-code-action">
  356. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  357. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  358. </svg>
  359. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  360. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  361. </svg>
  362. </div>
  363. </div>
  364. </div>
  365. <blockquote>
  366. <p><strong><em>NOTE:</em></strong> There is an accessibility bug in <a href="https://bugs.webkit.org/show_bug.cgi?id=259124">Safari</a> and <a href="https://github.com/nvaccess/nvda/issues/15118">NVDA</a> where labels are not properly associated to Form-Associated Custom Elements for screen readers. They are read fine with VoiceOver in Chromium browsers (Chrome, Edger, Brave, etc.) and Firefox on Mac and MS Narrator and JAWS on Windows. As a workaround, you can continue including your labels within your elements.</p>
  367. </blockquote>
  368. <p>Let’s also update our shadow root configuration to delegate focus. This will allow the input to be focused when the label is clicked like with native input elements.<br>
  369. </p>
  370. <div class="highlight js-code-highlight">
  371. <pre class="highlight tsx"><code><span class="kd">const</span> <span class="nx">shadowRoot</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attachShadow</span><span class="p">({</span> <span class="na">mode</span><span class="p">:</span> <span class="dl">'</span><span class="s1">open</span><span class="dl">'</span><span class="p">,</span> <span class="na">delegatesFocus</span><span class="p">:</span> <span class="kc">true</span> <span class="p">});</span>
  372. </code></pre>
  373. <div class="highlight__panel js-actions-panel">
  374. <div class="highlight__panel-action js-fullscreen-code-action">
  375. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  376. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  377. </svg>
  378. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  379. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  380. </svg>
  381. </div>
  382. </div>
  383. </div>
  384. <blockquote>
  385. <p>“If <code>delegatesFocus</code> is <code>true</code>, when a non-focusable part of the shadow DOM is clicked, or <code>.focus()</code> is called on the host element, the first focusable part is given focus, and the shadow host is given any available <code>:focus</code> styling.” - MDN</p>
  386. </blockquote>
  387. <h3>
  388. <a name="exposing-validation" href="#exposing-validation">
  389. </a>
  390. Exposing Validation
  391. </h3>
  392. <p>Now that the form is associated with our custom element, we can begin by exposing some of the core validation behavior we would expect with an input element like <code>checkValidity</code>, <code>reportValidity</code>, <code>validity</code>, and <code>validationMessage</code>.<br>
  393. </p>
  394. <div class="highlight js-code-highlight">
  395. <pre class="highlight tsx"><code><span class="k">public</span> <span class="nx">checkValidity</span><span class="p">():</span> <span class="nx">boolean</span> <span class="p">{</span>
  396. <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">checkValidity</span><span class="p">();</span>
  397. <span class="p">}</span>
  398. <span class="k">public</span> <span class="nx">reportValidity</span><span class="p">():</span> <span class="k">void</span> <span class="p">{</span>
  399. <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">reportValidity</span><span class="p">();</span>
  400. <span class="p">}</span>
  401. <span class="k">public</span> <span class="kd">get</span> <span class="nx">validity</span><span class="p">():</span> <span class="nx">ValidityState</span> <span class="p">{</span>
  402. <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">validity</span><span class="p">;</span>
  403. <span class="p">}</span>
  404. <span class="k">public</span> <span class="kd">get</span> <span class="nx">validationMessage</span><span class="p">():</span> <span class="kr">string</span> <span class="p">{</span>
  405. <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">validationMessage</span><span class="p">;</span>
  406. <span class="p">}</span>
  407. </code></pre>
  408. <div class="highlight__panel js-actions-panel">
  409. <div class="highlight__panel-action js-fullscreen-code-action">
  410. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  411. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  412. </svg>
  413. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  414. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  415. </svg>
  416. </div>
  417. </div>
  418. </div>
  419. <h3>
  420. <a name="controlling-validation" href="#controlling-validation">
  421. </a>
  422. Controlling Validation
  423. </h3>
  424. <p>The <code>ElementIntenrals</code> API gives us access to the <code>setValidity</code> method, which we can use to communicate to the form the validity status of our element.<br>
  425. </p>
  426. <div class="highlight js-code-highlight">
  427. <pre class="highlight tsx"><code><span class="nx">setValidity</span><span class="p">(</span><span class="nx">flags</span><span class="p">:</span> <span class="nx">ValidityStateFlags</span><span class="p">,</span> <span class="nx">message</span><span class="p">?:</span> <span class="kr">string</span><span class="p">,</span> <span class="nx">anchor</span><span class="p">?:</span> <span class="nx">HTMLElement</span><span class="p">)</span>
  428. </code></pre>
  429. <div class="highlight__panel js-actions-panel">
  430. <div class="highlight__panel-action js-fullscreen-code-action">
  431. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  432. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  433. </svg>
  434. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  435. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  436. </svg>
  437. </div>
  438. </div>
  439. </div>
  440. <p>As you can see, the <code>message</code> and <code>anchor</code> attributes are optional. If you want to reset the validation, you can pass an empty object (<code>{}</code>) as the flags parameter.</p>
  441. <h4>
  442. <a name="flags" href="#flags">
  443. </a>
  444. Flags
  445. </h4>
  446. <p>The <code>flags</code> interface is almost identical to the <code>ValidityState</code> object you get when you call <code>input.validity</code>, but each of the properties is optional and can be set (where an input’s <code>validity</code> is a read-only property). Here is an example of the interface as well as some examples of when these would be set with native HTML input elements.<br>
  447. </p>
  448. <div class="highlight js-code-highlight">
  449. <pre class="highlight tsx"><code><span class="kr">interface</span> <span class="nx">ValidityStateFlags</span> <span class="p">{</span>
  450. <span class="cm">/** `true` if the element is required, but has no value */</span>
  451. <span class="nl">valueMissing</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  452. <span class="cm">/** `true` if the value is not in the required syntax (when the "type" is "email" or "URL") */</span>
  453. <span class="nl">typeMismatch</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  454. <span class="cm">/** `true` if the value does not match the specified pattern */</span>
  455. <span class="nl">patternMismatch</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  456. <span class="cm">/** `true` if the value exceeds the specified `maxlength` */</span>
  457. <span class="nl">tooLong</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  458. <span class="cm">/** `true` if the value fails to meet the specified `minlength` */</span>
  459. <span class="nl">tooShort</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  460. <span class="cm">/** `true` if the value is less than the minimum specified by the `min` attribute */</span>
  461. <span class="nl">rangeUnderflow</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  462. <span class="cm">/** `true` if the value is greater than the maximum specified by the `max` attribute */</span>
  463. <span class="nl">rangeOverflow</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  464. <span class="cm">/** `true` if the value does not fit the rules determined by the `step` attribute (that is, it's not evenly divisible by the step value) */</span>
  465. <span class="nl">stepMismatch</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  466. <span class="cm">/** `true` if the user has provided input that the browser is unable to convert */</span>
  467. <span class="nl">badInput</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  468. <span class="cm">/** `true` if the element's custom validity message has been set to a non-empty string by calling the element's `setCustomValidity()` method */</span>
  469. <span class="nl">customError</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span>
  470. <span class="p">}</span>
  471. </code></pre>
  472. <div class="highlight__panel js-actions-panel">
  473. <div class="highlight__panel-action js-fullscreen-code-action">
  474. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  475. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  476. </svg>
  477. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  478. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  479. </svg>
  480. </div>
  481. </div>
  482. </div>
  483. <h4>
  484. <a name="message" href="#message">
  485. </a>
  486. Message
  487. </h4>
  488. <p>The <code>message</code> parameter is how we can provide the element with a custom error message whenever one of these validation parameters is validated.</p>
  489. <h4>
  490. <a name="anchor" href="#anchor">
  491. </a>
  492. Anchor
  493. </h4>
  494. <p>The <code>anchor</code> parameter is the element which we want to associate the error message with.</p>
  495. <h2>
  496. <a name="adding-validation" href="#adding-validation">
  497. </a>
  498. Adding Validation
  499. </h2>
  500. <p>Now that we can control how validation is set in our component, let’s add the functionality to make our input <code>required</code>.</p>
  501. <h3>
  502. <a name="initialize-validation" href="#initialize-validation">
  503. </a>
  504. Initialize Validation
  505. </h3>
  506. <p>Now let’s initialize the validation. Because our input’s <code>ValidityState</code> has essentially the same interface as our <code>ValidityStateFlags</code> we can use the input’s initial state to set the <code>ElementInterals</code> state. Right after our input selector in the <code>connectedCallback</code> method, let’s call the <code>setValidity</code> method based on our input.<br>
  507. </p>
  508. <div class="highlight js-code-highlight">
  509. <pre class="highlight tsx"><code><span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  510. <span class="p">...</span>
  511. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span> <span class="o">=</span> <span class="nx">shadowRoot</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="dl">'</span><span class="s1">input</span><span class="dl">'</span><span class="p">);</span>
  512. <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">setValidity</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">validity</span><span class="p">,</span>
  513. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">validationMessage</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">);</span>
  514. <span class="p">}</span>
  515. </code></pre>
  516. <div class="highlight__panel js-actions-panel">
  517. <div class="highlight__panel-action js-fullscreen-code-action">
  518. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  519. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  520. </svg>
  521. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  522. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  523. </svg>
  524. </div>
  525. </div>
  526. </div>
  527. <p>Here we use the internal input element’s <code>ValidityState</code>, but you can also pass in a subset of the <code>ValidityStateFlags</code> and a custom error message as well.<br>
  528. </p>
  529. <div class="highlight js-code-highlight">
  530. <pre class="highlight tsx"><code><span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">setValidity</span><span class="p">(</span>
  531. <span class="p">{</span>
  532. <span class="na">valueMissing</span><span class="p">:</span> <span class="kc">true</span>
  533. <span class="p">},</span>
  534. <span class="dl">'</span><span class="s1">Please fill out this required field</span><span class="dl">'</span><span class="p">,</span>
  535. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span>
  536. <span class="p">);</span>
  537. </code></pre>
  538. <div class="highlight__panel js-actions-panel">
  539. <div class="highlight__panel-action js-fullscreen-code-action">
  540. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  541. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  542. </svg>
  543. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  544. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  545. </svg>
  546. </div>
  547. </div>
  548. </div>
  549. <h3>
  550. <a name="testing-validation" href="#testing-validation">
  551. </a>
  552. Testing Validation
  553. </h3>
  554. <p>Everything should be wired up, so let’s test it out. Let’s update the HTML to add a submit button and a <code>required</code> attribute to our custom element.<br>
  555. </p>
  556. <div class="highlight js-code-highlight">
  557. <pre class="highlight html"><code><span class="nt">&lt;form</span> <span class="na">id=</span><span class="s">"my-form"</span><span class="nt">&gt;</span>
  558. <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">"input"</span><span class="nt">&gt;</span>My Input<span class="nt">&lt;/label&gt;</span>
  559. <span class="nt">&lt;my-input</span> <span class="na">id=</span><span class="s">"input"</span> <span class="na">required</span><span class="nt">&gt;&lt;/my-input&gt;</span>
  560. <span class="nt">&lt;button&gt;</span>Submit<span class="nt">&lt;/button&gt;</span>
  561. <span class="nt">&lt;/form&gt;</span>
  562. </code></pre>
  563. <div class="highlight__panel js-actions-panel">
  564. <div class="highlight__panel-action js-fullscreen-code-action">
  565. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  566. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  567. </svg>
  568. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  569. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  570. </svg>
  571. </div>
  572. </div>
  573. </div>
  574. <p>When you click the “Submit” button, you should see the browser validation message for a required field. If we select our form and call <code>form.checkValidity()</code>, it should return <code>false</code>.</p>
  575. <h3>
  576. <a name="updating-validation-using-raw-setvalidity-endraw-" href="#updating-validation-using-raw-setvalidity-endraw-">
  577. </a>
  578. Updating Validation Using <code>setValidity</code>
  579. </h3>
  580. <p>That validation state will remain as it is until we update it. If you enter text in the input and click “Submit”, you will still see an error message and <code>form.checkValidity()</code> will still return <code>false</code>.</p>
  581. <p>For this demonstration, we can set up a simple update whenever the user inputs content in the field. To do that, we will add an event listener to our <code>connectedCallback</code> method after we have selected our input element.<br>
  582. </p>
  583. <div class="highlight js-code-highlight">
  584. <pre class="highlight tsx"><code><span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  585. <span class="p">...</span>
  586. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">'</span><span class="s1">input</span><span class="dl">'</span><span class="p">,</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="k">this</span><span class="p">.</span><span class="nx">handleInput</span><span class="p">());</span>
  587. <span class="p">}</span>
  588. <span class="k">private</span> <span class="nx">handleInput</span><span class="p">()</span> <span class="p">{</span>
  589. <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">setValidity</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">validity</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">validationMessage</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">);</span>
  590. <span class="p">}</span>
  591. </code></pre>
  592. <div class="highlight__panel js-actions-panel">
  593. <div class="highlight__panel-action js-fullscreen-code-action">
  594. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  595. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  596. </svg>
  597. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  598. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  599. </svg>
  600. </div>
  601. </div>
  602. </div>
  603. <p>
  604. </p>
  605. <h2>
  606. <a name="updating-form-values-using-raw-setformvalue-endraw-" href="#updating-form-values-using-raw-setformvalue-endraw-">
  607. </a>
  608. Updating Form Values Using <code>setFormValue</code>
  609. </h2>
  610. <p>Using the <code>setFormValue</code> on the <code>ElementInternals</code> API, we can now update our form whenever the value changes in our custom element. This allows developers to easily get form values using the <code>FormData</code> API.</p>
  611. <p>Let’s set the initial value when the component loads in the <code>connectedCallback</code> method.<br>
  612. </p>
  613. <div class="highlight js-code-highlight">
  614. <pre class="highlight tsx"><code><span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  615. <span class="p">...</span>
  616. <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">setFormValue</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">value</span><span class="p">);</span>
  617. <span class="p">}</span>
  618. </code></pre>
  619. <div class="highlight__panel js-actions-panel">
  620. <div class="highlight__panel-action js-fullscreen-code-action">
  621. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  622. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  623. </svg>
  624. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  625. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  626. </svg>
  627. </div>
  628. </div>
  629. </div>
  630. <p>Now, let’s update the value any time the <code>input</code> event fires by adding the update to our event listener.<br>
  631. </p>
  632. <div class="highlight js-code-highlight">
  633. <pre class="highlight tsx"><code><span class="k">private</span> <span class="nx">handleInput</span><span class="p">()</span> <span class="p">{</span>
  634. <span class="p">...</span>
  635. <span class="k">this</span><span class="p">.</span><span class="nx">_internals</span><span class="p">.</span><span class="nx">setFormValue</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">value</span><span class="p">);</span>
  636. <span class="p">}</span>
  637. </code></pre>
  638. <div class="highlight__panel js-actions-panel">
  639. <div class="highlight__panel-action js-fullscreen-code-action">
  640. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  641. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  642. </svg>
  643. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  644. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  645. </svg>
  646. </div>
  647. </div>
  648. </div>
  649. <h3>
  650. <a name="testing-form-data" href="#testing-form-data">
  651. </a>
  652. Testing Form Data
  653. </h3>
  654. <p>To test our values, let’s update our component to include a <code>name</code> attribute for the form to identify it.<br>
  655. </p>
  656. <div class="highlight js-code-highlight">
  657. <pre class="highlight html"><code><span class="nt">&lt;form</span> <span class="na">id=</span><span class="s">"my-form"</span><span class="nt">&gt;</span>
  658. <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">"input"</span><span class="nt">&gt;</span>My Input<span class="nt">&lt;/label&gt;</span>
  659. <span class="nt">&lt;my-input</span> <span class="na">id=</span><span class="s">"input"</span> <span class="na">name=</span><span class="s">"myInput"</span> <span class="na">required</span><span class="nt">&gt;&lt;/my-input&gt;</span>
  660. <span class="nt">&lt;button&gt;</span>Submit<span class="nt">&lt;/button&gt;</span>
  661. <span class="nt">&lt;/form&gt;</span>
  662. </code></pre>
  663. <div class="highlight__panel js-actions-panel">
  664. <div class="highlight__panel-action js-fullscreen-code-action">
  665. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  666. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  667. </svg>
  668. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  669. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  670. </svg>
  671. </div>
  672. </div>
  673. </div>
  674. <p>We can now test to see if our value is being bound to the form by adding an event listener to the form’s <code>submit</code> event and grabbing the form data.<br>
  675. </p>
  676. <div class="highlight js-code-highlight">
  677. <pre class="highlight tsx"><code><span class="kd">const</span> <span class="nx">form</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="dl">"</span><span class="s2">my-form</span><span class="dl">"</span><span class="p">);</span>
  678. <span class="nx">form</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="dl">"</span><span class="s2">submit</span><span class="dl">"</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  679. <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
  680. <span class="kd">const</span> <span class="nx">formData</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">FormData</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">);</span>
  681. <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">`My Input Value - '</span><span class="p">${</span><span class="nx">formData</span><span class="p">.</span><span class="kd">get</span><span class="p">(</span><span class="dl">'</span><span class="s1">myInput</span><span class="dl">'</span><span class="p">)}</span><span class="s2">'`</span><span class="p">);</span>
  682. <span class="p">});</span>
  683. </code></pre>
  684. <div class="highlight__panel js-actions-panel">
  685. <div class="highlight__panel-action js-fullscreen-code-action">
  686. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  687. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  688. </svg>
  689. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  690. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  691. </svg>
  692. </div>
  693. </div>
  694. </div>
  695. <p>Type a value into the input and click the “Submit” button. You should see your value logged in the console.</p>
  696. <p>
  697. </p>
  698. <h2>
  699. <a name="elementinternals-lifecycle-hooks" href="#elementinternals-lifecycle-hooks">
  700. </a>
  701. ElementInternals Lifecycle Hooks
  702. </h2>
  703. <p>The <code>ElementInternals</code> API provides us with some additional lifecycle hooks that are important for controlling interactions with the browser and other elements. It is important to note that these are optional and should be used only when necessary.</p>
  704. <h3>
  705. <a name="-raw-formassociatedcallbackform-htmlformelement-endraw-" href="#-raw-formassociatedcallbackform-htmlformelement-endraw-">
  706. </a>
  707. <code>formAssociatedCallback(form: HTMLFormElement)</code>
  708. </h3>
  709. <p>This is called as soon as the element is associated with a form. We don’t really have a need for this right now so we won’t implement this right now.</p>
  710. <h3>
  711. <a name="-raw-formdisabledcallbackdisabled-boolean-endraw-" href="#-raw-formdisabledcallbackdisabled-boolean-endraw-">
  712. </a>
  713. <code>formDisabledCallback(disabled: boolean)</code>
  714. </h3>
  715. <p>This is called whenever the element or parent <code>&lt;fieldset&gt;</code> element are disabled. We can use this to help manage the disabled state of our internal element. We will add the callback method to the class and update our internal input element when it changes.<br>
  716. </p>
  717. <div class="highlight js-code-highlight">
  718. <pre class="highlight tsx"><code><span class="nx">formDisabledCallback</span><span class="p">(</span><span class="nx">disabled</span><span class="p">:</span> <span class="nx">boolean</span><span class="p">)</span> <span class="p">{</span>
  719. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">disabled</span> <span class="o">=</span> <span class="nx">disabled</span><span class="p">;</span>
  720. <span class="p">}</span>
  721. </code></pre>
  722. <div class="highlight__panel js-actions-panel">
  723. <div class="highlight__panel-action js-fullscreen-code-action">
  724. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  725. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  726. </svg>
  727. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  728. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  729. </svg>
  730. </div>
  731. </div>
  732. </div>
  733. <h3>
  734. <a name="-raw-formresetcallback-endraw-" href="#-raw-formresetcallback-endraw-">
  735. </a>
  736. <code>formResetCallback()</code>
  737. </h3>
  738. <p>This gives us the ability to control our element’s behavior when a user resets a form. In our case, we will keep it simple and reset the input value to whatever the initial value was when the component was loaded. We will create a private property called <code>_defaultValue</code>, set it in the <code>connectedCallback</code> method, and then use the <code>formResetCallback</code> callback method to reset the value if the form is reset.<br>
  739. </p>
  740. <div class="highlight js-code-highlight">
  741. <pre class="highlight tsx"><code><span class="k">private</span> <span class="nx">_defaultValue</span> <span class="o">=</span> <span class="dl">""</span><span class="p">;</span>
  742. <span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
  743. <span class="p">...</span>
  744. <span class="k">this</span><span class="p">.</span><span class="nx">_defaultValue</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">value</span><span class="p">;</span>
  745. <span class="p">}</span>
  746. <span class="nx">formResetCallback</span><span class="p">()</span> <span class="p">{</span>
  747. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_defaultValue</span><span class="p">;</span>
  748. <span class="p">}</span>
  749. </code></pre>
  750. <div class="highlight__panel js-actions-panel">
  751. <div class="highlight__panel-action js-fullscreen-code-action">
  752. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  753. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  754. </svg>
  755. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  756. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  757. </svg>
  758. </div>
  759. </div>
  760. </div>
  761. <p>Let’s update our form to include a reset button and add an initial value to the input. Now we can change the value and press the “Reset” button and it will revert back to the original value.<br>
  762. </p>
  763. <div class="highlight js-code-highlight">
  764. <pre class="highlight tsx"><code><span class="p">&lt;</span><span class="nt">form</span> <span class="na">id</span><span class="p">=</span><span class="s">"my-form"</span><span class="p">&gt;</span>
  765. <span class="p">&lt;</span><span class="nt">label</span> <span class="na">for</span><span class="p">=</span><span class="s">"input"</span><span class="p">&gt;</span>My Input<span class="p">&lt;/</span><span class="nt">label</span><span class="p">&gt;</span>
  766. <span class="p">&lt;</span><span class="nt">my</span><span class="err">-</span><span class="na">input</span> <span class="na">id</span><span class="p">=</span><span class="s">"input"</span> <span class="na">name</span><span class="p">=</span><span class="s">"myInput"</span> <span class="na">value</span><span class="p">=</span><span class="s">"test"</span> <span class="na">required</span><span class="p">&gt;&lt;/</span><span class="nt">my</span><span class="err">-</span><span class="na">input</span><span class="p">&gt;</span>
  767. <span class="p">&lt;</span><span class="nt">button</span> <span class="na">type</span><span class="p">=</span><span class="s">"reset"</span><span class="p">&gt;</span>Reset<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
  768. <span class="p">&lt;</span><span class="nt">button</span><span class="p">&gt;</span>Submit<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
  769. <span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
  770. </code></pre>
  771. <div class="highlight__panel js-actions-panel">
  772. <div class="highlight__panel-action js-fullscreen-code-action">
  773. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  774. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  775. </svg>
  776. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  777. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  778. </svg>
  779. </div>
  780. </div>
  781. </div>
  782. <h3>
  783. <a name="-raw-formstaterestorecallbackstate-mode-endraw-" href="#-raw-formstaterestorecallbackstate-mode-endraw-">
  784. </a>
  785. <strong><code>formStateRestoreCallback(state, mode)</code></strong>
  786. </h3>
  787. <p>This method callback gives the developer control over what happens when the browser completes form elements. The <code>state</code> property provides the value that is set using the <code>setFormValue</code> and the <code>mode</code> has two possible values - <code>restore</code> and <code>autocomplete</code>. The <code>restore</code> value is set when a user navigates away from a form and back to it again allowing them to continue where they left off. The <code>autocomplete</code> value is used when a browser’s input-assist tries to autocomplete the form. The downside to the <code>autocomplete</code> feature is that according to <a href="https://web.dev/more-capable-form-controls/#restoring-form-state">this article</a>, it is not supported yet. In that case, we can use a simple implementation to restore the input to the saved value.<br>
  788. </p>
  789. <div class="highlight js-code-highlight">
  790. <pre class="highlight tsx"><code><span class="nx">formStateRestoreCallback</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span> <span class="nx">mode</span><span class="p">)</span> <span class="p">{</span>
  791. <span class="k">this</span><span class="p">.</span><span class="nx">$input</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">state</span><span class="p">;</span>
  792. <span class="p">}</span>
  793. </code></pre>
  794. <div class="highlight__panel js-actions-panel">
  795. <div class="highlight__panel-action js-fullscreen-code-action">
  796. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title>
  797. <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path>
  798. </svg>
  799. <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title>
  800. <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path>
  801. </svg>
  802. </div>
  803. </div>
  804. </div>
  805. <p>
  806. </p>
  807. <h2>
  808. <a name="conclusion" href="#conclusion">
  809. </a>
  810. Conclusion
  811. </h2>
  812. <p>As you can see, this only scratches the surface of the potential of what these new APIs can do. We only implemented two attributes out of the many that are available in an <code>input</code> element and things get even more interesting when you introduce other form elements like <code>select</code>, <code>textarea</code>, and <code>button</code>. Hopefully, this gives you a solid start with creating form-associated custom elements. Happy coding!</p>
  813. </article>
  814. <hr>
  815. <footer>
  816. <p>
  817. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  818. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  819. </svg> Accueil</a> •
  820. <a href="/david/log/" title="Accès au flux RSS"><svg class="icon icon-rss2">
  821. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-rss2"></use>
  822. </svg> Suivre</a> •
  823. <a href="http://larlet.com" title="Go to my English profile" data-instant><svg class="icon icon-user-tie">
  824. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-user-tie"></use>
  825. </svg> Pro</a> •
  826. <a href="mailto:david%40larlet.fr" title="Envoyer un courriel"><svg class="icon icon-mail">
  827. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-mail"></use>
  828. </svg> Email</a> •
  829. <abbr class="nowrap" title="Hébergeur : Alwaysdata, 62 rue Tiquetonne 75002 Paris, +33184162340"><svg class="icon icon-hammer2">
  830. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-hammer2"></use>
  831. </svg> Légal</abbr>
  832. </p>
  833. <template id="theme-selector">
  834. <form>
  835. <fieldset>
  836. <legend><svg class="icon icon-brightness-contrast">
  837. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-brightness-contrast"></use>
  838. </svg> Thème</legend>
  839. <label>
  840. <input type="radio" value="auto" name="chosen-color-scheme" checked> Auto
  841. </label>
  842. <label>
  843. <input type="radio" value="dark" name="chosen-color-scheme"> Foncé
  844. </label>
  845. <label>
  846. <input type="radio" value="light" name="chosen-color-scheme"> Clair
  847. </label>
  848. </fieldset>
  849. </form>
  850. </template>
  851. </footer>
  852. <script src="/static/david/js/instantpage-5.1.0.min.js" type="module"></script>
  853. <script>
  854. function loadThemeForm(templateName) {
  855. const themeSelectorTemplate = document.querySelector(templateName)
  856. const form = themeSelectorTemplate.content.firstElementChild
  857. themeSelectorTemplate.replaceWith(form)
  858. form.addEventListener('change', (e) => {
  859. const chosenColorScheme = e.target.value
  860. localStorage.setItem('theme', chosenColorScheme)
  861. toggleTheme(chosenColorScheme)
  862. })
  863. const selectedTheme = localStorage.getItem('theme')
  864. if (selectedTheme && selectedTheme !== 'undefined') {
  865. form.querySelector(`[value="${selectedTheme}"]`).checked = true
  866. }
  867. }
  868. const prefersColorSchemeDark = '(prefers-color-scheme: dark)'
  869. window.addEventListener('load', () => {
  870. let hasDarkRules = false
  871. for (const styleSheet of Array.from(document.styleSheets)) {
  872. let mediaRules = []
  873. for (const cssRule of styleSheet.cssRules) {
  874. if (cssRule.type !== CSSRule.MEDIA_RULE) {
  875. continue
  876. }
  877. // WARNING: Safari does not have/supports `conditionText`.
  878. if (cssRule.conditionText) {
  879. if (cssRule.conditionText !== prefersColorSchemeDark) {
  880. continue
  881. }
  882. } else {
  883. if (cssRule.cssText.startsWith(prefersColorSchemeDark)) {
  884. continue
  885. }
  886. }
  887. mediaRules = mediaRules.concat(Array.from(cssRule.cssRules))
  888. }
  889. // WARNING: do not try to insert a Rule to a styleSheet you are
  890. // currently iterating on, otherwise the browser will be stuck
  891. // in a infinite loop…
  892. for (const mediaRule of mediaRules) {
  893. styleSheet.insertRule(mediaRule.cssText)
  894. hasDarkRules = true
  895. }
  896. }
  897. if (hasDarkRules) {
  898. loadThemeForm('#theme-selector')
  899. }
  900. })
  901. </script>
  902. </body>
  903. </html>