Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web Components shadowRoot style doesn't have CSSStyleSheet object #3645

Open
jcubic opened this issue Dec 25, 2023 · 0 comments
Open

Web Components shadowRoot style doesn't have CSSStyleSheet object #3645

jcubic opened this issue Dec 25, 2023 · 0 comments

Comments

@jcubic
Copy link

jcubic commented Dec 25, 2023

Basic info:

  • Node.js version: v20.10.0
  • jsdom version: 23.0.1

Minimal reproduction case

const { JSDOM } = require("jsdom");

const options = { };
const dom = new JSDOM(`<!DOCTYPE html><html><body>`, options);
const window = dom.window;
const document = window.document;

function tag(name, class_name, text) {
  const p = document.createElement(name);
  p.innerText = text;
  p.classList.add(class_name);
  return p;
}

class HelloWorld extends window.HTMLElement {
  constructor() {
    super();
    this.name = 'World';
    this.color = 'red';
  }

  connectedCallback() {
    const shadow = this.attachShadow({ mode: 'open' });
    const style = document.createElement('style');
    shadow.appendChild(style);
    const p = tag('p', 'test', `Hello ${this.name}`);
    shadow.appendChild(p);
    const { sheet } = style;
    console.log({sheet: style.sheet});
    const index = sheet.cssRules.length;
    sheet.insertRule(`.test { color: red }`, index);
  }

  attributeChangedCallback(property, oldValue, newValue) {
    if (oldValue === newValue) return;
    this[ property ] = newValue;
  }

  static get observedAttributes() {
    return ['name', 'color'];
  }
}

window.customElements.define('hello-world', HelloWorld);
const hello = document.createElement('hello-world');
document.body.appendChild(hello);

How does similar code behave in browsers?

https://codepen.io/jcubic/pen/xxBbdPa?editors=0011

The same code from connectedCallback works fine outside Web Components.

@jcubic jcubic changed the title Web Components shadowRoot style doesn't have style sheet object Web Components shadowRoot style doesn't have CSSStyleSheet object Dec 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant