You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support for building an optimal, nested CSSStylesheet programmatically #13367
I've been told this should be proposed here (I guess).
What problem are you trying to solve?
If a web component doesn't use ShadowDOM, but nevertheless wishes to provide a CSS Module to style the light children DOM elements that the web component generates, it would be natural, with the now shipping nested CSS support, to simply wrap the styles inside a selector that specifies the name of the custom element. The problem is, especially with scoped custom element registries, that name may not be uniform across all usages. Also, many web component libraries explicitly suggest using separate file to specify the name of the custom element.
It would also be quite useful to be able to use the same CSS Module, one for a custom element that doesn't use shadow DOM, one that does, that implements nearly identical content.
So we need a way to be able "wrap" the styles with a dynamic outer selector (typically the dynamic name of the custom element).
What solutions exist today?
I asked Claude AI how it would do it. It suggests:
functionnestStyleSheet(originalSheet,outerSelector){constnewSheet=newCSSStyleSheet();// Build nested CSS textletnestedRules=[];for(construleoforiginalSheet.cssRules){nestedRules.push(rule.cssText);}// Wrap all rules in the outer selector using nested CSS syntaxconstnestedCSS=`${outerSelector} {${nestedRules.join('\n ')}}`;newSheet.replaceSync(nestedCSS);returnnewSheet;}
This is probably all just fine, but I wonder if that means excessive string parsing? The browser now needs to reparse the entire contents again, rather than just cloning parsed "nodes".
Alternatively, given it's possible now to scope a style sheet to a root, could we instead have per element.adoptedStyleSheets and simply re-use :scope?
constsheet=newCSSStyleSheet();sheet.replaceSync(` /* Works as both a direct style sheet and shadow root sheet */ :scope, :host { /* custom element styles */ }`);element.adoptedStyleSheets=[sheet];
Activity
Jamesernator commentedon Jan 19, 2026
Alternatively, given it's possible now to scope a style sheet to a root, could we instead have per element
.adoptedStyleSheetsand simply re-use:scope?This would be useful even beyond custom elements.
sorvell commentedon Feb 21, 2026
What about using @mixin?