<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>

Add button element

Add a button element to the shadow DOM that will emit a custom event when clicked.