admin管理员组文章数量:1345307
I'm learning web ponents with a shadow root and can't seem to find on google if loading external stylesheets is possible with out-of-the-box code? I am NOT using polymer or any other web ponent library (yet). Code below:
<script src="../../libs/jquery-2.1.1.min.js"></script>
<script>
var hollaProto = Object.create(HTMLElement.prototype);
hollaProto.createdCallback = function () {
var shadow = this.createShadowRoot();
var content = document.querySelector('link[rel=import]').import.querySelector("div");
$("button[data-mand=holla]", content).on("click", function () { alert("Holla!"); });
shadow.appendChild(content);
};
var hollaWidget = document.registerElement("holla-back", {
prototype: hollaProto
});
</script>
<div class="holla-back">
<button data-mand="holla">Holla!</button>
</div>
If I put my link tag up top, above the first script tag, I style the whole web age, but not the web ponent.
If I put it under div.holla-back
it doesn't style anything.
How do you use external stylesheets with web ponents?
I'm learning web ponents with a shadow root and can't seem to find on google if loading external stylesheets is possible with out-of-the-box code? I am NOT using polymer or any other web ponent library (yet). Code below:
<script src="../../libs/jquery-2.1.1.min.js"></script>
<script>
var hollaProto = Object.create(HTMLElement.prototype);
hollaProto.createdCallback = function () {
var shadow = this.createShadowRoot();
var content = document.querySelector('link[rel=import]').import.querySelector("div");
$("button[data-mand=holla]", content).on("click", function () { alert("Holla!"); });
shadow.appendChild(content);
};
var hollaWidget = document.registerElement("holla-back", {
prototype: hollaProto
});
</script>
<div class="holla-back">
<button data-mand="holla">Holla!</button>
</div>
If I put my link tag up top, above the first script tag, I style the whole web age, but not the web ponent.
If I put it under div.holla-back
it doesn't style anything.
How do you use external stylesheets with web ponents?
Share Improve this question edited Jan 2, 2015 at 19:19 Thinking Sites asked Jan 2, 2015 at 18:12 Thinking SitesThinking Sites 3,54219 silver badges31 bronze badges2 Answers
Reset to default 9Link tags are inert in Shadow DOM according to the spec. However, you can use @import
, though that has its own performance issues.
The way Polymer works around this is it looks at the link
tags and uses xhr
to load those styles and apply them.
edit:
The folks working on Shadow DOM are aware of this shorting and that it needs to be fixed. Hopefully in the future we can e up with a system that supports external stylesheets.
Shadow DOM doesn't react to link tags. Infact, Chrome 41 throws an error when you use link tags. We have worked around that limitation by inlining CSS classes at the build time using vulcanize. This turned out to be quite handy in separating your CSS and the ponent definition.
本文标签: javascriptExternal stylesheets for shadow dom in web componentsStack Overflow
版权声明:本文标题:javascript - External stylesheets for shadow dom in web components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743812063a2543253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论