Created
July 2, 2021 09:59
-
-
Save typeofweb/da6ca1367fb2c70b0e1cf694557b44d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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