Skip to content

Commit 4f3eb02

Browse files
committed
Extract unresolved css variables
1 parent 1ebf8fc commit 4f3eb02

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

packages/engramma/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "engramma",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Web component to add Engramma on your website",
55
"keywords": [],
66
"author": "Bogdan Chadkin <opensource@trysound.io>",

packages/engramma/src/engramma-app.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,28 @@ import prismStyles from "prismjs/themes/prism-tomorrow.min.css?raw";
1717

1818
const extractUserCssVariables = () => {
1919
let css = ``;
20-
const root = document.documentElement;
21-
if (root.computedStyleMap) {
22-
for (const [property, cssValue] of root.computedStyleMap()) {
23-
if (property.startsWith("--")) {
24-
for (const value of cssValue) {
25-
css += `${property}: ${value.toString()};\n`;
26-
}
27-
}
20+
21+
for (const sheet of document.styleSheets) {
22+
let rules;
23+
try {
24+
rules = sheet.cssRules; // throws for cross-origin sheets
25+
} catch {
26+
continue;
2827
}
29-
} else {
30-
// fallback to fully computed styles (aliases won't be inferred)
31-
const computedStyles = getComputedStyle(root);
32-
for (const property of computedStyles) {
33-
if (property.startsWith("--")) {
34-
const value = computedStyles.getPropertyValue(property);
35-
css += `${property}: ${value};\n`;
28+
29+
for (const rule of rules) {
30+
if (rule instanceof CSSStyleRule) {
31+
// find selectors like ":root, html"
32+
const parts = rule.selectorText.split(",").map((s) => s.trim());
33+
if (!parts.some((part) => part === ":root" || part === "html")) {
34+
continue;
35+
}
36+
for (const property of rule.style) {
37+
if (property.startsWith("--")) {
38+
const value = rule.style.getPropertyValue(property);
39+
css += `${property}: ${value};\n`;
40+
}
41+
}
3642
}
3743
}
3844
}

0 commit comments

Comments
 (0)