Skip to content

[cssom] allow text in CSSStyleSheet constructor #12110

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

Open
jonathantneal opened this issue Apr 22, 2025 · 2 comments
Open

[cssom] allow text in CSSStyleSheet constructor #12110

jonathantneal opened this issue Apr 22, 2025 · 2 comments
Labels
cssom-1 Current Work

Comments

@jonathantneal
Copy link
Contributor

May we consider allowing CSSStyleSheetInit to accept a text property that synchronously assigns rules to the constructed sheet?

dictionary CSSStyleSheetInit {
  DOMString baseURL = null;
  (MediaList or DOMString) media = "";
  boolean disabled = false;
  DOMString text = "";
};

Example usage:

new CSSStyleSheet(":host{ /* styles */ }")

Example in adoptedStyleSheets:

shadowRoot.adoptedStyleSheets = [ new CSSStyleSheet(":host { /* styles */ }") ]

The goal is to improve the authoring experience for constructed stylesheets, especially within custom elements, by enabling inline creation without needing to call replaceSync() separately. This would be a small but meaningful ergonomic improvement and a low-barrier enhancement that could benefit many developers working with adoptedStyleSheets.

Would this be appropriate for inclusion in the spec, or is it better suited for discussion elsewhere?

@Loirooriol Loirooriol added the cssom-1 Current Work label Apr 29, 2025
@Loirooriol
Copy link
Contributor

Presumably the example would be

new CSSStyleSheet({ text: ":host{ /* styles */ }" })

Or do you want the parameter to be either a CSSStyleSheetInit or a string?

@castastrophe
Copy link

castastrophe commented Apr 30, 2025

I quite like this idea for dynamic interpolation for systems with design data stored in JSON for example.

new CSSStyleSheet({ text: `:host{ background: ${token.colors.blue.100} }` })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cssom-1 Current Work
Projects
None yet
Development

No branches or pull requests

3 participants