Skip to content

Commit b6bdfda

Browse files
Create variable-collector.js
1 parent b523950 commit b6bdfda

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

variable-collector.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// store color themes
2+
const colorTheme = [];
3+
const htmlStyles = getComputedStyle(document.documentElement);
4+
const targetStylesheet = document.querySelector("#color-themes");
5+
const regex = /--([^:\s]+):\s*var\(--([^)]+)\);/g;
6+
if (targetStylesheet) {
7+
const rules = targetStylesheet.sheet.cssRules || targetStylesheet.sheet.rules;
8+
for (const rule of rules) {
9+
if (rule.cssText.includes("data-theme=") && !rule.cssText.includes(`data-theme="0"`)) {
10+
const styleObject = {};
11+
let match;
12+
while ((match = regex.exec(rule.cssText)) !== null) {
13+
const key = "--" + match[1];
14+
const value = htmlStyles.getPropertyValue("--" + match[2]);
15+
styleObject[key] = value;
16+
}
17+
colorTheme.push(styleObject);
18+
}
19+
}
20+
}
21+
// ...colorThemes[0]
22+
export const colorThemes = colorTheme;

0 commit comments

Comments
 (0)