Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Shadow DOM when we have iframes?

Tags:

html

iframe

web

I heard of shadow DOM which seems to solve the problem of encapsulation in web widget development. DOM and CSS rules get encapsulated which is good for maintenance. But then isn't this what iframes are for? What problems are there with iframes that made it necessary for W3C to come up with Shadow DOM or HTML5 Web Components?

like image 387
Le Curious Avatar asked May 21 '13 18:05

Le Curious


People also ask

Why do we need shadow DOM?

Shadow DOM is very important for web components because it allows specific sections of the HTML document to be completely isolated from the rest of the document. This means CSS styles that are applied to the DOM are not applied to the Shadow DOM, and vice versa.

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 frameworks use shadow DOM?

By using the Shadow DOM. Nowadays, most frontend web developers build their UI with well-known libraries and frameworks such as React, Angular, Vue, and so on. Maybe many developers have almost forgotten how to dynamically create HTML elements by accessing the Document Object Model (DOM).

What is shadow DOM and how it works?

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.


2 Answers

Today, iframes are commonly used to assure separate scope and styling. Examples include Google's map and YouTube videos.

However, iframes are designed to embed another full document within the current HTML document. This means accessing values in a given DOM element in an iframe from the parent document is a hassle by design. The DOM elements are in a completely separate context, so you need to traverse the iframe’s DOM to access the values you’re looking for. Contrast this with web components which offer an elegant way to expose a clean API for accessing the values of custom elements.

Imagine creating a page using a set of 5 iframes that each contain one component. Each component would need a separate URL to host the iframe’s content. The resulting markup would be littered with iframe tags, yielding markup with low semantic meaning that is also clunky to read and manage. In contrast, web components support declaring rich semantic tags for each component. These tags operate as first class citizens in HTML. This aids the reader (in other words, the maintenance developer).

In summary, while both iframes and the shadow DOM provide encapsulation, only the shadow DOM was designed for use with web components and thus avoids the excessive separation, setup overhead, and clunky markup that occurs with iframes.

like image 167
Cory House Avatar answered Sep 29 '22 06:09

Cory House


iframes are use as just encapsulation objects...

with the exception of SVG (more on that later), today’s Web platform offers only one built-in mechanism to isolate one chunk of code from another — and it ain’t pretty. Yup, I am talking about iframes. For most encapsulation needs, frames are too heavy and restrictive.

Shadow DOM allows you to provide better and easier encapsulation, by creating another clone of the DOM or part of it.

For example imagine you build a widget (as I have) that is used across websites. You widget might be affected by the css on the page and look horrible, whereas with Shadow DOM it will not :)

Here is an excellent article on the topic:

What The Heck is Shadow DOM/

like image 42
Dory Zidon Avatar answered Sep 29 '22 05:09

Dory Zidon