Skip to content

Instantly share code, notes, and snippets.

@hiteshchoudhary
Created February 9, 2024 08:26
Show Gist options
  • Save hiteshchoudhary/8213e145b1ed681336333430d8f53c79 to your computer and use it in GitHub Desktop.
Save hiteshchoudhary/8213e145b1ed681336333430d8f53c79 to your computer and use it in GitHub Desktop.
Tailwind Plugins
// Plugin to add each Tailwind color as a global CSS variable
function addVariablesForColors({ addBase, theme }: any) {
const allColors = flattenColorPalette(theme('colors'));
const newVars = Object.fromEntries(
Object.entries(allColors).map(([key, value]) => [`--${key}`, value])
);
addBase({
':root': newVars,
});
}
// Plugin to add utilities for SVG patterns
function addSvgPatterns({ matchUtilities, theme }: any) {
matchUtilities(
{
'bg-grid': (value: any) => ({
backgroundImage: `url("${svgToDataUri(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="32" height="32" fill="none" stroke="${value}"><path d="M0 .5H31.5V32"/></svg>`
)}")`,
}),
'bg-grid-small': (value: any) => ({
backgroundImage: `url("${svgToDataUri(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="8" height="8" fill="none" stroke="${value}"><path d="M0 .5H31.5V32"/></svg>`
)}")`,
}),
'bg-dot': (value: any) => ({
backgroundImage: `url("${svgToDataUri(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="16" height="16" fill="none"><circle fill="${value}" id="pattern-circle" cx="10" cy="10" r="1.6257413380501518"></circle></svg>`
)}")`,
}),
},
{ values: flattenColorPalette(theme('backgroundColor')), type: 'color' }
);
}
// add to tailwind plugins
plugins: [addVariablesForColors, addSvgPatterns],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment