Comments on: Light-DOM-Only Web Components are Sweet https://frontendmasters.com/blog/light-dom-only/ Helping Your Journey to Senior Developer Mon, 08 Apr 2024 19:19:39 +0000 hourly 1 https://wordpress.org/?v=6.5.2 By: Jordan Brennan https://frontendmasters.com/blog/light-dom-only/#comment-2107 Mon, 08 Apr 2024 19:19:39 +0000 http://fem.flywheelsites.com/?p=166#comment-2107 In reply to John Loy.

Hey John thanks for mentioning TAC CSS! I recently rebuilt a React-based design system into a TAC-based design system. We’re now dependency-free and framework-agnostic, down to just 7 tiny kb, and getting better SEO and a11y for free.

Something else caught my eye: “They end up discovering that they can’t use global styles”.
I was under that impression for the first few years of doing WC until I learned how much global styles do in fact penetrate Shadow DOM and that even global stylesheets can easily be imported into WC. There’s surprisingly 8 ways to style Shadow DOM and 5 of those are done from the outside in: https://jordanbrennan.hashnode.dev/8-ways-to-style-the-shadow-dom

]]>
By: John Loy https://frontendmasters.com/blog/light-dom-only/#comment-286 Fri, 12 Jan 2024 23:00:10 +0000 http://fem.flywheelsites.com/?p=166#comment-286 Thanks Chris!

The thoughts and approaches mentioned in this article dovetail nicely with those mentioned in https://jordanbrennan.hashnode.dev/tac-a-new-css-methodology, by the way.

Custom HTML tags sans-Shadow DOM make a ton of sense for app/site-specific “components” like page skeletons, layouts, idiosyncratic UI, and wrappers/customizations around 3rd-party components. At least, they make sense if you care deeply about your markup structure, SEO, A11y, Web Vitals like LCP and CLS, JS payload size, and progressive enhancement.

The Shadow DOM and its encapsulation capability, however, seems very appropriate and useful for packaged 3rd-party components as a way for component authors to control the component public API in terms of behavior, content (slots), and styles. Take, for example, https://shoelace.style/. It’s a great example of customization in tandem with the Shadow DOM through features like slots, CSS custom properties and CSS parts. If these sorts of components could be customized without limits, then the ways a consumer could break them would wreak havoc on maintenance and support. The same applies, I think, to a well-maintained component collection for an internal design system, like https://opensource.adobe.com/spectrum-web-components/.

In my experience, where folks tend to go sour on web components is trying to wrangle with how to use Shadow DOM for app-specific components. They end up discovering that they can’t use global styles, or their sacred Tailwind cow, querySelector doesn’t seem to work as they expect, and they’re like, “what’s the point of making things harder”, and they totally write off web components as a technology because XYZ popular framework doesn’t have these constraints.

The custom element aspect of web components, of course, doesn’t require using the Shadow DOM–you still get JS “upgrading” and sweet-looking CSS selectors for free–so I’m glad to hear folks are coming around more and more to using them for this.

]]>
By: Josh Stockdale https://frontendmasters.com/blog/light-dom-only/#comment-258 Wed, 10 Jan 2024 19:56:50 +0000 http://fem.flywheelsites.com/?p=166#comment-258 Shadow-DOM really does make styling terrible. You can pierce through with CSS vars but that means the web components have to be set up to allow theming with CSS vars. So color, type, shape, spacing/padding, all have to be CSS vars if you want to allow someone to theme. Using tailwind with Shadow-DOM web components is also really hard, mainly due to the fact you can’t allow any dynamic variants in styling without being explicit about the options. I really do like the idea of web components though, I’ll have to give Light-DOM a go.

You mentioned a benefit for Shadow-DOM as encapsulation (which I’ve used before as well) but really, should there be so much JS on your page that you have collisions? JS has just gotten way out of hand. Coming from the massive JS framework perspective you automatically think, “of course you should encapsulate” but really? Do you?

Thanks, great article.

]]>
By: Danny Engelman https://frontendmasters.com/blog/light-dom-only/#comment-256 Wed, 10 Jan 2024 18:49:29 +0000 http://fem.flywheelsites.com/?p=166#comment-256 Alas, your code only works in CodePens and JSFiddles because those execute the JavaScript after the DOM is parsed.
IRL Custom Elements can be defined before DOM is parsed, and since the connectedCallback runs on the Opening Tag… there won’t be any lightDOM .. yet..

]]>
By: Steven Ramirez https://frontendmasters.com/blog/light-dom-only/#comment-138 Wed, 20 Dec 2023 01:01:28 +0000 http://fem.flywheelsites.com/?p=166#comment-138 you can duplicate slot functionality with a single utility function. heres an example:

class NewComponent extends HTMLElement {
  connectedCallback() {
    const html = `
    <header>
      <slot name="hdr"></slot>
    </header>
    <main>
      <slot name="mn"></slot>
    </main>
    <footer>
      <slot name="ftr"></slot>
    </footer>`;
    this.childrenToSlots(html);
  }

  childrenToSlots(html, src_elems) {
    var template = document.createElement("template");
    template.innerHTML = html;

    const slots = template.content.querySelectorAll("slot");
    for (const slot of slots) {
      const slotChildren = this.querySelectorAll(`[slot='${slot.name}']`);
      slot.replaceWith(...slotChildren);
    }

    this.replaceChildren(template.content);
  }
}
customElements.define("new-comp", NewComponent);

https://jsfiddle.net/f5pr21ju/

]]>
By: Chris Coyier https://frontendmasters.com/blog/light-dom-only/#comment-43 Thu, 14 Dec 2023 22:30:12 +0000 http://fem.flywheelsites.com/?p=166#comment-43 See!

https://twitter.com/junaidbhura/status/1735426353103843409

]]>