External Styling for CustomElements V1 & ShadowDOM

怎甘沉沦 提交于 2019-12-25 09:08:23

问题


Whilst this may seem to be a duplicate question those asked previously have been based around Polymer, not native CustomElements, and this is about the css itself, not penetrating the ShadowDOM or Custom CSS Properties / Variables

So here we have a simple Custom Element (note: as of writing this only works in newer Chrome versions)

class StyleMe extends HTMLElement {
	constructor () {
		super();
		let shadow = this.attachShadow({ mode: 'closed' });
		shadow.appendChild(document.querySelector('#style-me').content.cloneNode(true));
	}
}
customElements.define('style-me', StyleMe);
h1 {
	/* even !important doesn't penetrate */
	color: red !important;
}
<h1>I'm a normal heading</h1>
<style-me>I'm heading hidden in style-me's shadow</style-me>
<template id="style-me">
	<style>
		:host {
			background: blue;
			display: block;
		}
		h1 {
			color: yellow;
		}
	</style>
	<h1><slot></slot></h1>
</template>

This demonstrates nicely how styles are isolated when using the ShadowDOM.

What would be nice to do is have the contents of the <style> inside <template> stored in an external file, possibly generated by a pre-processor such as less.

After much searching only to find answers related to Polymer I've draw a blank, any thoughts?


I am not looking for custom properties they would allow me to use

<style>
    :host {
        background: blue;
        display: block;
    }
    h1 {
        color: var(--something);
    }
</style>

And set the color using

style-me {
    --something: yellow;
}

My question is about moving

:host {
    background: blue;
    display: block;
}
h1 {
    color: yellow;
}

Out of a <style> tag and into a separate file


回答1:


You can use the CSS @import url directive.

<template id="style-me">
    <style>
        @import url( '/css/style.css' )
    </script>
    <h1><slot></slot></h1>
</template>

The question was discussed here.




回答2:


I realize this question is more than a year old now, but you might start looking at the constructible style sheet specification/proposal.

That would allow you to do something like:

const customStyleSheet = new CSSStyleSheet();
await customStyleSheet.replace("@import url('/css/style.css')");
shadowDOMReference.adoptedStyleSheets = [ customStyleSheet ];


来源:https://stackoverflow.com/questions/45920166/external-styling-for-customelements-v1-shadowdom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!