How to style components nestled with Shadow DOM?

Asked

Viewed 41 times

2

I have 2 nested web components that use Shadow DOM. The first, my-component, only render the second component:

<my-another-component part="my-another-component" />

And the second, my-another-component, renders a native element:

<div part="my-div"></div>

How can I style the div from a component that renders the my-component?

The final result of this last component would be something like:

<style>/* ... */</style>

<my-component>
    #shadow-root
        <my-another-component part="my-another-component">
            #shadow-root
                <div part="my-div"></div>
        </my-another-component>
</my-component>

I tried to use the pseudo-element ::part nestled, but it didn’t work:

my-component::part(my-another-component)::part(my-div) {
    color: blue;
}

customElements.define('my-component',
  class extends HTMLElement {
    constructor() {
      super();
      let template = document.getElementById('my-component');

      this.attachShadow({mode: 'open'})
        .appendChild(template.content.cloneNode(true));
    }
  }
);

customElements.define('my-another-component',
  class extends HTMLElement {
    constructor() {
      super();
      let template = document.getElementById('my-another-component');

      this.attachShadow({mode: 'open'})
        .appendChild(template.content.cloneNode(true));
    }
  }
);
<style>
  my-component::part(my-another-component) {
    font-weight: bold;
  }
  my-component::part(my-another-component)::part(my-div) {
    color: blue;
  }
</style>
<my-component></my-component>

<template id="my-component">
  <my-another-component part="my-another-component"></my-another-component>
</template>

<template id="my-another-component">
  <div part="my-div">Example</div>
</template>

  • Who is alive always appears :D. Guy in this case you have to create the <style> tag in the scope of the web script

  • @hugocsl opa, I haven’t been using much Sopt, I sometimes enter chat and meta. Tu diz dentro do Shadow DOM do my-another-component? It would have a way without injecting code into the other component?

  • Take a look here and see how the style tag goes inside the component web template with an innerHTML https://css-tricks.com/styling-a-web-component/#move-the-Styles-inline-Inside-the-web-Component this stuff here tb you might be interested in https://meowni.ca/posts/part-Theme-explainer/ talks about :part and ::Theme

  • @hugocsl the exportparts solves the question, despite the limitations. But there is an error in the link, multiple exports must be separated by "," and not by " ". You can add an answer with this solution

  • I’ll let someone else answer, because I would be talking about something that I do not dominate rss. Actually I only commented here because these days I needed to take a look at web components and saw that the guys injected the css directly making an innerHtml with the style tag. But I’m glad you helped out there

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.