[Tailwind&Prisma] How can you tie a tailwind color configuration to a prisma enum❓
Context:
- You persist a user setting in your db, e.g. “primaryColor”
- The CSS styles for this are applied via tailwindcss
Problem:
- I don’t want to store stuff like “primaryColorDark” as well in the DB -- tailwind’s config is my source of truth for that
- However, adding a color now requires a whole db migration
11 Replies
This is a great use case for css variables. You can store the color hex code in the DB for each user and set the Tailwind config to something like this. Now you can use classes like
text-primary
and set the correct --color-primary
variable for each user
and the implementation is like this
https://github.com/stneto1/tw-react-theme
you just need to populate the store with the possible theme values from prisma
But what if I don't want to define the
primary
, secondary
etc. myself but use something predefined, like radix-colors instead?
https://github.com/brattonross/windy-radix-paletteGitHub
GitHub - brattonross/windy-radix-palette: Bring Radix Colors to Tai...
Bring Radix Colors to Tailwind CSS. Contribute to brattonross/windy-radix-palette development by creating an account on GitHub.
the color type is a generic type
you can change to whatever you like
you can switch from Color type to the radix color
would change that the code should do
Okay
What's your recommendation for storing the color hex code in the database?
In it's own table?
I don't quite understand why I would store the hexCode in the database as well.
Like, wouldn't the
colorVariable
(or however you'd define this) be enough and then simply use the class names for applying the color?
I haven't worked with css variables a lot, so I'm a bit ignorant of the benefit.if you plan on using radix colors
you can make a soft link between them
on the database you store the theme
and load on client
something like this
Oh interesting
For clarification: you mean
redixColors[c.name]
right?
I think this could be what I was looking for. 🙌yea
discord isnt a good
fair
Do you know if there's a way for me to obtain the hex value at runtime?
Instead of the variable string, I'd need the hex value. :/
You have to check the radix theme for hex values