21 ms·
Declarative Shadow DOM
- deleted 4y ago[deleted]
- pyrolistical 4y agoCan’t wait for this to be everywhere. React was amazing to get us where we are today but this shadow dom without js will allow server side rendering components without react
- misterbwong 4y agoAfter this TP gets released, it looks like FF will be the only outlier[1] [1] https://caniuse.com/declarative-shadow-dom https://caniuse.com/declarative-shadow-dom
- j79 4y agoThankfully Firefox recently changed their stance from "Neutral" to "Positive". https://github.com/mozilla/standards-positions/pull/740 https://github.com/mozilla/standards-positions/pull/740 > Neutral stance: We're not convinced that the complexity this feature introduces upon the HTML parser carries its weight in terms of usefulness for web developers. There's also a risk that the processing model is not compatible with a future declarative custom elements feature as it was developed in isolation. Having said that, the proposal is a reasonable approach for this functionality that takes into account the various constraints and security considerations that come with changing the HTML parser. > Positive Stance: This is a reasonable proposal which takes into account the various constraints and security considerations that come with changing the HTML parser. Hopefully that's a sign of things to come! (Fingers crossed)
- dmitriid 4y agoYou don't need React to server-side render anything, including components. Besides, if you need components, web components are probably the last thing you want to reach for. Edit: Also, none of the major frameworks, and very few of the new frameworks target web components for many reason fully ignored by browser vendors. They can "consume" them (that is embed them in code), but at most compile to web components, reluctantly. And still provide components and SSR, and have for years.
- lab14 4y agoWhy would you need react to render server side components?
- ricardobeat 4y ago> many modern websites and web-based applications deploy a technique called “server-side rendering” whereby programs running on a web server generate HTML markup with the initial content for web browsers to consume, instead of fetching content over the network once scripts are loaded I chuckled reading this. It’s like someone landed in 2020 and learned web development while being completely unaware of events in the preceding decade.
- jalino23 4y agoclient-side rendering was coined because server-side rendering was the default. now that client-side rendering is ubiquitous, being explicit about server-side rending became necessary and along lost in translation became "technique" - is what I think happened here.
- mr_toad 4y agoProbably too late to change now, but I wish the term ‘rendering’ wasn’t used to describe completely different things. Server side rendering is about dynamically generating HTML code. Client side rendering generates DOM objects in memory, and only rarely generates HTML strings. And then there’s actual rendering, which generates pixels on a display. People who aren’t familiar with web development often conflate these concepts.
- deleted 4y ago[deleted]
- CharlesW 4y ago> I chuckled reading this. It’s like someone landed in 2020 and learned web development while being completely unaware of events in the preceding decade. Note that they mean SSR specifically here vs. just server-generated web pages. Server-rendering of component updates is a post-SPA innovation (2018?), so it still seems worth explaining for most web developers.
- dvt 4y ago> Server-rendering of component updates is a post-SPA innovation (2018?), so it still seems worth explaining for most web developers. Absolutely not true. I was doing this in 2009 and so were many other people (albeit through janky XMLHttpRequest via PHP polling ways).
- schemescape 4y agoI’m not familiar with Shadow DOM. The links in the article seem to indicate that the main use case is working around how CSS styling works. Surely there is more to it than that. What am I missing?
- infogulch 4y agoThere was a thing called "scoped css" back in 2012 that never went anywhere which seems similar. But I think Shadow DOM also mixes in some notion of DOM access protection iirc. Also interested in a description of what shadow dom is and what it can be used for. https://developer.chrome.com/blog/a-new-experimental-feature-style-scoped/ https://developer.chrome.com/blog/a-new-experimental-feature...
- a13o 4y agoThe isolation was primarily designed with Web Components in mind, but it's popular as well in browser extensions. Content-scripts might want to inject elements into a host page without being subject to that page's styles or JavaScript. (e.g. the host page calling normalize() on their DOM.)
- throwaway894345 4y agoIt exists to help you define components without running into naming conflicts. For example, if I want to make a "my-button" component that has a <button class="button"> and some CSS that targets `.button { ... }`, I won't end up styling a bunch of other things that have `class=button`. Pretty sure it also protects against other things doing `document.getElementById("button")` and getting the element whose ID is `button` from your component instead of whatever the caller was expecting. Effectively, shadow DOM provides encapsulation.
- rossmohax 4y agoIs it accurate to mentally model this feature as browser mounting/applying CSS not to the document, but to the arbitrary DOM element in it?
- 4y ago
- whatever1 4y agoWith this SSR trend do we now need three job families ? The front end engineer, the render-backend engineer and the api-backend engineer?
- wizzwizz4 4y agoYou're forgetting the UX designer, the UI designer, the UX researcher, the UI translator, the database administrator, the system administrator… We have always been able to break complex tasks down into multiple, simpler job descriptions. That doesn't mean each person is limited to doing at most one of them.
- chrisweekly 4y agoI've seen candidates describe themselves as "Front of the Front-End" in the context of job interviews where the web UI stack included a BFF (Backend-for-Frontend, eg Next.js or Remix). It actually makes sense. The corresponding "Back of the FE" skillset encompasses proxy servers, API gateways, routing, observability, browser networking, server APIs, deployment infr, etc., on top of pure UI per se. There's a breadth of knowledge required that puts many backend-oriented "full-stack" devs to shame.
- rektide 4y agoNolan Lawson's recent talk on CSS Runtime Performance[1] show some pretty amazing styling performance wins for Shadow DOM. It's nice we can do more of this easily! I think this should all work outside <template> tags. Re-remembering how they worked slowed down my comprehension some. The MDN article[2] was a solid refresher. [1] https://nolanlawson.com/2023/01/17/my-talk-on-css-runtime-performance/ https://nolanlawson.com/2023/01/17/my-talk-on-css-runtime-pe... [2] https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots https://developer.mozilla.org/en-US/docs/Web/Web_Components/...
- gardenhedge 4y agoThanks for posting this. Very interesting. After watching I opened up dev tools to look at performance. Looks very complicated!
- nolanl 4y agoThanks for the shout-out! I think I mention this in the talk, but note that YMMV. I designed that benchmark as a kind of "worst-case scenario" where shadow DOM / scoped styles really show a benefit. Depending on your CSS rules, DOM size, and amount of thrashing, the perf benefit could be small to large. Also, it's still possible to shoot yourself in the foot, especially if you have a large/complex stylesheet repeated across multiple shadow roots. (Not because of the repetition – that's optimized in browsers [1] – but rather because of the number of DOM nodes affected.) That said, I still think the perf benefits of shadow DOM have been undersung. And Declarative Shadow DOM makes it way more useful. [1]: https://github.com/whatwg/dom/issues/831#issuecomment-585489960 https://github.com/whatwg/dom/issues/831#issuecomment-585489...
- rickstanley 4y agoVery good! I can already see Lit-Element[0], one of the beneficiaries, leveraging this! When I first started writing/exploring Shadow DOM API I felt so weird writing inline `class`, but then again, it's just a syntactic sugar for prototype functions. - [0]: https://lit.dev/docs/ssr/overview/ https://lit.dev/docs/ssr/overview/
- azangru 4y ago> I felt so weird writing inline `class`, but then again, it's just a syntactic sugar for prototype functions You felt weird using standard ES6 syntax? Did you prefer writing Thing.prototype.method = function(){}... ? Or Derived.prototype = Object.create(Base.prototype)?
- rezonant 4y agoI think they mean class expression (ie const c = class {}) versus class declaration (class c {}). Both are indeed ES standard though.
- tehbeard 4y ago> This meant that this feature was not available when JavaScript is disabled such as in email clients This reason to be add it seems, really out of touch with reality. I mean, most of html is not available, or horrendously borked in email clients. Hence the continuation of table within table within table to make anything other than plain text.
- chrismorgan 4y agoI believe Microsoft’s email clients are all still using the MSO renderer, which is basically a very incomplete and quite buggy implementation of HTML 3.2, considerably worse than IE 5.0. As far as I can tell, the only thing that has changed in the last 25 years is the addition of high-DPI support (which is mildly imperfect, and still requires a conditional comment shibboleth in many cases).
- WorldMaker 4y agoDesktop Outlook (the "flagship" to many, because of its prominence in Big Enterprise) is the only one left using something related to the MSO renderer, and even it seems to have some sort of heuristics now that it switches to what seems to be WebView 2 (embedded Edge Chromium) for some emails (I don't know what those heuristics are or if non-Microsoft originating emails can opt-in to that behavior). The rest of their email clients including "Mobile" Outlook seem to all use some version of WebView 2 (or are possibly entirely inside WebView 2/React Native). The "One Outlook" convergence allegedly draws nigh and that will be really interesting to see how much of the "MSO-style" rendering survives.
- chrismorgan 4y agoWindows 10’s Mail app used MSO when I looked at it a few years back, and I don’t expect it’s changed. Therefore it wouldn’t surprise me if their new Outlook app (currently https://apps.microsoft.com/store/detail/outlook-for-windows/9NRX63209R7B https://apps.microsoft.com/store/detail/outlook-for-windows/...) also used MSO, though I hope not. Not sure about what you say on traditional Outlook supporting WebView2 rendering; searching isn’t finding me even a single mention of it, although I’d expect it to be very big news in the email industry. https://www.litmus.com/blog/a-guide-to-rendering-differences-in-microsoft-outlook-clients/ https://www.litmus.com/blog/a-guide-to-rendering-differences... was posted from six months ago and is basically unaltered from what one would have said in 2007 (and not much from what one would have said in 2000).
- no_wizard 4y agoDoes this mean we'll finally get Template-Instantiation[0], providing a native way to do HTML templates in the browser? There's been a bunch of hand waving that the reason we still don't have this is because they needed a solution for encapsulation and a solution for SSR. Now we have both. Can we please finally deliver on something people have been asking for since the mid 2000s? [0]: https://github.com/WICG/webcomponents/blob/gh-pages/proposals/Template-Instantiation.md https://github.com/WICG/webcomponents/blob/gh-pages/proposal...
- spankalee 4y agoDeclarative shadow roots and Template Instantiation are very different proposals with no overlap. The only relation here is that with some critical feature done like this and shadow selection, there will be more time to work on Template Instantiation.
- no_wizard 4y agoI know, as I highlighted, it appears to me that all the hurdles for Template Instantiation have been passed, and its about time we start to see actual movement and traction on this. its been in the pipeline since 2017 with little major movement on it, while a bunch of things developers didn't actually ask for have managed to have more precedence. Whenever I've pressed for reasons for this, it gets hand waved away that they need solutions for SSR and encapsulation. We have both now. Honestly should have shipped day one with Web Components, would have made it far more compelling. It has always felt like such a big miss to me by the standards bodies that this didn't get attention.
- Andrex 4y agoI was always really bullish on Web Components since the first whispers started... Then I wanted to start building non-JS websites. And suddenly "Web" Components just weren't an option. Now I've dug even further to SSR, and the shortcoming is painful. I don't want to use Next.js, I want to use HTML. I want standards. Maybe in 4-5 years this will finally be a solved problem...
- di4na 4y agoSo as someone that work only with server side rendered stuff, you know, no JS on the browser and css loaded as one big blob, is there something useful for me here? I feel i am missing the point completely... I really feel like html has been totally forgotten by the power that be that work on CSS...
- skerit 4y agoFinally. Not being able to pre-render shadow dom on the server is what made me just skip it entirely.
- azangru 4y agoIs the repeated inclusion of the <style> tag into every copy of a declaratively rendered component — with the same css strings repeated every time for every instance of a component — a concern at all?
- mattigames 4y agoYou can use good old regular link tags as well to avoid that kind of repetition.
- spankalee 4y agogzip/brotli handles this very well, but it still is text to parse through. Some form of declarative CSS module scripts would help a lot. A feature request for that here: https://github.com/WICG/webcomponents/issues/939 https://github.com/WICG/webcomponents/issues/939
- jacobr 4y agoAlmost all examples of Shadow DOM uses Custom Elements, but that’s not required. I think this conflation harms adoption of Shadow DOM. As an example of what you can use the Shadow DOM for - it works fine as the node to render a React app/component in. So if you need a couple of React components on a page to not affect each others style, you can do React.createRoot(myShadowRoot) and they’re fully encapsulated.
- MuffinFlavored 4y agoWhen would somebody want to use Shadow DOM instead of just the regular DOM? Is shadow DOM the next incarnation of the previous fad before it (virtual DOM)?
- vhiremath4 4y agoWhen you have an extension that modifies a given page/injects them with some elements and you don't want the class names or styles of those elements to interfere with the class names and styles of the parent page. There are lots of extensions like this.
- ithkuil 4y agoIt scopes styling
- jacobr 4y agoFor example, encapsulating a third party widget, cookie banner or your main navigation. That way any CSS added inside the shadow DOM only affects those elements - you can safely write selectors like `h1` or `button` and they’ll only match what’s inside that same shadow root boundary.
- tomxor 4y agoThe utility of the shadow DOM is in controlled isolation for both CSS and JS, because nether can arbitrarily reach into a shadow DOM's tree via the parent DOM. Although they can be made to intentionally affect it through the root element in a useful but controlled way. A virtual DOM is just an abstraction in JS; whereas the shadow DOM is a feature of the browser engine. They are not really comparable, a shadow DOM _is_ the DOM. The confusion may come from the fact that it can be used in custom elements where MVCs might be involved again.
- tambourine_man 4y agoWhen people start claiming that Safari is the new IE, dragging its feet, etc, I just point them to the WebKit blog. Apple just has different priorities and visions for the Web than Google.
- taeric 4y agoSo, it is exactly like IE, in many ways? :D It isn't like IE was completely lacking of innovation. To wit, div/span/XMLHttpRequest/BorderBox. Lots of things did come from there. I remember they used fieldset and legend in a much cleaner way, too.
- nextaccountic 4y ago> XMLHttpRequest Yep, Microsoft invented Ajax, so in a sense it was IE that made the Web 2.0 possible
- taeric 4y agoI was taking a quick check to see if I was correct on the div/span claim. I am actually not finding anything to corroborate that. I also want to add that I have little love for IE. Same for some of the other things that Microsoft have done. I just find it odd the juxtaposition of why some of that behavior is fine today, as long as it is someone else doing it.
- layer8 4y agoThe problem with IE was that they basically stopped after IE 6. The innovation was from before. After the release of IE 6, the IE team was basically disbanded, because MS thought they had won the browser wars (which at the time, they had). That was in 2001. IE 7 was only released in 2006, over five years later. And in 2008 Google released the first version of Chrome.
- taeric 4y agoWell, that and it was Microsoft. They had an earned reputation for extend/extinguish. They have mostly shed it with VSCode, but I honestly can't see how. Seems to be in a very similar runbook to how they used to be?
- mgerdts 4y agoI first encountered shadow roots while trying to use tampermonkey to work around a problem with the latest release of Gerrit. What I thought would be a relatively trivial effort to find the right element and modify its style turned into a lot more effort. https://gist.github.com/mgerdts/f508ae81795097397106156fa291bafa https://gist.github.com/mgerdts/f508ae81795097397106156fa291... While I get that the purpose of the shadow roots is to provide isolation, is there a simple way to break through that isolation without manually walking the DOM tree and traversing all of the .shadowRoot elements? It seems Gerrit wraps most elements with a shadow root. For someone that doesn't do front-end development, is there an explanation for how this is helpful?
- jjcm 4y agoCSS variables help here quite a bit. I typically will add in component-specific tokens as css variables, that way I have simple penetration into the shadowdom.
- irishcarbomb777 4y agoOne nice way of handling this is to use the CSSStyleSheet object. In a nutshell, you can create a module like shadowCssRules.js and in that create a CSSStyleSheet object and set your global rules ( * { box-sizing: border-box;} ) then in your WebComponent constructor you call this.shadowRoot.adoptedStyleSheets = [shadowCssSheet]. This MDN page has an example at the bottom https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/CSSStyleSheet https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleShe... Also allows you to update the style sheet from anywhere in your js and it’ll effect all the shadow doms that adopted it
- jacobr 4y agoUnfortunately none of that works with the feature in TFA because it’s not supported by Declarative Shadow DOM.
- sh4rks 4y agoShadow roots are a nightmare when doing any kind of Web scraping or UI integration testing
- bryanrasmussen 4y ago>In summary, declarative shadow DOM introduces an exciting new way of defining a shadow tree in HTML, which will be useful ...as well as in... email clients. I guess ... isn't really the thing to do there, as the parts redacted are not only important but it seems to me much more likely to be supported. Although maybe I am overly pessimistic about email clients?
- claytongulick 4y agoAs a huge fan of Web Components, I'm sort of strangely on the other side of shadow dom. I've been doing 100% of my UI work using vanilla Web Components for years now, and what I generally recommend is to avoid the use of shadow dom unless there's a good reason for it. The CSS parts API is difficult to work with, and if you want to use a CSS framework like bootstrap etc... you end up importing all the CSS into the component's shadow context. Web Components with the light dom work great, they're an effective encapsulation mechanism. I'm not sure why most examples you see with WC go straight to shadow DOM. It isn't necessary for most components and adds an lot of styling complexity.
- robbiejs 4y agoInteresting. What is the added styling complexity? I am interested in using Shadow DOM for the next version of my component DataGridXL (https://www.datagridxl.com/ https://www.datagridxl.com/) to prevent outside CSS from affecting the component. WHy do you say it is not worth it?
- claytongulick 4y agoFor that case, I probably would use Shadow DOM, it's great for library components. For application development, I prefer the light DOM.
- microflash 4y agoThis. I've started using web components extensively and eventually came to the same conclusion.
- robbiejs 4y agoSo the components you are talking about are simpler UI components for which Shadow DOM is overkill? For advanced components like a grid, you would still recommend going with Shadow Dom?
- deleted 4y ago[deleted]
- jjcm 4y agoI'm super excited to test performance of this, but also I'm very curious about adoption. This approach to templating seems great for simple elements, but often times when instantiating a custom element we also want to bind functionality into the elements of the DOM. The @ decorator is very common in many webcomponent libraries' (i.e. lit) html() function. I wonder if the tradeoff of a more declarative approach is worth it for those relying on that existing functionality.
- andrewingram 4y agoA couple of neat tricks you can do with this: * Declarative streaming with slots: https://enamel.pages.dev/ https://enamel.pages.dev/ * A countdown timer without client-side JS: https://dash.deno.com/playground/how-to-crash-a-browser https://dash.deno.com/playground/how-to-crash-a-browser The second one is mine, it's a massive hack and definitely shouldn't be done as it creates opens a new element every second and never closes them; but it was fun to see if it was possible.
- jskulski 4y agoSHADOW DOM
- megaman821 4y agoYou can do some interesting inline styling with this, like using the hover state: <div> <template shadowroot="open"> <style> :host { border: 3px solid blue; } :host(:hover) { border-color: pink; } </style> Hello World </template> </div>