As per the documentation:
The most important implication of how Tailwind extracts class names is that it will only find classes that exist as complete unbroken strings in your source files.
If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS:
Don’t construct class names dynamically
<div class="text-{{ error ? 'red' : 'green' }}-600"></div>
In the example above, the strings text-red-600 and text-green-600 do not exist, so Tailwind will not generate those classes.
Instead, make sure any class names you’re using exist in full:
Always use complete class names
<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
hover:bg-${color}-200, bg-${color}-100, bg-${color}-200 and hover:bg-${color}-400 work by coincidence since the resulting class appear as unbroken strings in other code scanned by Tailwind.
To resolve your problem, you could consider using full class names in definitions, like:
const color = 'bg-red-200 hover:bg-red-300';
<div className={color}>
Or you could consider adding the possible classes to the safelist:
/** @type {import('tailwindcss').Config} */
module.exports = {
// …
safelist: [
{ pattern: /^bg-(red|green|blue)-200$/ },
{
pattern: /^bg-(red|green|blue)-300$/,
variants: ['hover'],
},
],
// …
}