Slotted polyfill does not work if slot is not a direct child
From @TimvdLippe on April 2, 2017 13:17
Description
If you use ::slotted(foo) where foo is not a direct child of the element, the polyfill will not style foo properly.
The polyfill rewrites the ::slotted(foo) to my-elem > foo, which is incorrect if there is an element in-between. Given the JSBin, I think the correct rewrite is .style-scope.my-elem section > foo. For example the polyfill could add a class slot-X to the parent of <slot>, such that it rewrites to .style-scope.my-elem .slot-default > foo.
JSBin
http://jsbin.com/pumogujefe/edit?html,output
Expected results
In Firefox, the background should be red just as it is in Chrome
Actual results
In Firefox the background is white, while in Chrome it is red
Copied from original issue: webcomponents/webcomponentsjs#751
From @SchnWalter on April 6, 2017 10:16
This is cause by the fact that the parent Shadow DOM elements show up as normal DOM elements in the polyfilled markup
Related to:
Polymer v2: this.parentElement points to the wrong DOM element when using polyfills Polymer/polymer#4506
From @lukasoppermann on November 5, 2017 20:42
Is there any news on this, running into the same issue. 😢
From @lukasoppermann on November 13, 2017 6:38
Hey @azakus is there any chance this can be solved? Does https://github.com/webcomponents/shadydom/issues/119 fix this? Thanks for looking into this.
From @fuerte656 on July 3, 2018 10:32
The shortest workaround is to modify style generated by shadowCss. It is working for IE11,FireFox etc. It is worked like a charm
connectedCallback(){
super.connectedCallback();
let tagName = this.constructor.is;
let style = Array.from(document.styleSheets).find((item) => item.ownerNode && item.ownerNode.getAttribute("scope") === tagName);
if(style){
Array.from(style.cssRules || style.rules || []).forEach((rule)=>{
if(rule.selectorText.indexOf("slot") > -1){
rule.selectorText = rule.selectorText.replace(/>/ig," ");
}
})
}
}
Example of my style
:host ::slotted([slot=au-drawer-toggle]) {
display: none;
}
Example of mockup
<div class$="drawer-container [[location]]" id="container">
<div class="drawer-main">
<slot name="main"></slot>
</div>
<div class="drawer-drawer" id='drawerDrawer'>
<div class="content-wrapper" id="contentWrapper">
<slot name="drawer"></slot>
</div>
<slot name="au-drawer-toggle"></slot>
</div>
</div>
This is an issue in ShadyDOM: http://jsbin.com/doxawoliyi/edit?html,console,output
Probably it is ShadyCss issue?)
https://github.com/webcomponents/shadycss/issues
I have faced this problem two days ago and found a workaround by this evening. And I believe that all people can use this)
Also, you can read the code, that doing such CSS selector)
https://github.com/webcomponents/shadycss/blob/c095c6b70613a3049f4f2f5bef22b941ea95a3c3/src/style-transformer.js#L268
selector = selector.replace(SLOTTED_PAREN, (m, paren) => > ${paren});
We seem to have this issue still
:host .btn ::slotted(button) {
position: absolute;
visibility: hidden;
}
When testing the computed styles of this button element, the visibility is visible and position is static. Other properties do not work either, as expected.
I saw the line of code @fuerte656 provided in my node_modules/@webcomponents/shadycss/src/style-transformer so that seems up to date.
We're using webcomponentsjs v2.2.7 which depends on shadycss v1.9.0
Are there any updates on this issue?
Possibly related shadycss issues: https://github.com/webcomponents/shadycss/issues/154 https://github.com/webcomponents/shadycss/issues/155
Seems to be working with latest webcomponents.