Confession: in my Web Components journey I’ve used Shadow DOM on a few components and it made sense to do so and it worked well (no regrets).
Remember that these different styles of component are tools in the toolbox.
Shadow DOM isn’t a silver bullet (or even a good default choice) but when you understand the tradeoffs you’re making and weigh those against the purpose and use case of the component, it sometimes makes sense to use it!
(And if you’re new to Web Components, don’t start here)
29 replies
@zachleat at the moment Web Components are my silver bullet.
@_web_ glad you’re getting mileage out of them!
-
-
@bkardell feel the shadow dom flow through you
-
@zachleat I'm super down with "Shadow DOM is not a good default"
@chriscoyier @zachleat I'm curious to hear what you both think are the common "default" problems Shadow DOM fails to solve?
Are there any specific shortcomings where you think there are better alternatives?
@develwithoutacause @zachleat It's styling for me. ::part and custom properties sneaking through are not real styling solutions. Plopping a chunk of CSS into a template literal I think is clunky and weird. Adopted stylesheets are another request and also weird. So just screw it. Light DOM. I want to use my regular CSS to style things. *generally*.
@chriscoyier i’m also in this camp, but slots are only usable with shadow DOM. @justinfagnani proposed “open-stylable” shadow roots which seem like the best of both worlds: https://github.com/WICG/webcomponents/issues/909
@jakelazaroff @chriscoyier @justinfagnani @zachleat I get the need, but I feel like open-styleable is a nuclear option.
I'm all for improvements to APIs like CSS parts, and I'd rather fix those than throw away style isolation altogether.
@develwithoutacause @jakelazaroff @chriscoyier @justinfagnani I love the style isolation too but requiring shadow DOM for this jumps ahead a few steps for me! Is it future-feasible to get slots and style isolation with light DOM?
@zachleat @develwithoutacause @jakelazaroff @chriscoyier @justinfagnani
It makes no sense to me to use slots without shadow DOM.
How do you envision this being used and how it would work?I mean, if you're "just" cloning a template to replace the initial light DOM, it's 10 lines to `querySelector` the initial children and move them to the cloned template & replace children.
→ https://codepen.io/tbroyer/details/vYbmbXxBut that's not really "slotting" for me: you lose dynamic manipulation of slots/slottables.
@zachleat @develwithoutacause @jakelazaroff @chriscoyier @justinfagnani
Wrt style isolation, would a CSS reset within @scope (with a lower bound) work?
(I did use CSS scoping in the above pen, but didn't add a CSS reset)@tbroyer it would just transclude the slotted content in place of the <slot /> element, no? basically the same as react children/svelte slots/etc
@jakelazaroff @zachleat @develwithoutacause @chriscoyier @justinfagnani Show me some code 😉
(what you write in the HTML and JS, and what the final DOM looks like: because without shadow dom, the light dom is modified, how could you have transclusion rather than moving nodes around? react/svelte are different, they create the DOM from scratch whereas WCs start with DOM children already)
How different would it be from what I did in this pen?
@tbroyer haven’t been at a computer all day so can’t inspect the resulting DOM tree in your pen, but i think i’m describing what you seem to be doing. tbh i’m not really opinionated about the implementation; i just think this particular cow path is extremely well traveled, so we should pave it. it’s nice that it can be done in userland but imo the platform should provide a solution too.
@jakelazaroff @tbroyer @zachleat @develwithoutacause @chriscoyier light DOM slots just aren't really possible.
Any proposal would have to describe the assignment algorithm: what nodes are slotted where. I haven't seen anyone do this, because... there's no way to tell what element any slot actually belongs too, so what elements are potentially slottable, and then how to avoid circularity issues.
Shadow DOM solves this with two trees, and I think two trees are just necessary.
@jakelazaroff @tbroyer @zachleat @develwithoutacause @chriscoyier this is why I think the solution to a few shadow DOM problems is to decouple the encapsulation features, because slots are the most fundamental thing that SD does, and you can't get interoperable composition other ways.
@develwithoutacause @chriscoyier ah I did a whole talk on this! 😅
https://www.zachleat.com/web/jsheroes/ is probably the best video of it—I wish I had a blog post representing the thoughts in the talk, hmm
@chriscoyier @zachleat
I think context is important here regarding defaults. For my personal components I‘d rarely use Shadow DOM, but for a lib with a lot of uncertaint usage scenarios and distribution on a per-component basis, Shadow DOM seems like a reasonable default in a lot of cases.
@zachleat is there a write-up you recommend (offhand, I can Google myself if you don't have one in mind) of the trade-offs?
@paddy I did a talk on this topic! https://www.zachleat.com/web/jsheroes/ (I need to write a blog post too 😭)
@zachleat there are many good use cases for shadow dom!
ultra isolated content like data visualization, animated illustrations, ads, third-party widgets, video players, 3D objects…
these use cases are very specialized, and will only form a small percentage of most websites consist of
@hi_mayank @zachleat would you say Shadow DOM is good for obscuring complexity that isn't needed for the user?
@DavidDarnes @hi_mayank it’s the DOM we pile into the closet and say our room is clean
@zachleat Good to know as I have yet to start the journey.