Skip to content

Commit d53f795

Browse files
committed
main.addon.Stylesheet: setCssVariable() #3891
1 parent bbb6a6b commit d53f795

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/main/addon/Stylesheet.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Stylesheet extends Base {
3131
'createStyleSheet',
3232
'deleteCssRules',
3333
'insertCssRules',
34+
'setCssVariable',
3435
'swapStyleSheet'
3536
]
3637
},
@@ -264,6 +265,37 @@ class Stylesheet extends Base {
264265
}
265266
}
266267

268+
/**
269+
* @param {Object} data
270+
* @param {String} data.key
271+
* @param {String} [data.priority] optionally pass 'important'
272+
* @param {String} data.theme
273+
* @param {String} data.value
274+
*/
275+
setCssVariable(data) {
276+
let key = data.key,
277+
rule, sheet;
278+
279+
if (!key.startsWith('--')) {
280+
key = '--' + key;
281+
}
282+
283+
for (sheet of document.styleSheets) {
284+
if (sheet.href.includes(data.theme)) {
285+
for (rule of sheet.cssRules) {
286+
if (rule.type === 1) { // CSSRule.STYLE_RULE
287+
if (rule.style.getPropertyValue(key) !== '') {
288+
rule.style.setProperty(key, data.value, data.priority);
289+
return true;
290+
}
291+
}
292+
}
293+
}
294+
}
295+
296+
return false;
297+
}
298+
267299
/**
268300
* @param {Object} data
269301
* @param {String} data.href

0 commit comments

Comments
 (0)