shadycss icon indicating copy to clipboard operation
shadycss copied to clipboard

:host() ::slotted(*) selector does not get shimmed

Open ebidel opened this issue 10 years ago • 14 comments

I have a container around a <slot> and a :host([noshadow]) ::slotted(*) as a selector. Note the selector left of ::slotted(*). This does not get shimmed properly:

:host([noshadow]) ::slotted(*) {
  box-shadow: none;
}

However, this does work:

:host([noshadow]) .slot-container ::slotted(*) {
  box-shadow: none;
}

ebidel avatar Sep 14 '16 21:09 ebidel

I'm having a similar issue:

These selectors aren't shimmed properly:

::slotted([bottom-item]) {
 ...
}

::content > [top-item] {
 ... 
}

However, these are:

:host ::slotted([bottom-item]) {

}

:host ::content > [top-item] {
 ... 
}

/cc @azakus

blasten avatar Sep 20 '16 19:09 blasten

@blasten That seems to because of the documented limitation - need a selector left of ::slotted https://github.com/webcomponents/shadycss#selector-scoping

@ebidel Would that just be another case of that limitation, or would you expect that it would shim that selector as .my-element[noshadow] * (which would seem too over-encompassing)

keanulee avatar Sep 20 '16 19:09 keanulee

Yea, it may be actually

ebidel avatar Sep 20 '16 20:09 ebidel

So :host ::slotted(label){ should be working? Because it is not working for me.

I get the following in firefox:

material-toggle.material-toggle::slot > label{
}

Any idea how to fix it?

lukasoppermann avatar Oct 28 '16 08:10 lukasoppermann

@lukasoppermann x ::slotted(y) is converted to x > y in the shim, so unless <slot> is a direct child of the shadow root (i.e. child of <template>), you'll need the immediate parent in the selector to the left of slotted (e.g. <div><slot></slot></div> would require div ::slotted()).

keanulee avatar Oct 28 '16 21:10 keanulee

Okay, but what if it is a direction child? I got the slot straight in the shadowroot

lukasoppermann avatar Oct 28 '16 21:10 lukasoppermann

:host ::slotted(x) is converted to my-element > x

keanulee avatar Oct 28 '16 21:10 keanulee

@keanulee not for me 😢 for me :host ::slotted(x) creates my-element.my-element::slot > x. This means that it will not work.

Could I be doing something wrong? How can I get the .my-element::slot to go away?

lukasoppermann avatar Oct 29 '16 09:10 lukasoppermann

@lukasoppermann having ::slot in the selector is unexpected. Not sure how that could happen, but I suggest opening a separate issue on that specific problem ideally with a repro.

sorvell avatar Oct 31 '16 16:10 sorvell

Feedback on the core issue here is welcome, given the basic limitations of shimming the behavior of ::slotted.

Here's some background on how the shimming works. In the spec, ::slotted is a pseudo-element that can live off of a selector matching a <slot> (e.g. slot.one::slotted(*)). However, when polyfilling Shadow DOM, the <slot> element does not end up in the composed tree and is therefore not targetable via css. So, to shim this, we require a "scoping selector" to the left of the ::slotted. This selector must match an element in the composed tree and it must match the parent of the <slot>. For example:

<style>
  .slot-one-parent ::slotted(*) {...}
</style>
<div class=".slot-one-parent">
  <slot class="one"></slot>
</div>

As @keanulee mentioned, the style here would get transformed to .slot-one-parent > * and this works ok. However, if the "scoping-selector" matches an ancestor not the parent of the elements distributed to the slot, the shimming fails. This means the user must setup the DOM precisely like this.

This may trip up users that have experience with Shadow DOM v0 in Polymer 1.x. Under the v0 Shadow DOM, the ::content selector worked similar to ::slotted and the shimming requirement was the same. However, since ::content could style any descendant in the distributed subtree, we shimmed it with a descendant selector rather than a child selector>(e.g. in the above example:.slot-one-parent *). As it worked out, this allowed users to make a "scoping selector" that was an ancestor but not a parent of the<content>. This was not good because the styles could leak to any descendants of that ancestor actually in the shadow tree.

For these reasons we're inclined to keep the current behavior, but if it's too onerous to setup the DOM such that there is always a <slot> parent that can be matched via a "scoping selector", we may consider changing the behavior, adding an option, or considering other alternatives.

sorvell avatar Oct 31 '16 16:10 sorvell

Just FYI problem was that npm did not correctly update shadycss so I was using an outdated version which seems to have this bug. With the current release it works fine.

lukasoppermann avatar Oct 31 '16 19:10 lukasoppermann

Hi, I have been trying to get this shim to work with firefox using the example code you provided (modified by yours truly) below. It does not work. I am not sure why. I think it is related to this issue, though.

I made it so that the connectedCallback() is used, but it still does not render the box around the content per the style. Thanks for any advice.

<html>
  <head>
    <title>Issue 5</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="bower_components/custom-elements/custom-elements.min.js"></script>
    <script src="bower_components/shadycss/shadycss.min.js"></script>
    <script src="bower_components/shadydom/shadydom.min.js"></script>
  </head>
  
  <body>
  <template id="myElementTemplate">
      <style>
        :host {
          display: block;
          padding: 8px;
        }

        #content {
          background-color: var(--content-color);
        }

        .slot-container ::slotted(*) {
          border: 5px solid greenyellow;
          margin: 4px;
        }
      </style>
      <div id="content">Content</div>
      <div class="slot-container">
        <slot></slot>
      </div>
</template>
<script>
  const myElementTemplate = document.querySelector('#myElementTemplate');
  ShadyCSS.prepareTemplate(myElementTemplate, 'my-element');
  class MyElement extends HTMLElement {
    connectedCallback() {
      this.attachShadow({mode: 'open'});
      this.shadowRoot.appendChild(
        document.importNode(myElementTemplate.content, true));
      ShadyCSS.applyStyle(this);
    }
  }

  customElements.define('my-element', MyElement);
  
  const newElem = document.createElement('my-element');
  const newP = document.createElement('p');
  newP.textContent = "Foo";
  newElem.appendChild(newP);
  document.body.appendChild(newElem);
</script>
  </body>
</html>

prushforth avatar Dec 01 '16 18:12 prushforth

Deduplicating with #134

dfreedm avatar Sep 22 '17 23:09 dfreedm

See my workaround here https://github.com/webcomponents/webcomponentsjs/issues/751

fuerte656 avatar Jul 03 '18 10:07 fuerte656