How the Shadow DOM has Got You Covered?

Rate this content
Bookmark

We learn about possibilities of what DOM can do but have you ever wondered what all possibilities Shadow DOM can have. Well, In this talk I will help you understand how the Shadow DOM has got you covered. We will further deep dive into how you can use React with Web Components.  

This talk has been presented at React Day Berlin 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Shadow DOM is a hidden DOM tree that behaves differently from the Light DOM. It is used to encapsulate and isolate parts of the DOM and CSS.

Shadow DOM differs from Light DOM in its creation and usage. Shadow DOM provides encapsulation and composability, allowing elements to have their own styles and behaviors without affecting the rest of the webpage.

Examples of elements that use Shadow DOM include the text area, choose file button, video, and audio elements. These elements have built-in styles and functionality defined in the Shadow DOM.

You can create a Shadow DOM for an element using the 'attachShadow' method with the mode set to 'open' or 'closed'. This method attaches a shadow root to the element, allowing you to define its inner HTML and styles.

Encapsulation in Shadow DOM means that the styles and functionality defined within a Shadow DOM are scoped to that particular element and do not interfere with the rest of the webpage. This provides a way to create reusable web components with isolated styles.

Composability in Shadow DOM allows you to create web components that can accept external data and change dynamically. This is similar to passing props in React, and it is achieved using slots in Shadow DOM.

You can inspect the Shadow DOM in a browser by enabling the 'Show user agent shadow DOM' option in the browser's developer tools. This allows you to see and modify the elements and styles within the Shadow DOM.

A practical application of Shadow DOM is creating a custom web component, such as a button with unique behavior and styles. Another example is a Chrome extension that provides vocabulary assistance by displaying word meanings on any webpage.

Web components are custom elements that utilize Shadow DOM to encapsulate their styles and behavior. They are reusable and can be created by defining a class, attaching a shadow root, and defining the element's inner HTML and styles.

Shadow DOM does not directly improve SEO and search result visibility. However, by providing encapsulated and composable web components, it can enhance the user experience and interactivity of a webpage, which can indirectly contribute to better SEO performance.

Mukul Ramchandani
Mukul Ramchandani
18 min
12 Dec, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Shadow DOM allows for encapsulation and composability, enabling elements to have their own features without affecting the rest of the webpage. Custom elements in the Shadow DOM have their own behavior through encapsulation and scoped styles. Composability is key in software development, allowing for dynamic data passing. The Shadow DOM provides a way to modify the appearance of elements within it, but some properties are marked as important and cannot be changed. Building a Chrome extension using the Shadow DOM allows for composable and encapsulated experiences.

1. Introduction to Shadow DOM

Short description:

The Shadow DOM is a hidden DOM tree that behaves differently from the Light DOM. It allows for encapsulation and composability, enabling elements to have their own features and functions without affecting the rest of the webpage. The Shadow DOM is different from the Light DOM in how it is created on a specific element. It provides a way to encapsulate elements, similar to how different pizza slices have their own toppings. This allows for better organization and separation of functionality.

So, hello everybody. As you can see, the title of the talk, it's how the Shadow DOM has got you covered. Not, not has it ever got you covered. It has got you covered. And in the the hindsight, for sure. So in this Lightning Talk, we're going to see like how it has got you covered.

So let's let's start with the talk. So, hey, everyone. I'm Mukul and I'm working as a front-end engineer at Hubilo. Apart from work, I like, I'm very passionate about indie music, board games and who is not passionate about traveling? So yeah, what is Shadow DOM? To define it simply, it's a normal hidden tree, hidden DOM tree with differences in how it behaves with the rest of the Light DOM. So given to the context of the talk, the DOM we interact on a daily basis. We add, we append, we remove elements. It's a normal DOM, right? And the DOM that we are going to talk about, we are going to refer that normal DOM as a Light DOM and the DOM that we are going to talk extensively about is the Shadow DOM. So as you can see on the screen, like this text is in the Shadow DOM. You can see it is a placeholder text, right? Whenever you try to inspect this and when you try to find out, you usually don't find this specific text written anywhere in the DOM, but it is present, very present. It's present in the Shadow DOM. While this is another example, like if you see a choose file button, someone must have written a button CSS of it and how this element should come in the inline and the code. What should happen when someone clicks on the choose file button, but all we do is we type input type file and then magic happens and this element appears. But the whole CSS of this has been written somewhere in the Shadow DOM. So, yeah, so let's just move further to know more about the Shadow DOM.

So how it's different than the Light DOM? So as I said, Light DOM is the normal DOM and how it's different from that? So very first thing, it is different on how this specific Shadow DOM is created on a specific element. So for example, let's say there is an element called as, let's say a div element, but you want the shadow element to that development you can create it. So the way you create and you use it, it differs in that area. Another very amazing and one of my favorite features which Shadow DOM brings in is encapsulation and composability. So think about this, like think about this, like, let's say you're eating a pizza and on one slice of pizza you basically get all your veggies, all your, all your toppings on one slice. Another slice would have its own set of toppings. So it's kind of those pizza slice has everything in it encapsulated. So in the same manner, Shadow DOM has its property of encapsulation and composability. Having its own way of features functions and ability to perform on its own and instead of disturbing the rest of the functionality on the webpage. If we talk about the precedence, let's say an element having a normal DOM and a Shadow DOM.

2. Shadow DOM and Web Components

Short description:

Shadow DOM has a greater precedence than Light DOM. Some HTML elements have their own Shadow DOM, like Textarea, Choose File Button, Video, and Audio. These elements come with pre-built functionality and styling. However, not every element has its own Shadow DOM. To leverage the benefits of Shadow DOM, we can create our own elements, like a custom button, using web components and the Shadow DOM API.

So Shadow DOM is usually given a preference. Shadow DOM has a greater precedence than Light DOM is one of the another difference when compared with the Light DOM. Cool, moving on so but not everyone has its own Shadow. Like we took the example of Textarea, we took the example of Choose File Button. There are other examples like Video and Audio. If you see Video and Audio elements, they have a plus button, they have a Seek bar, they have a Options button, all those elements come handy to us. We just add a video tag and they just appear. So someone must have written a CSS and the complete code and complete functionality of it. So that all resides in the Shadow DOM. And the Select option, the dropdown, the CSS around it, all that resides into a Shadow DOM. But not every element in the given HTML elements has its own Shadow DOM. For example, a div element, for example a span element doesn't have its own Shadow DOM. So we need to dive deeper on how we can get the benefits of the Shadow DOM on the way we want it, the way we want to encapsulate it, the way we to add a composability in nature to our HTML elements.

So one of the ways where we can create a Shadow DOM, so let's say there is an element called a header and let's say we want to attach Shadow with mode open. This is a browser API that is available, Attach Shadow, which helps us basically attach a Shadow DOM and once, attach a Shadow DOM to the element that we just created, a header, and with this header what we can do is we can add an inner HTML and add a like Hello Shadow DOM and that would be added to that element as a Shadow DOM. We are gonna look into it very soon on how it appears in the browser. But that is not it right. If you think about it here, we don't want to attach Shadow DOM to the existing elements. We can have their own, because they have their own properties, they have their own way of behavior as per the element, for example input, header, H1, H2 tags, and whatsoever. Every element has their own behavior. So we don't want to hinder that. Instead we would like to use the Shadow DOM by creating something of our own. Like let's take the example of a button. Like let's say you want to have a button where it has its own way of behavior, own way of animation, own way of giving way of title to button. So hence, Shadow DOM, when we talk about the Shadow DOM, we have to bring picture, we have to bring web components into the picture. So web components. Today we are going to look into the web components from little from the Shadow DOM perspective instead of like bringing up re-usability point of view. That aspect would be covered a little bit as well. So if you think about, so if you see how I can create a web component, basically a web component, a complete custom element of my own, like let's say I want to create my cool button, my cool button, and if I want to like put anything here, how would I go about it? I would just create a class. I would disconnected callback and disconnected callback would be the functions like, yeah, now that we are in the react event, so connected callback is basically when something is mounted in the DOM and disconnected callback is when the element is unmounted in the DOM, so you get this callback and based on this callback, you can add listeners, you can add CSS, you can add whatever you want and you can add a behavior to it using this DOM.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

