Skip to content

Commit 200342c

Browse files
committed
perf: set adoptedStyleSheets only once per component
1 parent c29bfc3 commit 200342c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

packages/vaadin-themable-mixin/src/lumo-injector.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class LumoInjector {
8484
this.#root = root;
8585
this.#cssPropertyObserver = new CSSPropertyObserver(this.#root, 'vaadin-lumo-injector', (propertyName) => {
8686
const tagName = propertyName.slice(2).replace('-lumo-inject', '');
87-
this.#updateComponentStyleSheet(tagName);
87+
this.#updateStyleSheet(tagName);
8888
});
8989
}
9090

@@ -103,8 +103,15 @@ export class LumoInjector {
103103
this.#componentsByTag.set(tagName, this.#componentsByTag.get(tagName) ?? new Set());
104104
this.#componentsByTag.get(tagName).add(component);
105105

106-
this.#updateComponentStyleSheet(tagName);
106+
const stylesheet = this.#styleSheetsByTag.get(tagName);
107+
if (stylesheet) {
108+
if (stylesheet.cssRules.length > 0) {
109+
injectLumoStyleSheet(component, stylesheet);
110+
}
111+
return;
112+
}
107113

114+
this.#initStyleSheet(tagName);
108115
this.#cssPropertyObserver.observe(lumoInjectPropName);
109116
}
110117

@@ -121,17 +128,21 @@ export class LumoInjector {
121128
removeLumoStyleSheet(component);
122129
}
123130

124-
#updateComponentStyleSheet(tagName) {
131+
#initStyleSheet(tagName) {
132+
this.#styleSheetsByTag.set(tagName, new CSSStyleSheet());
133+
this.#updateStyleSheet(tagName);
134+
}
135+
136+
#updateStyleSheet(tagName) {
125137
const { tags, modules } = parseStyleSheets(this.#rootStyleSheets);
126138

127139
const cssText = (tags.get(tagName) ?? [])
128140
.flatMap((moduleName) => modules.get(moduleName) ?? [])
129141
.map((rule) => rule.cssText)
130142
.join('\n');
131143

132-
const stylesheet = this.#styleSheetsByTag.get(tagName) ?? new CSSStyleSheet();
144+
const stylesheet = this.#styleSheetsByTag.get(tagName);
133145
stylesheet.replaceSync(cssText);
134-
this.#styleSheetsByTag.set(tagName, stylesheet);
135146

136147
this.#componentsByTag.get(tagName)?.forEach((component) => {
137148
if (cssText) {

0 commit comments

Comments
 (0)