Skip to content

Instantly share code, notes, and snippets.

@praveeno
Created July 30, 2024 08:29
Show Gist options
  • Save praveeno/af6a6efe94fb4cf9446bf47ad33ca5ad to your computer and use it in GitHub Desktop.
Save praveeno/af6a6efe94fb4cf9446bf47ad33ca5ad to your computer and use it in GitHub Desktop.
custom component in plain html
<!-- 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