Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
How to pass Javascript custom event past the shadowDOM

I'm writing a Javascript custom element and want a event to bubbel past it's shadowDOM boundary. Catching the event inside the shadowDOM is no problem but how can I listen for the event in the lightDOM?

html

    <div class="tasks">
        <m-task name="Abc" number=123></m-task>
    </div>

javascript

customElements.define('m-task',
    class mTask extends HTMLElement {
        constructor() {
            super()
            this.attachShadow({ mode: 'open' })
        

        // ---> LISTENING FOR EVENT <---
        this.addEventListener('connected', e => {
                console.log(e.detail)
            })
        // ---> LISTENING FOR EVENT <---
        this.addEventListener('disconnected', e => {
                console.log(e.detail)
            })

        }
        connectedCallback() {
            console.log('connected', this)
            this.dispatchEvent(new CustomEvent('connected', {
                bubbles: true,
                composed: true,
                detail: {
                    type: 'connected',
                    value: 1234
                }
            }))
        }
        disconnectedCallback() {
            console.log('disconnected', this)
            this.dispatchEvent(new CustomEvent('disconnected', {
                bubbles: true,
                composed: true,
                detail: {
                    type: 'disconnected',
                    value: -99
                }
            }))
        }
    })

// ---> LISTENING FOR EVENT <---
//      THIS IS NOT WORKING
document.querySelector('.tasks').addEventListener('connected', e => {
    console.log('listening in the light')
    console.log(e.detail)
})

console

connected <m-task name="Abc" number="123">
Object { type: "connected", value: 1234 }
disconnected <m-task name="Abc" number="123">
Object { type: "disconnected", value: -99 }

edit

waiting 1ms gives the expected result

setTimeout(() => {
    this.dispatchEvent(new CustomEvent('connected', {
        bubbles: true,
        composed: true,
        detail: {
            type: 'connected',
            value: 1234
        }
     }))
}, 1);
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda