JSFiddle - React, Tailwind, and code Playground
HTML
<template id="text">
<style>
.text {
color:red;
}
</style>
<div class="text">Hello</div>
</template>
<template id="page">
<style>
:host {
border:5px solid green;
display: block;
margin:20px;
height:100px;
width:100px;
}
</style>
This is a page
</template>
<div id="my-body"></div>
JavaScript
class Text extends HTMLElement {
constructor(){
super();
this.attachShadow({mode: 'open'});
const template = document.getElementById('text');
const node = document.importNode(template.content, true);
this.shadowRoot.appendChild(node);
}
}
customElements.define('my-text', Text);
class Page extends HTMLElement {
constructor(){
super();
this.attachShadow({mode: 'open'});
const template = document.getElementById('page');
const node = document.importNode(template.content, true);
this.shadowRoot.appendChild(node);
}
}
customElements.define('my-page', Page);
const body = document.getElementById('my-body')
const page = document.createElement('my-page')
const text = document.createElement('my-text')
// Not working, element is hidden
page.appendChild(text)
body.appendChild(page)
// Working
//body.appendChild(text)