123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <!doctype html><!-- This is a valid HTML5 document. -->
- <!-- Screen readers, SEO, extensions and so on. -->
- <html lang="fr">
- <!-- Has to be within the first 1024 bytes, hence before the `title` element
- See: https://www.w3.org/TR/2012/CR-html5-20121217/document-metadata.html#charset -->
- <meta charset="utf-8">
- <!-- Why no `X-UA-Compatible` meta: https://stackoverflow.com/a/6771584 -->
- <!-- The viewport meta is quite crowded and we are responsible for that.
- See: https://codepen.io/tigt/post/meta-viewport-for-2015 -->
- <meta name="viewport" content="width=device-width,initial-scale=1">
- <!-- Required to make a valid HTML5 document. -->
- <title>Understanding Kristofer Joseph's Single File Web Component (archive) — David Larlet</title>
- <meta name="description" content="Publication mise en cache pour en conserver une trace.">
- <!-- That good ol' feed, subscribe :). -->
- <link rel="alternate" type="application/atom+xml" title="Feed" href="/david/log/">
- <!-- Generated from https://realfavicongenerator.net/ such a mess. -->
- <link rel="apple-touch-icon" sizes="180x180" href="/static/david/icons2/apple-touch-icon.png">
- <link rel="icon" type="image/png" sizes="32x32" href="/static/david/icons2/favicon-32x32.png">
- <link rel="icon" type="image/png" sizes="16x16" href="/static/david/icons2/favicon-16x16.png">
- <link rel="manifest" href="/static/david/icons2/site.webmanifest">
- <link rel="mask-icon" href="/static/david/icons2/safari-pinned-tab.svg" color="#07486c">
- <link rel="shortcut icon" href="/static/david/icons2/favicon.ico">
- <meta name="msapplication-TileColor" content="#f7f7f7">
- <meta name="msapplication-config" content="/static/david/icons2/browserconfig.xml">
- <meta name="theme-color" content="#f7f7f7" media="(prefers-color-scheme: light)">
- <meta name="theme-color" content="#272727" media="(prefers-color-scheme: dark)">
- <!-- Documented, feel free to shoot an email. -->
- <link rel="stylesheet" href="/static/david/css/style_2021-01-20.css">
- <!-- See https://www.zachleat.com/web/comprehensive-webfonts/ for the trade-off. -->
- <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>
- <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>
- <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>
- <link rel="preload" href="/static/david/css/fonts/triplicate_t3_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
- <link rel="preload" href="/static/david/css/fonts/triplicate_t3_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
- <link rel="preload" href="/static/david/css/fonts/triplicate_t3_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
- <script>
- function toggleTheme(themeName) {
- document.documentElement.classList.toggle(
- 'forced-dark',
- themeName === 'dark'
- )
- document.documentElement.classList.toggle(
- 'forced-light',
- themeName === 'light'
- )
- }
- const selectedTheme = localStorage.getItem('theme')
- if (selectedTheme !== 'undefined') {
- toggleTheme(selectedTheme)
- }
- </script>
-
- <meta name="robots" content="noindex, nofollow">
- <meta content="origin-when-cross-origin" name="referrer">
- <!-- Canonical URL for SEO purposes -->
- <link rel="canonical" href="https://til.simonwillison.net/web-components/understanding-single-file-web-component">
-
- <body class="remarkdown h1-underline h2-underline h3-underline em-underscore hr-center ul-star pre-tick" data-instant-intensity="viewport-all">
-
-
- <article>
- <header>
- <h1>Understanding Kristofer Joseph's Single File Web Component</h1>
- </header>
- <nav>
- <p class="center">
- <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
- <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
- </svg> Accueil</a> •
- <a href="https://til.simonwillison.net/web-components/understanding-single-file-web-component" title="Lien vers le contenu original">Source originale</a>
- </p>
- </nav>
- <hr>
- <p><a href="https://twitter.com/brianleroux/status/1453472609518034944" rel="nofollow">Via Brian LeRoux</a> I found <a href="https://gist.github.com/kristoferjoseph/c4e47389ae0f0447db175b914e471628">single-file-web-component.html</a> by Kristofer Joseph. It's really clever! It demonstrates how to build a <code><hello-world></hello-world></code> custom Web Component in a single HTML file, using some neat tricks.</p>
- <p>For my own education I spent some time picking it apart and built my own annotated version of the code showing what I learned.</p>
- <p>Justin Fagnani <a href="https://twitter.com/justinfagnani/status/1453531485240102916" rel="nofollow">provided useful feedback</a> on this on Twitter.</p>
- <div class="highlight highlight-text-html-basic"><pre><span class="pl-c1"><!DOCTYPE html<span class="pl-kos">></span></span>
- <span class="pl-kos"><</span><span class="pl-ent">html</span> <span class="pl-c1">lang</span>="<span class="pl-s">en</span>"<span class="pl-kos">></span>
- <span class="pl-kos"><</span><span class="pl-ent">head</span><span class="pl-kos">></span>
- <span class="pl-kos"><</span><span class="pl-ent">meta</span> <span class="pl-c1">charset</span>="<span class="pl-s">UTF-8</span>" />
- <span class="pl-kos"><</span><span class="pl-ent">title</span><span class="pl-kos">></span>Single File Web Component<span class="pl-kos"></</span><span class="pl-ent">title</span><span class="pl-kos">></span>
- <span class="pl-kos"><</span><span class="pl-ent">style</span><span class="pl-kos">></span>
- <span class="pl-ent">body</span> {
- <span class="pl-c1">background-color</span><span class="pl-kos">:</span> <span class="pl-pds"><span class="pl-kos">#</span>eee</span>;
- <span class="pl-c1">font-family</span><span class="pl-kos">:</span> Helvetica<span class="pl-kos">,</span> sans-serif;
- }
- <span class="pl-ent">h1</span> {
- <span class="pl-c1">color</span><span class="pl-kos">:</span> blue;
- <span class="pl-c1">background-color</span><span class="pl-kos">:</span> pink;
- }
- <span class="pl-kos"></</span><span class="pl-ent">style</span><span class="pl-kos">></span>
- <span class="pl-kos"></</span><span class="pl-ent">head</span><span class="pl-kos">></span>
- <span class="pl-kos"><</span><span class="pl-ent">body</span><span class="pl-kos">></span>
- <span class="pl-kos"><</span><span class="pl-ent">template</span> <span class="pl-c1">id</span>="<span class="pl-s">single-file</span>"<span class="pl-kos">></span>
- <span class="pl-kos"><</span><span class="pl-ent">style</span><span class="pl-kos">></span>
- <span class="pl-c">/*</span>
- <span class="pl-c"> These styles affect only content inside the shadow DOM.</span>
- <span class="pl-c"></span>
- <span class="pl-c"> Styles in the outside document mostly do not affect stuff</span>
- <span class="pl-c"> here, but there are some exceptions: font-family affects</span>
- <span class="pl-c"> this <h1> for example. I don't understand the rules here.</span>
- <span class="pl-c"> */</span>
- <span class="pl-ent">h1</span> {
- <span class="pl-c1">color</span><span class="pl-kos">:</span> red;
- }
- <span class="pl-kos"></</span><span class="pl-ent">style</span><span class="pl-kos">></span>
-
- <span class="pl-kos"><</span><span class="pl-ent">h1</span><span class="pl-kos">></span>Hello world (web component)<span class="pl-kos"></</span><span class="pl-ent">h1</span><span class="pl-kos">></span>
- <span class="pl-c"><!--</span>
- <span class="pl-c"> This code still works if you remove the type="module" parameter.</span>
- <span class="pl-c"></span>
- <span class="pl-c"> Using that parameter enables features such as 'import ... from'</span>
- <span class="pl-c"></span>
- <span class="pl-c"> More importantly it stops variables inside the script tag from</span>
- <span class="pl-c"> leaking out to the global scope - if you remove type="module"</span>
- <span class="pl-c"> from here then the HelloWorld class becomes visible.</span>
- <span class="pl-c"> --></span>
- <span class="pl-kos"><</span><span class="pl-ent">script</span> <span class="pl-c1">type</span>="<span class="pl-s">module</span>"<span class="pl-kos">></span>
- <span class="pl-k">class</span> <span class="pl-v">HelloWorld</span> <span class="pl-k">extends</span> <span class="pl-v">HTMLElement</span> <span class="pl-kos">{</span>
- <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
- <span class="pl-c">/*</span>
- <span class="pl-c"> If you remove the call to super() here Firefox shows an error:</span>
- <span class="pl-c"> "Uncaught ReferenceError: must call super constructor before</span>
- <span class="pl-c"> using 'this' in derived class constructor'"</span>
- <span class="pl-c"> */</span>
- <span class="pl-smi">super</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
- <span class="pl-k">const</span> <span class="pl-s1">template</span> <span class="pl-c1">=</span> <span class="pl-smi">document</span><span class="pl-kos">.</span><span class="pl-en">getElementById</span><span class="pl-kos">(</span><span class="pl-s">"single-file"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
- <span class="pl-c">/*</span>
- <span class="pl-c"> mode: 'open' means you are allowed to access</span>
- <span class="pl-c"> document.querySelector('hello-world').shadowRoot to get</span>
- <span class="pl-c"> at the DOM inside. Set to 'closed' and the .shadowRoot</span>
- <span class="pl-c"> property will return null.</span>
- <span class="pl-c"> */</span>
- <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-en">attachShadow</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-c1">mode</span>: <span class="pl-s">"open"</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">appendChild</span><span class="pl-kos">(</span>
- <span class="pl-s1">template</span><span class="pl-kos">.</span><span class="pl-c1">content</span><span class="pl-kos">.</span><span class="pl-en">cloneNode</span><span class="pl-kos">(</span><span class="pl-c1">true</span><span class="pl-kos">)</span>
- <span class="pl-kos">)</span><span class="pl-kos">;</span>
- <span class="pl-c">/* </span>
- <span class="pl-c"> template.content is a 'DocumentFragment' array.</span>
- <span class="pl-c"></span>
- <span class="pl-c"> template.content.cloneNode() without the true performs</span>
- <span class="pl-c"> a shallow clone, which provides a empty DocumentFragment</span>
- <span class="pl-c"> array.</span>
- <span class="pl-c"></span>
- <span class="pl-c"> template.content.cloneNode(true) provides one with 6 items</span>
- <span class="pl-c"> */</span>
- <span class="pl-kos">}</span>
- <span class="pl-en">connectedCallback</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
- <span class="pl-c">// https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#using_the_lifecycle_callbacks</span>
- <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">"Why hello there 👋"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
- <span class="pl-kos">}</span>
- <span class="pl-kos">}</span>
- <span class="pl-s1">customElements</span><span class="pl-kos">.</span><span class="pl-en">define</span><span class="pl-kos">(</span><span class="pl-s">"hello-world"</span><span class="pl-kos">,</span> <span class="pl-v">HelloWorld</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
- <span class="pl-kos"></</span><span class="pl-ent">script</span><span class="pl-kos">></span>
- <span class="pl-kos"></</span><span class="pl-ent">template</span><span class="pl-kos">></span>
-
- <span class="pl-kos"><</span><span class="pl-ent">h1</span><span class="pl-kos">></span>This is not a web component<span class="pl-kos"></</span><span class="pl-ent">h1</span><span class="pl-kos">></span>
-
- <span class="pl-kos"><</span><span class="pl-ent">hello-world</span><span class="pl-kos">></span><span class="pl-kos"></</span><span class="pl-ent">hello-world</span><span class="pl-kos">></span>
-
- <span class="pl-kos"><</span><span class="pl-ent">script</span><span class="pl-kos">></span>
- <span class="pl-k">const</span> <span class="pl-s1">sf</span> <span class="pl-c1">=</span> <span class="pl-smi">document</span><span class="pl-kos">.</span><span class="pl-en">getElementById</span><span class="pl-kos">(</span><span class="pl-s">"single-file"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
- <span class="pl-c">/*</span>
- <span class="pl-c"> Before executing this line, sf.content.lastElementChild</span>
- <span class="pl-c"> is the <script type="module"> element hidden away inside</span>
- <span class="pl-c"> the <template> - we remove it from the template here and</span>
- <span class="pl-c"> append it to the document.body, causing it to execute in</span>
- <span class="pl-c"> the main document and activate the <hello-world> tag.</span>
- <span class="pl-c"> */</span>
- <span class="pl-smi">document</span><span class="pl-kos">.</span><span class="pl-c1">body</span><span class="pl-kos">.</span><span class="pl-en">appendChild</span><span class="pl-kos">(</span><span class="pl-s1">sf</span><span class="pl-kos">.</span><span class="pl-c1">content</span><span class="pl-kos">.</span><span class="pl-c1">lastElementChild</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
- <span class="pl-kos"></</span><span class="pl-ent">script</span><span class="pl-kos">></span>
- <span class="pl-kos"></</span><span class="pl-ent">body</span><span class="pl-kos">></span>
- <span class="pl-kos"></</span><span class="pl-ent">html</span><span class="pl-kos">></span></pre></div>
-
- <p class="created">Created 2021-10-27T17:33:08-07:00, updated 2021-10-27T19:48:23-07:00 · <a href="https://github.com/simonw/til/commits/main/web-components/understanding-single-file-web-component.md">History</a> · <a href="https://github.com/simonw/til/blob/main/web-components/understanding-single-file-web-component.md">Edit</a></p>
- </article>
-
-
- <hr>
-
- <footer>
- <p>
- <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
- <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
- </svg> Accueil</a> •
- <a href="/david/log/" title="Accès au flux RSS"><svg class="icon icon-rss2">
- <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-rss2"></use>
- </svg> Suivre</a> •
- <a href="http://larlet.com" title="Go to my English profile" data-instant><svg class="icon icon-user-tie">
- <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-user-tie"></use>
- </svg> Pro</a> •
- <a href="mailto:david%40larlet.fr" title="Envoyer un courriel"><svg class="icon icon-mail">
- <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-mail"></use>
- </svg> Email</a> •
- <abbr class="nowrap" title="Hébergeur : Alwaysdata, 62 rue Tiquetonne 75002 Paris, +33184162340"><svg class="icon icon-hammer2">
- <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-hammer2"></use>
- </svg> Légal</abbr>
- </p>
- <template id="theme-selector">
- <form>
- <fieldset>
- <legend><svg class="icon icon-brightness-contrast">
- <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-brightness-contrast"></use>
- </svg> Thème</legend>
- <label>
- <input type="radio" value="auto" name="chosen-color-scheme" checked> Auto
- </label>
- <label>
- <input type="radio" value="dark" name="chosen-color-scheme"> Foncé
- </label>
- <label>
- <input type="radio" value="light" name="chosen-color-scheme"> Clair
- </label>
- </fieldset>
- </form>
- </template>
- </footer>
- <script src="/static/david/js/instantpage-5.1.0.min.js" type="module"></script>
- <script>
- function loadThemeForm(templateName) {
- const themeSelectorTemplate = document.querySelector(templateName)
- const form = themeSelectorTemplate.content.firstElementChild
- themeSelectorTemplate.replaceWith(form)
-
- form.addEventListener('change', (e) => {
- const chosenColorScheme = e.target.value
- localStorage.setItem('theme', chosenColorScheme)
- toggleTheme(chosenColorScheme)
- })
-
- const selectedTheme = localStorage.getItem('theme')
- if (selectedTheme && selectedTheme !== 'undefined') {
- form.querySelector(`[value="${selectedTheme}"]`).checked = true
- }
- }
-
- const prefersColorSchemeDark = '(prefers-color-scheme: dark)'
- window.addEventListener('load', () => {
- let hasDarkRules = false
- for (const styleSheet of Array.from(document.styleSheets)) {
- let mediaRules = []
- for (const cssRule of styleSheet.cssRules) {
- if (cssRule.type !== CSSRule.MEDIA_RULE) {
- continue
- }
- // WARNING: Safari does not have/supports `conditionText`.
- if (cssRule.conditionText) {
- if (cssRule.conditionText !== prefersColorSchemeDark) {
- continue
- }
- } else {
- if (cssRule.cssText.startsWith(prefersColorSchemeDark)) {
- continue
- }
- }
- mediaRules = mediaRules.concat(Array.from(cssRule.cssRules))
- }
-
- // WARNING: do not try to insert a Rule to a styleSheet you are
- // currently iterating on, otherwise the browser will be stuck
- // in a infinite loop…
- for (const mediaRule of mediaRules) {
- styleSheet.insertRule(mediaRule.cssText)
- hasDarkRules = true
- }
- }
- if (hasDarkRules) {
- loadThemeForm('#theme-selector')
- }
- })
- </script>
- </body>
- </html>
|