Comments on: Light-DOM-Only Web Components are Sweet https://frontendmasters.com/blog/light-dom-only/ Helping Your Journey to Senior Developer Sun, 08 Jun 2025 19:55:06 +0000 hourly 1 https://wordpress.org/?v=6.9 By: Chris Coyier https://frontendmasters.com/blog/light-dom-only/#comment-29841 Sun, 08 Jun 2025 19:55:06 +0000 http://fem.flywheelsites.com/?p=166#comment-29841 In reply to Yawar.

Looks like some stuff was lost in translation here. Feel free to use Markdown triple-backtick blocks in a new comment and I can swap them out.

<div>which should allow html</div>
]]>
By: Yawar https://frontendmasters.com/blog/light-dom-only/#comment-29831 Sun, 08 Jun 2025 15:29:13 +0000 http://fem.flywheelsites.com/?p=166#comment-29831 Hi Chris, this post really resonates with me. I want to also say that the other huge benefit of Light DOM is that we can render it on any backend server with no special concerns about browser support. And we don’t even need to lose the functionality that ‘slots’ give us if we use HTML generation libraries in the backend that just use function calls. Eg suppose you have a component:

This message should be passed in as a ‘slot’.

We can use a library like FastHTML ( https://www.fastht.ml/ ) to define the rendering function like this:

from fasthtml.components import alert_message

def AlertMessage(msg_slot):
return alert_message(message_slot)

Obviously this is just a simplified example for demonstration purposes but the custom element can be rendered with many more slots and attributes.

]]>
By: Samuel C.Tyler https://frontendmasters.com/blog/light-dom-only/#comment-13850 Tue, 22 Oct 2024 13:41:18 +0000 http://fem.flywheelsites.com/?p=166#comment-13850 Personally, I prefer shadow DOM with a lot of css variables. You can style fine with css variables and it won’t get in the way of your necessary css. As another option, you could pass a style object as a prop and use that to style where you want or allow complete overriding of styles. What’s the point of styling your component so it works everywhere, onky to have someone else inadvertently overwrite it then complain or file a bug report about it? IMHO, it’s not worth the hassle. Giving up the power of shadow dom for allowing users to overwrite your styles is not a good trade, imo.

]]>
By: James https://frontendmasters.com/blog/light-dom-only/#comment-8807 Thu, 08 Aug 2024 12:46:00 +0000 http://fem.flywheelsites.com/?p=166#comment-8807 Wait then how exactly does YouTube work with their custom web components like and because whenever I expand them, I don’t see #shadow-root but instead I see , and I know they use Polymer. Does Polymer have anything to do with this, and how could I do something similar?

]]>
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

]]>