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

176
Visualizações
Create <select> type dropdown using web components

The html element needs child elements. The select itself isn't really useful unless there are the option elements. I want to be doing to same thing.

If I create a web component and it looks like this

<my-select>
  <my-option> one </my-option>
  <my-option> two </my-option>
  <my-option> three </my-option>
</my-select>

My aim in general here is that the option tags shouldn't be put in a slot. They are needed to define data for rendering the select. So at the very least they would need to be moved into the shadow DOM. But that seems like the wrong thing to do.

Another example of this sort of thing would be and

  • . If the was a custom element you wouldn't want to put the
  • into slots. is another example.

    Ok here is a concrete example. Given the markup above, the my-select web component will actually create a shadow DOM and render a instead. So the my-select needs to look at all the elements inside it and create an

  • for each one.

    So how the heck do I do that? I must be missing something right?

  • about 4 years ago · Juan Pablo Isaza
    1 Respostas
    Responde à pergunta

    0

    <my-select>
      <my-option> one </my-option>
      <my-option> two </my-option>
      <my-option> three </my-option>
    </my-select>
    

    If you have attached a shadowDOM and you want to move this lightDOM to shadowDOM you can use:

    connectedCallback(){
      setTimeout(()=>{
        this.shadowRoot.append(...this.children);
      }
    }
    

    The setTimeout is required because the connectedCallback fires on the opening tag, the lightDOM at that time is not parsed yet.
    You have to wait for all lightDOM to be parsed, setTimeout makes sure you execute the code at the first opportunity (when the Event Loop is empty).

    For Event Loop details see: https://dev.to/lydiahallie/javascript-visualized-event-loop-3dif

    about 4 years ago · Juan Pablo Isaza Relatório
    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