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.  

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
There has been no shortage of both fair and unfair criticism toward Web Components from a wide range of folks that build for the web, including but not limited to JavaScript Framework authors in supposed competition with the platform. In this talk I'll show you how to navigate and simplify the multifaceted landscape of web components, satisfying common criticisms and showing how you can Use the Platform most effectively today.
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
Over the past few years, the number of web frameworks available to us has exploded. In some ways, the breadth of choice is a clear win for our ecosystem. However, for many of us, it also comes with harsh drawbacks: - Have you ever used a popular open-sourced component built for framework A, and wished it existed in framework B? What about a design system library? - Does your company have frontends built in different frameworks, and your web teams are frustrated about the wasted hours needed to achieve a consistent design system? - Does your team build SDKs for web frameworks, and must manually re-write them for each framework? The solution to all 3 of these problems exists today. To fully understand it, we must first examine today’s web frameworks, re-think what a component should look like, and introduce a new Intermediate Representation of our components. This is what we have done at Builder.io when we created Mitosis, and we’re excited to share it with everyone.
Web Components, Lit and Santa 🎅
JSNation Live 2021JSNation Live 2021
28 min
Web Components, Lit and Santa 🎅
Get started with Web Components – enabling you to define new HTML elements that work everywhere regular HTML does. This talk will focus on Lit, a suite from Google that helps you create WCs with features you'd expect like data-binding and declarative definitions. It'll also cover how we've used them to build one of the web's jolliest sites, Google's Santa Tracker 🎅
Authoring HTML in a JavaScript World
React Summit US 2023React Summit US 2023
21 min
Authoring HTML in a JavaScript World
 In this talk, Tony shows how an authoring and semantic HTML-first approach to JSX template work leads to more readable, maintainable, and accessible UI components. He demonstrates how to think through implementing a UI prototype in a semantic way, authoring HTML before visuals. He shows how accessible markup improves performance, reduces DOM size, minimizes time spent on CSS, and reduces cognitive load not only for developers, but also for all our users, no matter how they consume our sites and applications.
Immutable Web Apps
JSNation 2022JSNation 2022
20 min
Immutable Web Apps
Resolving dependencies when they are all bundled together is easy. Resolving dependencies when they are in being loaded via script tags is much more challenging. The goal of this talk is to explain how Meltwater handles dependency resolution when building native Web Component based applications that rely on packages published by many different teams.
Web Components are awesome!
JSNation 2022JSNation 2022
10 min
Web Components are awesome!
Ever wondered how by placing "video" or "audio" into your HTML, you get a media player with controls included? Or how, depending on the type attribute, "input" can be a button, a place to enter text, select a date or file, color picker and more? What if you could create your own element? The answer: Web Components! 🤯 In this talk, we’ll take a look at what Web Components are, how to make one and include it into an application.

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.