random
random

Reputation: 7899

Styling Shadow DOM elements using ::part

I am trying to apply the css styles to the elements within the shadow DOM using ::part. 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

In the below code - Words: 120 is within 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. Example

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

How can I apply styles to the shadow DOM span element using ::part?

Upvotes: 2

Views: 316

Answers (1)

Supersharp
Supersharp

Reputation: 31229

The part attribute must be defined:

  • inside the Shadow DOM,
  • on the element which you want to apply the style to.

In you case, it's the <span> element:

<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:

[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>

Upvotes: 1

Related Questions