title: HTML with Superpowers
url: https://daverupert.com/2021/10/html-with-superpowers/
hash_url: eb14bb1071cfe2bd952aa9280c6a4860

<p>Earlier this year I spoke at An Event Apart’s Spring Summit and today I’m happy to share with you that talk called <em>HTML with Superpowers</em>, which is all about Web Components.</p>
<p><a href="https://noti.st/davatron5000/k6N2LP/html-with-superpowers" class="button">View slides on Notist</a></p>
<p>This talk had two major objectives. Firstly, In January 2021 I joined the <a href="https://www.w3.org/community/webcomponents/">W3C Web Components Community Group</a>, which is hoping to advance Web Components and raise awareness. It’s a worthwhile cause to get over some of Web Components’ historical marketing problems. I also get a chance to provide feedback on nascent stage proposals. I’m a fringe member in this group, but happy to escalate any questions or feedback you have.</p>
<p>The second reason I wanted to talk about Web Components is that I feel there’s an education gap.  I noticed that nearly all the people I know that love HTML, CSS, and Design Systems… they don’t use Web Components. It makes sense when you realize a lot of web component education targets JavaScript developers. Every <em>How-To Web Components</em> post on <a href="http://Dev.to">Dev.to</a> starts with the class-based JavaScript API. That’s great… if you like JavaScript. But if I’m 100% honest, if you already have a JS framework that works for you, I don’t see much point in switching. That’s an intense amount of sunk costs and organizational entropy to overcome. I wanted to write a talk that approaches Web Components from a different angle.</p>
<p>Emulating <a href="https://speaking.adactio.com/87IIn1#sn3gBTn">Jeremy Keith’s pacing layers for the Web</a>, which emulates Stuart Brand’s pacing layers for architecture, I start with the slower layers of the Web. The layers unlikely to change from under you and then build up to the “<a href="https://www.youtube.com/watch?v=RFm9ClqlGuo">Jeremy Beremy</a>” velocity of JavaScript. That means starting with HTML, then talk about CSS, then talk about JavaScript.</p>
<h2>Part 1: How to use Web Components with HTML</h2>
<p>Let’s start with HTML. Let’s use Web Components that someone else built. No build tools. No NPM install blah blah. Copy and paste a script tag from the web like the old days. I walk through half a dozen examples on how to use Web Components to make interactive sites, reusable components, and even 3D realms using HTML and a humble <code>&lt;script&gt;</code> tag.</p>
<p>My favorite is <a href="https://github.com/GoogleChromeLabs/two-up"><code>&lt;two-up&gt;</code> from Google Chrome Labs</a> which you can use in your code today to make one of those neat before/after picture sliders.</p>
<pre><code class="language-html">&lt;two-up&gt;
  &lt;div&gt;&lt;img src="before.jpg" alt="before"&gt;&lt;/div&gt;
  &lt;div&gt;&lt;img src="after.jpg" alt="after"&gt;&lt;/div&gt;
&lt;/two-up&gt;

&lt;script src="path/to/two-up.js"&gt;&lt;/script&gt;
</code></pre>
<p>I love it because I didn’t have to code it myself. I didn’t have to fuss with absolute positioning, pointer events, nothing. It gives me a great effect with ~5 lines of HTML.</p>
<h2>Part 2: How to style Web Components with CSS</h2>
<p>In the second part of the talk we come face to face with the most frustrating part of Web Components… styling.</p>
<p>Styling Web Components is the biggest foot gun of the entire Web Component system because it involves something called “Piercing the Shadow DOM”. The Shadow DOM is a boundary inside the component where no styles bleed in and no styles bleed out… but some styles do pass through! Why??!? This is figure-outable, but it’s not intuitive and can leave you with a sour first experience.</p>
<pre><code class="language-css">/* This does not work */
custom-alert button {
  background: pink;
}
</code></pre>
<p>When some styles pass through the shadow boundary and some styles don’t, it creates a bit of a frustrating developer experience. A loss of agency. As a CSS professional, I’m used to having access to style nearly everything on the page. But Web Components break that social contract and there’s not even a mechanism to say “Hey, I’m an expert, let me style this.”</p>
<p>I did the thing where I chucked a lot of CSS at a Web Component to see what sticks. We also walk through options like CSS Custom Properties and CSS Named Parts that you can add to your components to offer more customizable styling APIs.</p>
<h2>Part 3: Build Web Components with JS</h2>
<p>The final step, I sort of bring it all together and write a <code>&lt;custom-alert&gt;</code> component using Lit. It’s a hundred lines of code and I’d wager it’s half CSS. If I could redo the talk I’d probably rip out the styles and layer them in at the end.</p>
<p>The biggest takeaway that I hope people get is that components have a lifecycle, like a React or Vue component. And there are micro-libraries that give you different flavors of Web Components to suit your niche aesthetics. They also have a bit of reactive component state built-in and I could probably cover that more better next time around. Point being… they’re probably a little bit better than the last time you checked them out.</p>
<h2>tl;dr</h2>
<p>I think if you were using Web Components before 2020 you were an early adopter and you probably have some scars to show for it. But in 2021, now that all modern browsers support Web Components, I think they’re worth investigating. They have one superpower that no other JavaScript framework offers called the Shadow DOM which is both powerful but frustrating.  But another superpower — the power I’m most excited about — is that you can use them standalone without any frameworks, build tools, or package managers.</p>
<p>And from there, if we have good, robust Web Components… I believe they can inform the Web Platform and we can get more native elements in the future.</p>
<p>If you have questions, want to air grievances, or would like to know more about Web Components, hit me up on Twitter. I’m also putting together a workshop on Web Components so if that interests you or your company please reach out.</p>
