Created
July 30, 2024 08:29
-
-
Save praveeno/af6a6efe94fb4cf9446bf47ad33ca5ad to your computer and use it in GitHub Desktop.
custom component in plain html
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
<!-- Modular component example --> | |
<template id="my-component"> | |
<style> | |
/* Component-specific styles */ | |
.container { | |
padding: 10px; | |
background-color: #f0f0f0; | |
} | |
</style> | |
<div class="container"> | |
<h1>My Component</h1> | |
<p>This is a modular component.</p> | |
</div> | |
</template> | |
<script> | |
class MyComponent extends HTMLElement { | |
constructor() { | |
super(); | |
const template = document.getElementById('my-component').content; | |
this.attachShadow({ mode: 'open' }).appendChild(template.cloneNode(true)); | |
} | |
} | |
customElements.define('my-component', MyComponent); | |
</script> | |
<my-component></my-component> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment