A place to cache linked articles (think custom and personal wayback machine)
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.md 6.2KB

před 3 roky
12345678910111213141516171819202122232425262728293031323334353637
  1. title: HTML with Superpowers
  2. url: https://daverupert.com/2021/10/html-with-superpowers/
  3. hash_url: eb14bb1071cfe2bd952aa9280c6a4860
  4. <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>
  5. <p><a href="https://noti.st/davatron5000/k6N2LP/html-with-superpowers" class="button">View slides on Notist</a></p>
  6. <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>
  7. <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>
  8. <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>
  9. <h2>Part 1: How to use Web Components with HTML</h2>
  10. <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>
  11. <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>
  12. <pre><code class="language-html">&lt;two-up&gt;
  13. &lt;div&gt;&lt;img src="before.jpg" alt="before"&gt;&lt;/div&gt;
  14. &lt;div&gt;&lt;img src="after.jpg" alt="after"&gt;&lt;/div&gt;
  15. &lt;/two-up&gt;
  16. &lt;script src="path/to/two-up.js"&gt;&lt;/script&gt;
  17. </code></pre>
  18. <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>
  19. <h2>Part 2: How to style Web Components with CSS</h2>
  20. <p>In the second part of the talk we come face to face with the most frustrating part of Web Components… styling.</p>
  21. <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>
  22. <pre><code class="language-css">/* This does not work */
  23. custom-alert button {
  24. background: pink;
  25. }
  26. </code></pre>
  27. <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>
  28. <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>
  29. <h2>Part 3: Build Web Components with JS</h2>
  30. <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>
  31. <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>
  32. <h2>tl;dr</h2>
  33. <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>
  34. <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>
  35. <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>