Last active
March 16, 2023 22:28
-
-
Save ErickPetru/d56a698f6600d19697181fa475671ce5 to your computer and use it in GitHub Desktop.
Extract CSS custom properties (CSS variables) from Tailwind Color Palette by Web scrapping its documentation
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
/* Extract CSS custom properties (CSS variables) from Tailwind Color Palette | |
* Navigate to: https://tailwindcss.com/docs/customizing-colors | |
* Then open browser's console and run the following code | |
*/ | |
const palette = [ | |
...document | |
.querySelector("#content-wrapper > .grid.grid-cols-1") | |
.querySelectorAll(".flex.flex-col.space-y-3"), | |
] | |
.map((row) => ({ | |
color: row | |
.querySelector(".w-16.shrink-0") | |
.textContent.toString() | |
.toLowerCase(), | |
shades: [...row.querySelectorAll(".relative.flex > .cursor-pointer")].map( | |
(color) => ({ | |
shade: color.querySelector(".font-medium").textContent.toString(), | |
rgb: color | |
.querySelector(".h-10") | |
.style.backgroundColor.replace(/[rgb|(|)|,]/g, "") | |
.replace(")", ""), | |
hex: color | |
.querySelector(".font-mono") | |
.textContent.toString() | |
.toLowerCase(), | |
}) | |
), | |
})) | |
.reduce( | |
(acc, curr) => ({ | |
...acc, | |
[curr.color]: curr.shades.reduce( | |
(acc2, curr2) => ({ | |
...acc2, | |
[curr2.shade]: { rgb: curr2.rgb, hex: curr2.hex }, | |
}), | |
{} | |
), | |
}), | |
{} | |
); | |
console.log( | |
Object.keys(palette) | |
.map( | |
(color) => | |
`/* ${color[0].toUpperCase()}${color.substr(1)} */\n` + | |
Object.keys(palette[color]) | |
.map( | |
(shade) => | |
`--color-${color}-${shade}-rgb: rgb(${palette[color][shade].rgb});\n--color-${color}-${shade}-hex: ${palette[color][shade].hex};` | |
) | |
.join("\n") | |
) | |
.join("\n\n") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generating a new
950
shade for each color, and outputting a reversed color palette for automatic dark themes: