使用:: part设置Shadow DOM元素的样式

时间:2019-11-14 17:24:50

标签: javascript css shadow-dom

我正在尝试使用shadow DOM将css样式应用于::part中的元素。参考-https://www.w3.org/TR/css-shadow-parts-1/#selectordef-parthttps://github.com/fergald/docs/blob/master/explainers/css-shadow-parts-1.md

在下面的代码中-Words: 120在影子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>

尝试以不同方式将样式应用于影子DOM失败。例子

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

如何使用::part将样式应用于阴影DOM span元素?

1 个答案:

答案 0 :(得分:1)

必须定义part属性:

  • 在Shadow DOM内,
  • 要在其中应用样式的元素。

在这种情况下,它是<span>元素:

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

(全局)::part伪元素的定义如下,在此之前或不使用自定义元素选择器:

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

请参见下面的运行示例。

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>