Last active
February 4, 2022 07:44
-
-
Save queviva/69f775902fa0174e690af16315545fd8 to your computer and use it in GitHub Desktop.
create list of all css variables
This file contains hidden or 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
// run after all styles load | |
const cssVars = { | |
list : () => [...new Set([...[ | |
[...document.styleSheets] | |
.map(s => [...s.cssRules] | |
.map(r => r.cssText)), | |
[...document.querySelectorAll('[style]')] | |
.map(n => n.getAttribute('style'))] | |
.join(' ') | |
.matchAll(/(--[^:;]*):/gm)] | |
.map(a => a[1]) | |
)], | |
obj: () => Object.fromEntries([...[ | |
[...document.styleSheets] | |
.map(s => [...s.cssRules] | |
.map(r => r.cssText)), | |
[...document.querySelectorAll('[style]')] | |
.map(n => n.getAttribute('style'))] | |
.join(' ') | |
.matchAll(/(--[^:;]*):([^;]*);/gm)] | |
.map(a => [a[1],a[2]]) | |
) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this will get really close to finding all css variable names; it looks in external styles and inline style parameters aswell
it has to be run either in a defer script, from a script tag after the body, or after window.onload - all the styles should be loaded before running this
the cssVars.obj() version seems worthless to me; the value of the variable will simple be whatever is coded - if you want to know the actual current value being used by an element, you have to .getPropertyValue('--css-variable') anyway, so I don't know why people would be interested in that, except maybe to see
matchAll()
and the regex?