The Good, The Bad, and The Web Components
JSNation 2023JSNation 2023
29 min
The Good, The Bad, and The Web Components
Top Content
Web Components are a piece of reusable UI enabled by web standards and built into the web platform. They offer the potential for faster component initialization and less library overhead. Web Components can be created from scratch and utilized with existing component libraries. Shadow DOM and Declarative Shadow DOM provide benefits such as scoped CSS and server-rendered components. The tradeoff between not repeating oneself and achieving full server-side rendering support is discussed. User experience is deemed more important than developer experience.
It's Time to De-Fragment the Web
React Day Berlin 2022React Day Berlin 2022
34 min
It's Time to De-Fragment the Web
Top Content
Today's Talk introduces Mitosis, an open source project that solves the problem of building framework agnostic components. It supports JSX and Svelte syntax and outputs human-readable code for various web frameworks. Mitosis is already being used by enterprise customers and can be easily integrated with React, Svelte, and other frameworks. It saves time, allows for easy handling of framework version migrations, and enables efficient unit and integration testing.
Web Components, Lit and Santa 🎅
JSNation Live 2021JSNation Live 2021
28 min
Web Components, Lit and Santa 🎅
Web Components and the Lit library are introduced, highlighting their ability to create custom elements and replicate built-in components with different functionality. The use of semantic HTML and the benefits of web components in development are emphasized. The features of Lit, such as data binding and rendering, are discussed. The Santa Tracker is showcased as an example of web components being used in educational games. The compatibility of web components with other frameworks and their versatility in creating small widgets or large applications are highlighted.
Authoring HTML in a JavaScript World
React Summit US 2023React Summit US 2023
21 min
Authoring HTML in a JavaScript World
Watch video: Authoring HTML in a JavaScript World
This Talk by Tony Alicia focuses on authoring HTML in a JavaScript world. The speaker challenges developers to change their approach to building React components by starting with HTML first. By authoring HTML in a semantic way, readability and maintainability can be improved. Well-authored HTML provides better understanding of content and improves accessibility. It also has performance benefits by reducing DOM size. Investing time in HTML can save time and make applications more future-proof.
Immutable Web Apps
JSNation 2022JSNation 2022
20 min
Immutable Web Apps
Today's Talk discusses immutable web apps and their benefits, such as faster loading times and easy version tracking. The use of Universal Module Definition (UMD) style bundling allows for flexible dependency management and gradual upgrades. Tools like Webpack and Rollup provide ways to reference UMDs in bundles and automate dependency configuration. Arborist and YAML files help resolve dependency trees and handle conflicts, while the Orchard CLI tool automates dependency ordering. Internal and external dependencies can be initialized and managed effectively for optimal performance.
Web Components are awesome!
JSNation 2022JSNation 2022
10 min
Web Components are awesome!
Web components allow you to create your own HTML elements that can do anything you want, and they are supported by all modern browsers. Many companies, including YouTube and GitHub, use web components to enhance their websites. There are extensive tooling and libraries available for web component development. The Model Viewer Web Component enables the display of 3D models in Virtual and Augmented Reality without needing to know underlying technologies. Web components can be used with various frameworks and libraries, and there are resources available to help with compatibility.

Workshops on related topic

Web Components in Action
JSNation Live 2021JSNation Live 2021
184 min
Web Components in Action
Workshop
Joren Broekema
Alex Korzhikov
2 authors
The workshop extends JavaScript programming language knowledge, overviews general software design patterns. It is focused on Web Components standards and technologies built on top of them, like Lit-HTML and Lit-Element. We also practice writing Web Components both with native JavaScript and using Lit-Element. In the end we overview key tooling to develop an application - open-wc.