如何防止 Slotted HTML 元素在进入 shadowRoot 之前进行渲染?
How do I prevent a Slotted HTML element from rendering until is it inside the shadowRoot?
我有一个与此类似的非常简单的网站 jsfiddle。一切似乎都工作正常,但在我本地的,但是当我停在第一行 JS 时(在声明自定义元素之前)我可以看到没有格式的 div...
<jrg-test>
<div slot="test">
This should work
</div>
</jrg-test>
connectedCallback() {
console.log("Ok we are working")
this.shadowRoot.innerHTML = "<slot name='test'></slot>"
const element = document.createElement('style');
element.textContent = "::slotted(*){background-color:red;color:white;}";
this.shadowRoot.appendChild(element);
}
所以基本上,如果我在呈现自定义元素之前停止,我会看到原始 div。我知道有一些涉及定位和 CSS 的骇人听闻的解决方案,但是否有更简洁的解决方案。可能是我可以专门用 JS 实现的?
所以主要问题是如何在应用红色背景和白色之前隐藏 This should work
文本?
也许您可以尝试使用 :defined
CSS 伪 class 来隐藏定义的自定义元素。
参见下面的示例:
class TestElement extends HTMLElement{
constructor(){
super();
console.log("Attaching the shadow")
this.attachShadow({mode:'open'})
}
connectedCallback() {
console.log("Ok we are working")
this.shadowRoot.innerHTML = `<style>
::slotted(*){background-color:red;color:white;}
</style>
<slot name='test'></slot>`
}
}
upgrade.onclick = () => customElements.define("jrg-test", TestElement)
jrg-test:not(:defined) {
display:none
}
<jrg-test>
<div slot="test">This should work</div>
</jrg-test>
<button id="upgrade">upgrade</button>
您可以通过添加到全局样式来隐藏所有未定义的自定义元素
<style>
*:not(:defined) { display:none }
</style>
例如
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style> *:not(:defined) { display:none } </style>
<script type="module" src="./index.js"></script>
</head>
<body>
<test-tag>
This content will be visible only after "test-tag" is defined
</test-tag>
</body>
</html>
我有一个与此类似的非常简单的网站 jsfiddle。一切似乎都工作正常,但在我本地的,但是当我停在第一行 JS 时(在声明自定义元素之前)我可以看到没有格式的 div...
<jrg-test>
<div slot="test">
This should work
</div>
</jrg-test>
connectedCallback() {
console.log("Ok we are working")
this.shadowRoot.innerHTML = "<slot name='test'></slot>"
const element = document.createElement('style');
element.textContent = "::slotted(*){background-color:red;color:white;}";
this.shadowRoot.appendChild(element);
}
所以基本上,如果我在呈现自定义元素之前停止,我会看到原始 div。我知道有一些涉及定位和 CSS 的骇人听闻的解决方案,但是否有更简洁的解决方案。可能是我可以专门用 JS 实现的?
所以主要问题是如何在应用红色背景和白色之前隐藏 This should work
文本?
也许您可以尝试使用 :defined
CSS 伪 class 来隐藏定义的自定义元素。
参见下面的示例:
class TestElement extends HTMLElement{
constructor(){
super();
console.log("Attaching the shadow")
this.attachShadow({mode:'open'})
}
connectedCallback() {
console.log("Ok we are working")
this.shadowRoot.innerHTML = `<style>
::slotted(*){background-color:red;color:white;}
</style>
<slot name='test'></slot>`
}
}
upgrade.onclick = () => customElements.define("jrg-test", TestElement)
jrg-test:not(:defined) {
display:none
}
<jrg-test>
<div slot="test">This should work</div>
</jrg-test>
<button id="upgrade">upgrade</button>
您可以通过添加到全局样式来隐藏所有未定义的自定义元素
<style>
*:not(:defined) { display:none }
</style>
例如
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style> *:not(:defined) { display:none } </style>
<script type="module" src="./index.js"></script>
</head>
<body>
<test-tag>
This content will be visible only after "test-tag" is defined
</test-tag>
</body>
</html>