Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Shadow DOM let us achieve?

I think shadow DOM lets us achieve style encapsulation and also hiding HTML implementation of the component.

But when I inspect shadow root in chrome I can see the HTML of the component. enter image description here

So what exactly does it help us to achieve? Is it only style encapsulation?

like image 751
Narayan Prusty Avatar asked Apr 22 '16 09:04

Narayan Prusty


People also ask

What is the purpose of a shadow Dom?

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.

Does Shadow DOM improve performance?

First off, it's true that shadow DOM can improve style performance, so our theory about style encapsulation holds up. However, ID and class selectors are fast enough that actually it doesn't matter much whether shadow DOM is used or not – in fact, they're slightly faster without shadow DOM.

What is Shadow DOM in selenium?

Shadow DOM refers to the ability of the browser to include a subtree of DOM elements into the rendering of a document, but not into the main document DOM tree. Shadow DOM enables encapsulation. With Shadow DOM, a component can have its own “shadow” DOM tree that cannot be accidentally accessed from the main document.

What is the Shadow DOM in angular?

Shadow DOM is like a parallel DOM tree hosted inside a component (an HTML element, not to be confused with Angular components), hidden away from the main DOM tree. No part of the application has access to this shadow DOM other than the component itself.


1 Answers

Sure, the DevTools allow you to investigate the shadow DOM but if you get the HTML for index.html querySelector('body').innerHTML the shadow DOM of the elements is not included.

You explicitly need to switch to the shadow DOM of a custom element to get access to that HTML.

Shadow DOM is not about hiding your elements implementation from other developers, it's about hiding it from CSS, JS or other means that might accidentally read or manipulate it.

This allows to break down complexity of the DOM of a whole application to smaller parts that are better manageable.

like image 151
Günter Zöchbauer Avatar answered Oct 14 '22 02:10

Günter Zöchbauer