Tailwind Shades to CSS Variables
This script can be used to generate a set of CSS variables from a Tailwind shades object.
Overview
Let's say you have the following JavaScript object. You might have generated such an object on a site such as Tailwind Shades.
{
'50': '#B2B7DF',
'100': '#A0A6D8',
'200': '#7C84C9',
'300': '#5863BA',
'400': '#414B9E',
'500': '#323A7A',
'600': '#232956',
'700': '#141832',
'800': '#06060D',
'900': '#000000'
}
Running this object through the shades-to-css
script (shades-to-css primary [object]
) will output the following CSS:
--primary-50: #B2B7DF;
--primary-100: #A0A6D8;
--primary-200: #7C84C9;
--primary-300: #5863BA;
--primary-400: #414B9E;
--primary-500: #323A7A;
--primary-600: #232956;
--primary-700: #141832;
--primary-800: #06060D;
--primary-900: #000000;
Usage
Create a new file anywhere in your project and paste the contents of shades-to-css.js
into that file.
Once you have done that, copy your shades object and run the following command:
node ./path/to/shades-to-css.js [color] "[shades]"
Here's an example:
node ./shades-to-css.js primary "{ '50': '#B2B7DF', '100': '#A0A6D8', '200': '#7C84C9', '300': '#5863BA', '400': '#414B9E', '500': '#323A7A', '600': '#232956', '700': '#141832', '800': '#06060D', '900': '#000000' }"
This will generate a set of CSS variables with the prefix --primary-
followed by the key of the object, e.g. 100
, 500
, 900
.
You can then use this set of CSS variables to create dynamic Tailwind colors. A good example is whitelabelling or "skinning" your user interface for different clients.
Options
--to-rgb
When this option is provided, any hexadecimal values inside of your shades object will be converted to RGB and used as the value of the property.
--unwrap-rgb
This option can be used alongside the --to-rgb
option. It will convert the hexadecimal value into an RGB string but instead of wrapping the values in rgb()
, it will output a comma-separated list instead.
The use case for this option is generating custom color palettes that support opacity and other Tailwind modifiers. You can read more about this here.