Sco*_*ott 6 javascript html5 web-component shadow-dom native-web-component
我正在迈入Web组件的第一步,而未使用任何第三方库(例如Polymer)。主要卖点之一是Web组件样式与其他地方定义的样式分开,从而允许在类似于沙盒的环境中对组件的shadow-DOM进行样式设置。
我遇到的问题是样式如何通过插槽元素级联。由于带槽的元素不是影子DOM的一部分,因此只能用::slotted()组件模板中的选择器对其进行定位。这很棒,但是几乎不可能保证Web组件在所有上下文中都能正确显示,因为外部定义的样式还具有无可比拟的专一性*应用于空位元素。
*此外!important。
这个问题可以归结为:
customElements.define("my-nav",
class extends HTMLElement {
constructor() {
super();
const template = document.querySelector("template#my-nav").content;
this.attachShadow({ mode: "open" })
.appendChild(template.cloneNode(true));
}
}
);Run Code Online (Sandbox Code Playgroud)
a {
color: red; /* >:( */
}Run Code Online (Sandbox Code Playgroud)
<template id="my-nav">
<style>
.links-container ::slotted(a) {
color: lime;
font-weight: bold;
margin-right: 20px;
}
</style>
<div class="links-container">
<slot name="links"></slot>
</div>
</template>
<p>I want these links to be green:</p>
<my-nav>
<a href="#" slot="links">Link 1</a>
<a href="#" slot="links">Link 2</a>
<a href="#" slot="links">Link 3</a>
</my-nav>Run Code Online (Sandbox Code Playgroud)
我很难理解这个“功能”的价值。我要么必须以其他某种格式指定链接,然后使用JS创建它们的节点,要么添加!important到我的color属性- 当我未定义的其他任何属性时,它仍然不能保证一致性。
这个问题已经解决了吗,还是可以通过更改我的轻型DOM结构轻松解决?我不确定如何将链接列表添加到插槽中。
其<slot>有意设计为允许外部代码设置放入其中的内容的样式。如果使用得当,这是一个很棒的功能。
但是,如果您想更好地控制 Web 组件中显示的内容,那么您需要将内容的克隆副本复制到this.childNodes影子 DOM 中。那么你就可以100%控制CSS。
好的。您实际上只有 90% 的控制权,因为使用您的组件的人仍然可以设置该style属性。
customElements.define("my-nav",
class extends HTMLElement {
constructor() {
super();
const template = document.querySelector("template#my-nav").content;
this.attachShadow({ mode: "open" })
.appendChild(template.cloneNode(true));
}
connectedCallback() {
var container = this.shadowRoot.querySelector('.links-container');
var children = this.childNodes;
if (children.length > 0 && container) {
while(container.firstChild) {
container.removeChild(container.firstChild);
}
for (var i = 0; i < children.length; i++) {
container.appendChild(children[i].cloneNode(true));
}
}
}
}
);Run Code Online (Sandbox Code Playgroud)
a {
color: red;
}Run Code Online (Sandbox Code Playgroud)
<template id="my-nav">
<style>
.links-container a {
color: lime;
font-weight: bold;
margin-right: 20px;
}
</style>
<div class="links-container">
</div>
</template>
<p>I want these links to be green:</p>
<my-nav>
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#" style="color: red">Link 3</a>
</my-nav>Run Code Online (Sandbox Code Playgroud)
正如您在上面的示例中看到的,第三个链接仍然是红色的,因为我们设置了属性style。
如果您想防止这种情况发生,那么您需要style从内部内容中删除该属性。
customElements.define("my-nav",
class extends HTMLElement {
constructor() {
super();
const template = document.querySelector("template#my-nav").content;
this.attachShadow({ mode: "open" })
.appendChild(template.cloneNode(true));
}
connectedCallback() {
var container = this.shadowRoot.querySelector('.links-container');
var children = this.childNodes;
if (children.length > 0 && container) {
while(container.firstChild) {
container.removeChild(container.firstChild);
}
for (var i = 0; i < children.length; i++) {
container.appendChild(children[i].cloneNode(true));
}
container.querySelectorAll('[style]').forEach(el => el.removeAttribute('style'));
}
}
}
);Run Code Online (Sandbox Code Playgroud)
a {
color: red;
}Run Code Online (Sandbox Code Playgroud)
<template id="my-nav">
<style>
.links-container a {
color: lime;
font-weight: bold;
margin-right: 20px;
}
</style>
<div class="links-container">
</div>
</template>
<p>I want these links to be green:</p>
<my-nav>
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#" style="color: red">Link 3</a>
</my-nav>Run Code Online (Sandbox Code Playgroud)
我什至创建了一些组件,允许我读入独特的子节点并将其转换为自定义内部节点。
想想<video>标签及其<source>子标签。这些子项实际上并不渲染任何内容,它们只是保存用于指示要播放的视频的源位置的数据的一种方式。
这里的关键是了解它<slot>的用途,并且只以这种方式使用它,而不是试图强迫它做一些它从未打算做的事情。
奖励积分
由于ConnectedCallback每次将此节点放入 DOM 时都会被调用,因此每次都必须小心删除影子 DOM 中的任何内容,否则您将一遍又一遍地复制子节点。
customElements.define("my-nav",
class extends HTMLElement {
constructor() {
super();
const template = document.querySelector("template#my-nav").content;
this.attachShadow({ mode: "open" })
.appendChild(template.cloneNode(true));
}
connectedCallback() {
var container = this.shadowRoot.querySelector('.links-container');
var children = this.childNodes;
if (children.length > 0 && container) {
for (var i = 0; i < children.length; i++) {
container.appendChild(children[i].cloneNode(true));
}
}
}
}
);
function reInsert() {
var el = document.querySelector('my-nav');
var parent = el.parentNode;
el.remove();
parent.appendChild(el);
}
setTimeout(reInsert, 1000);
setTimeout(reInsert, 2000);Run Code Online (Sandbox Code Playgroud)
a {
color: red;
}Run Code Online (Sandbox Code Playgroud)
<template id="my-nav">
<style>
.links-container a {
color: lime;
font-weight: bold;
margin-right: 20px;
}
</style>
<div class="links-container">
</div>
</template>
<p>I want these links to be green:</p>
<my-nav>
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#" style="color: red">Link 3</a>
</my-nav>Run Code Online (Sandbox Code Playgroud)
因此删除重复的节点很重要:
customElements.define("my-nav",
class extends HTMLElement {
constructor() {
super();
const template = document.querySelector("template#my-nav").content;
this.attachShadow({ mode: "open" })
.appendChild(template.cloneNode(true));
}
connectedCallback() {
var container = this.shadowRoot.querySelector('.links-container');
var children = this.childNodes;
if (children.length > 0 && container) {
while(container.firstChild) {
container.removeChild(container.firstChild);
}
for (var i = 0; i < children.length; i++) {
container.appendChild(children[i].cloneNode(true));
}
}
}
}
);
function reInsert() {
var el = document.querySelector('my-nav');
var parent = el.parentNode;
el.remove();
parent.appendChild(el);
}
setTimeout(reInsert, 1000);
setTimeout(reInsert, 2000);Run Code Online (Sandbox Code Playgroud)
a {
color: red;
}Run Code Online (Sandbox Code Playgroud)
<template id="my-nav">
<style>
.links-container a {
color: lime;
font-weight: bold;
margin-right: 20px;
}
</style>
<div class="links-container">
</div>
</template>
<p>I want these links to be green:</p>
<my-nav>
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#" style="color: red">Link 3</a>
</my-nav>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
379 次 |
| 最近记录: |