I recently ran into an issue that my custom colors are not working whenever I make them dynamically. I will provide 2 codes and 2 screenshots, one with the dynamical code, one with the dynamical output, one with the static code and one with the static output.
I refuse to acutally program them statically because I get that information from a seperate file that can change easily so it’s easier for me to add one instead of creating a whole new divset.
Dynamic:
<div className='w-full bg-bgWhite'>
<div className='w-1/2 grid grid-rows-3 gap-10 grid-cols-3'>
{skills.map((skill, index) => (
<div className={`p-5`}>
{createElement(skill.icon.type, {
className: `w-20 h-20`,
})}
<p className={`text-[${skill.color}]`}>{skill.color}</p>
</div>
))}
</div>
</div>
Static:
<div className='w-full bg-bgWhite'>
<div className='w-1/2 grid grid-rows-3 gap-10 grid-cols-3'>
{skills.map((skill, index) => (
<div className={`p-5`}>
{createElement(skill.icon.type, {
className: `w-20 h-20`,
})}
<p className={`text-[#3FB27F]`}>{skill.color}</p>
</div>
))}
</div>
</div>
QUICK SIDENOTE
Whenever I reset the code back to the dynamical form after executing the static form I get this solution:
This is gone after restarting the project.
Help is very much appreciated! 🙂
3
Answers
Apparently postcss makes this impossible, the only solution is adding them in tailwind config
I think you need to active
just-in-time
mode.tailwind.config.js
with
safelisting classes
Because Tailwind class names should be extracted at build-time, you cannot use string concatenation for generating a class name with a dynamic arbitrary.
So
text-[${skill.color}]
won’t work.You can change
color
property’s value astext-[#3FB27F]
in your object then it will work.Or you can create a new array with
map
to then loop with anothermap
without touching your originalcolor
value in DB or wherever you get that:Suggestion: I didn’t see
key
attribute in your loop. So don’t forget to add it. If possible, don’t useindex
.