Web Components are a set of web platform APIs that allow you to create reusable, custom HTML elements. These components encapsulate their functionality, making it easier to build complex user interfaces while maintaining clean and modular code. The three main technologies that make up Web Components are:
In this blog post, we'll focus on Custom Elements and Shadow DOM, as they form the core of Web Components.
Custom Elements allow you to define your own HTML tags with custom behavior. This feature enables you to create reusable components that can be used across your application or even shared with other developers.
Let's create a simple custom element called <hello-world>
:
class HelloWorld extends HTMLElement { constructor() { super(); this.textContent = 'Hello, World!'; } } customElements.define('hello-world', HelloWorld);
Now you can use this custom element in your HTML:
<hello-world></hello-world>
This will display "Hello, World!" on the page.
Shadow DOM provides a way to encapsulate the structure, style, and behavior of your custom elements. It creates a separate DOM tree that is isolated from the main document, preventing style conflicts and maintaining a clean separation of concerns.
Let's enhance our <hello-world>
element with Shadow DOM:
class HelloWorld extends HTMLElement { constructor() { super(); const shadow = this.attachShadow({ mode: 'open' }); const wrapper = document.createElement('span'); wrapper.textContent = 'Hello, World!'; wrapper.style.color = 'blue'; shadow.appendChild(wrapper); } } customElements.define('hello-world', HelloWorld);
Now the styles applied to the <span>
element inside our custom element won't affect or be affected by the rest of the document.
Let's create a more complex example that combines Custom Elements and Shadow DOM to create a reusable card component:
class UserCard extends HTMLElement { constructor() { super(); const shadow = this.attachShadow({ mode: 'open' }); const template = ` <style> .card { border: 1px solid #ccc; border-radius: 4px; padding: 16px; max-width: 300px; } .name { font-weight: bold; font-size: 1.2em; } .email { color: #666; } </style> <div class="card"> <div class="name"></div> <div class="email"></div> </div> `; shadow.innerHTML = template; this.nameElement = shadow.querySelector('.name'); this.emailElement = shadow.querySelector('.email'); } connectedCallback() { this.nameElement.textContent = this.getAttribute('name') || 'Unknown'; this.emailElement.textContent = this.getAttribute('email') || 'No email provided'; } } customElements.define('user-card', UserCard);
Now you can use this component in your HTML like this:
<user-card name="John Doe" email="john@example.com"></user-card> <user-card name="Jane Smith" email="jane@example.com"></user-card>
This will create two user cards with different information, each styled independently thanks to Shadow DOM.
Web Components and Shadow DOM offer a powerful way to create reusable, encapsulated UI elements using vanilla JavaScript. By leveraging these technologies, you can build more modular and maintainable web applications without relying on heavy frameworks. As you continue your journey in JavaScript mastery, exploring Web Components will undoubtedly expand your toolkit for creating robust and scalable web applications.
22/10/2024 | VanillaJS
14/09/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS