<my-ce></my-ce>
<script>
class MyCe extends HTMLElement {
constructor() {
super()
const shadow = this.attachShadow({ mode: 'open' })
shadow.innerHTML = '<button>Click me</button>'
shadow.querySelector('button').addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('my-event', { detail: 'Hello, World!' }))
})
}
}
customElements.define('my-ce', MyCe)
</script>
Create an element with Shadow DOM.
Create a custom element with Shadow DOM as described in Shadow DOM recipe.