Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Light DOM and Shadow DOM

I stumbled a couple of times into this Light DOM thing but wasn't able to understand the difference with Shadow DOM. If someone out there could provide a clear answer it would be much appreciated.

like image 241
theFreedomBanana Avatar asked Feb 07 '17 15:02

theFreedomBanana


People also ask

What is light DOM and shadow DOM?

With shadow DOM, CSS styles defined in a parent component don't apply to a child component. Contrastingly, light DOM enables styling from the root document to target a DOM node and style it. The styles on the following native shadow component cascades into the child component's light DOM.

What is the difference between DOM and shadow DOM?

In short, the shadow DOM is a browser technology whose main objective is to provide encapsulation when creating elements. On the other hand, the virtual DOM is managed by JavaScript libraries—e.g., React—and it's mostly a strategy to optimize performance.

What is difference between virtual DOM and shadow DOM in React?

Is the Shadow DOM the same as the Virtual DOM? No, they are different. The Shadow DOM is a browser technology designed primarily for scoping variables and CSS in web components. The virtual DOM is a concept implemented by libraries in JavaScript on top of browser APIs.

What is shadow DOM good for?

Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with a shadow root, underneath which you can attach any element, in the same way as the normal DOM.


1 Answers

The Light DOM is simply the plain old DOM tree inside a HTML element.

The term is only used in the context of web components to distinguish it from the Shadow DOM. I suppose the normal DOM was redefined as Light in contrast with Shadow.

The specs call it the shadowroot host's node tree, or light tree:

A shadow root is always attached to another node tree through its host. A shadow tree is therefore never alone. The node tree of a shadow root’s host is sometimes referred to as the light tree.

I call it the normal DOM :-)


The Shadow DOM is the added DOM that recovers, masks, or replaces the normal DOM, as explained in the article from Google.

The rendered DOM can be a combination of the Shadow DOM and the Light DOM (through <slot> elements)

Note: it's not possible to completely polyfill the Shadow DOM behaviour in JavaScript, so Shadow DOM polyfills actually deal with normal DOM trees only.

like image 98
Supersharp Avatar answered Sep 24 '22 07:09

Supersharp