-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShadowDom.txt
25 lines (16 loc) · 2.06 KB
/
ShadowDom.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
The Shadow DOM is a web technology used in modern web development to encapsulate and isolate a piece of HTML, CSS, and JavaScript from the rest of the web page. This means that the styles and scripts defined within the Shadow DOM will not affect or be affected by the outside document. It's a part of the Web Components technology suite, which allows for the creation of reusable custom elements.
Key Features of Shadow DOM
Encapsulation: Styles and scripts inside a Shadow DOM are scoped to the element itself. This prevents style conflicts and ensures that the component's appearance and behavior remain consistent regardless of the surrounding environment.
Reusability: Components that use Shadow DOM can be reused across different parts of an application or even across different projects without worrying about conflicting styles or scripts.
DOM isolation: The Shadow DOM provides a separate, hidden DOM tree attached to an element, preventing external access and manipulation, which enhances security and stability.
How it Works
A shadow host: An ordinary DOM node that the Shadow DOM is attached to.
Shadow tree: The DOM tree inside the Shadow DOM, which is separate from the main document's DOM.
Shadow boundary: The boundary between the Shadow DOM and the regular DOM, preventing styles and scripts from leaking in or out.
In HTML and JavaScript, a Shadow DOM can be attached to an element like this:
// Find the element to host the Shadow DOM
const hostElement = document.querySelector('#shadow-host');
// Attach a Shadow DOM to the host element
const shadowRoot = hostElement.attachShadow({ mode: 'open' });
// Now you can append HTML, styles, and scripts to shadowRoot, and they will be encapsulated within the Shadow DOM
In this example, shadowRoot acts as the root node of the Shadow DOM, and anything added to it (like elements, styles, or scripts) will be encapsulated and isolated from the rest of the page. The mode can be open (allowing JavaScript from the main page to access the Shadow DOM) or closed (preventing access from the main page, which further encapsulates the Shadow DOM).