<my-element>MyElement</my-element>
my-element:state(clicked) {
  color: red;
}
class MyElement extends HTMLElement {
  #internals;
  
  constructor() {
    super();
    this.#internals = this.attachInternals();
    this.addEventListener("click", this.#onClick.bind(this));
  }
  
  #onClick() {
    this.#internals.states.add("clicked");
  }
}

customElements.define("my-element", MyElement);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.