Here is a JSFiddle demonstration of the custom element: https://jsfiddle.net/c4bLo097/5/
Here is the code from the fiddle:
JavaScript:
window.customElements.define('test-element', class TestElement extends HTMLElement {
constructor() {
super()
let contents = `
<style>
:host {
display: block;
}
:host([hidden]) {
display: none;
}
</style>`
// convert string to nodes
let template = document.createElement('template')
template.innerHTML = contents
// create shadow
this.attachShadow({mode: 'open'})
// insert nodes
this.shadowRoot.appendChild(template.content.cloneNode(true))
}
})
HTML:
<test-element>
This element should have a natural height.
<div style="height: 300px;"></div>
I should be able to see this text on a green background.
</test-element>
CSS:
test-element {
width: 200px;
background: green;
}
If you inspect <custom-element>
with your developer tools, you should see that the shadow is there. But my element will not render its height correctly.
Here is an example JSFiddle of what I'm trying to achieve: https://jsfiddle.net/9483s1qb/2/