webcomponentsjs icon indicating copy to clipboard operation
webcomponentsjs copied to clipboard

Slotted polyfill does not work if slot is not a direct child

Open TimvdLippe opened this issue 9 years ago • 6 comments

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

TimvdLippe avatar Apr 02 '17 13:04 TimvdLippe

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

SchnWalter avatar Apr 06 '17 10:04 SchnWalter

Is there any news on this, running into the same issue. 😢

lukasoppermann avatar Nov 05 '17 20:11 lukasoppermann

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.

lukasoppermann avatar Nov 13 '17 06:11 lukasoppermann

The shortest workaround is to modify style generated by ShadyCSS. 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>

fuerte656 avatar Jul 03 '18 10:07 fuerte656

This is an issue in ShadyDOM: http://jsbin.com/doxawoliyi/edit?html,console,output

TimvdLippe avatar Jul 03 '18 10:07 TimvdLippe

This issue was moved to webcomponents/shadydom#258

TimvdLippe avatar Jul 03 '18 10:07 TimvdLippe