Created
October 28, 2024 13:45
-
-
Save robertsosinski/e6b44c85fc8bca7b1ff8a49e45e601ce to your computer and use it in GitHub Desktop.
A HTML page that uses Web Compoents, Templates with slot elements, and the Shadow DOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body> | |
<template id="custom-card-template"> | |
<h2><slot name="card-title">Default Card Title</slot></h2> | |
<p><slot name="card-content">Default content goes here.</slot></p> | |
</template> | |
<custom-card> | |
<span slot="card-title">My First Title</span> | |
<span slot="card-content">This is my first card content!</span> | |
</custom-card> | |
<custom-card> | |
<span slot="card-title">My Second Title</span> | |
<span slot="card-content">This is my second card content!</span> | |
</custom-card> | |
<script> | |
customElements.define('custom-card', class CustomCard extends HTMLElement { | |
constructor() { | |
super(); | |
const template = document.getElementById("custom-card-template"); | |
const templateContent = template.content; | |
const shadowRoot = this.attachShadow({ mode: 'open' }); | |
shadowRoot.appendChild(templateContent.cloneNode(true)); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment