简体   繁体   English

使用::part 样式化 Shadow DOM 元素

[英]Styling Shadow DOM elements using ::part

I am trying to apply the css styles to the elements within the shadow DOM using ::part .我正在尝试使用::part将 css styles 应用于shadow DOM中的元素。 Reference - https://www.w3.org/TR/css-shadow-parts-1/#selectordef-part , https://github.com/fergald/docs/blob/master/explainers/css-shadow-parts-1.md参考 - https://www.w3.org/TR/css-shadow-parts-1/#selectordef-part,https ://github.com/fergald/docs/blob/master/explainers/css-shadow-parts -1.md

In the below code - Words: 120 is within shadow DOM.在下面的代码中 - Words: 120在 shadow DOM 中。

 class WordCount extends HTMLParagraphElement { constructor() { // Always call super first in constructor super(); // count words in element's parent element var wcParent = this.parentNode; function countWords(node) { var text = node.innerText || node.textContent return text.trim().split(/\s+/g).length; } var count = 'Words: ' + countWords(wcParent); // Create a shadow root var shadow = this.attachShadow({ mode: 'open' }); // Create text node and add word count to it var text = document.createElement('span'); text.textContent = count; // Append it to the shadow root shadow.appendChild(text); // Update count when element content changes setInterval(function() { var count = 'Words: ' + countWords(wcParent); text.textContent = count; }, 200) } } // Define the new element customElements.define('word-count', WordCount, { extends: 'p' });
 <h1>Word count rating widget</h1> <article contenteditable=""> <h2>Sample heading</h2> <p>Pellentesque ornare tellus sit amet massa tincidunt congue. Morbi cursus, tellus vitae pulvinar dictum, dui turpis faucibus ipsum, nec hendrerit augue nisi et enim. Curabitur felis metus, euismod et augue et, luctus dignissim metus. Mauris placerat tellus id efficitur ornare. Cras enim urna, vestibulum vel molestie vitae, mollis vitae eros. Sed lacinia scelerisque diam, a varius urna iaculis ut. Nam lacinia, velit consequat venenatis pellentesque, leo tortor porttitor est, sit amet accumsan ex lectus eget ipsum. Quisque luctus, ex ac fringilla tincidunt, risus mauris sagittis mauris, at iaculis mauris purus eget neque. Donec viverra in ex sed ullamcorper. In ac nisi vel enim accumsan feugiat et sed augue. Donec nisl metus, sollicitudin eu tempus a, scelerisque sed diam. </p> <p part="some-box" is="word-count"> </p> </article>

Tried applying style to the shadow DOM using different ways with no success.尝试使用不同的方式将样式应用于阴影 DOM,但没有成功。 Example例子

::part(some-box) span{
  color: beige;
}

How can I apply styles to the shadow DOM span element using ::part ?如何使用::part将 styles 应用于阴影 DOM span 元素?

The part attribute must be defined:必须定义part属性:

  • inside the Shadow DOM,在 Shadow DOM 内部,
  • on the element which you want to apply the style to.在要应用样式的元素上。

In you case, it's the <span> element:在您的情况下,它是<span>元素:

<p is="word-count">
  #shadow-dom
    <span part="some-box">Words: 120</span>
</p>

The (global) ::part pseudo-element is defined like this, with or without the custom element selector before: (全局) ::part伪元素定义如下,之前有或没有自定义元素选择器:

[is=word-count]::part(some-box) {
  color: beige;
}

See the running example below.请参阅下面的运行示例。

 class WordCount extends HTMLParagraphElement { constructor() { // Always call super first in constructor super(); // count words in element's parent element var wcParent = this.parentNode; function countWords(node) { var text = node.innerText || node.textContent return text.trim().split(/\s+/g).length; } var count = 'Words: ' + countWords(wcParent); // Create a shadow root var shadow = this.attachShadow({ mode: 'open' }); // Create text node and add word count to it var text = document.createElement('span'); text.textContent = count; text.setAttribute( 'part', 'some-box' ) // Append it to the shadow root shadow.appendChild(text); // Update count when element content changes setInterval(function() { var count = 'Words: ' + countWords(wcParent); text.textContent = count; }, 200) } } // Define the new element customElements.define('word-count', WordCount, { extends: 'p' });
 p::part(some-box) { color: red; }
 <h1>Word count rating widget</h1> <article contenteditable=""> <h2>Sample heading</h2> <p>Add some words please. </p> <p is="word-count"> </p> </article>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM