Shadow Tree

May 20, 2023

Shadow tree is a fundamental concept in web development that enables the creation of custom and reusable HTML elements. A shadow tree is a separate DOM tree that sits alongside the main document DOM tree, which is used to encapsulate a component’s internal structure, styling, and functionality. This way, the encapsulated content can be hidden from the rest of the document, preventing it from interfering with other elements while allowing developers to reuse code across multiple pages.

Shadow trees are typically used to create custom web components that can be easily integrated with other websites. Shadow DOM, which is the underlying technology behind shadow trees, provides a way to encapsulate and style a component’s internal structure without affecting the rest of the document. This provides web developers with a way to create custom HTML elements that behave similarly to built-in HTML elements, but with custom functionality and styling.

Purpose

The primary purpose of shadow tree is to create reusable and isolated custom HTML elements. With shadow trees, developers can encapsulate a component’s internal structure, styling, and functionality within a separate DOM tree. This provides a level of isolation that prevents the component’s content from interfering with other elements on the page.

Additionally, shadow trees allow developers to create custom HTML elements that can be easily reused across multiple web pages. For example, a custom video player can be created as a web component using shadow trees that can be easily integrated into any page that requires a video player.

Usage

Shadow trees are used extensively in web development to create custom web components. To create a custom web component using shadow trees, developers typically follow the following steps:

  1. Create a new HTML element that will be used as the custom component.
  2. Create a shadow tree using the attachShadow method of the element.
  3. Add HTML content to the shadow tree using the innerHTML property of the shadow root.
  4. Style the shadow tree using CSS.

Here’s an example of how to create a custom component using shadow trees:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Custom Video Player</title>
    <style>
      .player {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
      }
      video {
        width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <custom-video-player src="myvideo.mp4"></custom-video-player>
    <script>
      class CustomVideoPlayer extends HTMLElement {
        constructor() {
          super();
          const shadowRoot = this.attachShadow({ mode: "open" });
          shadowRoot.innerHTML = `
            <style>
              .player {
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                width: 100%;
                height: 100%;
              }
              video {
                width: 100%;
                height: auto;
              }
            </style>
            <div class="player">
              <video src="${this.getAttribute("src")}" controls></video>
            </div>
          `;
        }
      }
      customElements.define("custom-video-player", CustomVideoPlayer);
    </script>
  </body>
</html>

In this example, a custom video player is created using shadow trees. The CustomVideoPlayer class extends the HTMLElement class, and the attachShadow method is called to create a shadow tree for the component. The innerHTML property of the shadow root is then used to add HTML content to the shadow tree, including a video element that displays the video and a custom div element with the class player that is styled with CSS.

The customElements.define method is then used to define the new custom element, which can be used like any other HTML element. In this case, the new custom element is called custom-video-player.

<custom-video-player src="myvideo.mp4"></custom-video-player>

This creates a new custom video player element that can be used anywhere in the document. The src attribute specifies the video file to play, and the controls attribute adds the standard video controls to the player.