Skip to content

Instantly share code, notes, and snippets.

@typeofweb
Created July 2, 2021 09:59
Show Gist options
  • Save typeofweb/da6ca1367fb2c70b0e1cf694557b44d7 to your computer and use it in GitHub Desktop.
Save typeofweb/da6ca1367fb2c70b0e1cf694557b44d7 to your computer and use it in GitHub Desktop.
const getAllCSSVariableNames = () =>
Array.from(document.styleSheets)
.filter((s) => s.href === null || s.href.startsWith(window.location.origin))
.flatMap((s) => Array.from(s.cssRules))
.filter((r): r is CSSStyleRule => r.type === CSSRule.STYLE_RULE)
.flatMap((r) => (r.selectorText === ":root" ? Array.from(r.style) : []))
.filter((name) => name.startsWith("--"))
.sort();
const getCSSColorVariableNames = () =>
getAllCSSVariableNames().filter((name) => name.startsWith("--color-"));
const getCSSColorVariables = () => {
const computedStyle = window.getComputedStyle(document.documentElement);
return getCSSColorVariableNames().reduce<Record<string, string>>((acc, name) => {
acc[name] = computedStyle.getPropertyValue(name);
return acc;
}, {});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment