A Tailwind plugin to mix colors with CSS color-mix #13085
JavierM42
started this conversation in
Show and tell
Replies: 1 comment
-
This plugin is neat. It takes a bit of work to make it work correctly with tailwind-merge, but the end result is neat. I ended up swapping the order tailwind-merge configurationimport { extendTailwindMerge, fromTheme } from "tailwind-merge";
type AdditionalClassGroupIds = "mix-bg" | "mix-bg-amount" | "mix-bg-method";
type AdditionalThemeGroupIds = never;
const arbitraryValueRegex = /^\[(?:([a-z-]+):)?(.+)]$/i;
const isArbitraryValue = (value: string) => arbitraryValueRegex.test(value);
const colors = fromTheme("colors");
const customTwMerge = extendTailwindMerge<
AdditionalClassGroupIds,
AdditionalThemeGroupIds
>({
extend: {
classGroups: {
"mix-bg": [colors],
"mix-bg-amount": [
{
"mix-bg-amount": ["20", "40", "60", "80", isArbitraryValue],
},
],
"mix-bg-method": [
"mix-bg-method-shorter-hue",
"mix-bg-method-longer-hue",
],
},
conflictingClassGroups: {
"mix-bg": ["mix-bg"],
"mix-bg-amount": ["mix-bg-amount"],
"mix-bg-method": ["mix-bg-method"],
},
},
});
export { customTwMerge as twMerge }; BeforeAfterEnd resultSome much more interesting and varied colors achieved through merging color blends with the weather card's default styles. 🎉 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I recently built the tailwindcss-color-mix plugin to mix colors at runtime.
Live demo
My motivation was generating slightly different shades of colors to standardize hover/focus states, but there may be other use cases, so I tried to keep naming as generic as possible.
Usage
bg-red-500 bg-mix-blue-500 bg-mix-amount-50
How it works
bg
utilities now also set a--tw-bg-base
property to the same value they setbackground-color
to.bg-mix-amount
utilities control a--tw-bg-mix-amount
property.bg-mix
utilities overwrite thebackground-color
rule with acolor-mix(...)
call, usingvar(--tw-bg-base)
as the base color andvar(--tw-bg--mix-amount
) as the mix amount.Beta Was this translation helpful? Give feedback.
All reactions