1

I'm using tailwind v3 and according to the docs, it's possible to override existing colors using the tailwind.config file

https://tailwindcss.com/docs/customizing-colors

module.exports = {
    theme: {
        extend: {
            colors: {
                gray: {
                    "50": "#fcefed",
                    "100": "#fadedb",
                    "200": "#f7ceca",
                    "300": "#f5bdb8",
                    "400": "#f2ada6",
                    "500": "#ef9d94",
                    "600": "#ed8c82",
                    "700": "#ea7c71",
                    "800": "#e86b5f",
                    "900": "#e55b4d"
                }
            }
        }
}

I want to override the color palette when a user clicks on a button. How can I override it during runtime? Ideally, I could use the same convention as the tailwind config file, something like that:

    //user click on a button
    const onColorsPicked = () => {
        const colors = {
            "50": "#fcefed",
            "100": "#fadedb",
            "200": "#f7ceca",
            "300": "#f5bdb8",
            "400": "#f2ada6",
            "500": "#ef9d94",
            "600": "#ed8c82",
            "700": "#ea7c71",
            "800": "#e86b5f",
            "900": "#e55b4d"
        }
        //what would be the best approach to implement this function? it's even possible?
        overrideTailwindColor("gray", colors)
    }

Not sure if the just-in-time-mode could help or not (https://v2.tailwindcss.com/docs/just-in-time-mode)

What would be the best approach to do that? There are any CSS variables I can override instead? Or I should get all the classes that tailwind could generate and override them? (sounds like a huge overkill)

c137
  • 181
  • 9

1 Answers1

0

I want to override the color palette when a user clicks on a button. How can I override it during runtime?

Short answer is you can't do this!

because tailwind is installed as a dev dependency it means it'll not go to production. under the hood, tailwind work as a compiler and then compile your code to CSS you can't compile this code on runtime.

"devDependencies": {
    "autoprefixer": "^10.4.13",
    "postcss": "^8.4.20",
    "tailwindcss": "^3.2.4"
 }
zain ul din
  • 1
  • 1
  • 2
  • 23