Im trying to import an object containing reusable TailwindCSS classes and reuse these classes across my app. For some reason I need to copy + paste this object locally INSIDE the react component I want to use it in for it to work.
// constants.ts (I want to reuse this object eg. (className={ElementTypeColors[type]}))
export const ElementTypeColors: { [key in IElementType]: string } = {
'alkali metal': 'bg-lime-400 dark:bg-lime-800',
metalloid: 'bg-yellow-400 dark:bg-yellow-900',
actinide: 'bg-orange-400 dark:bg-orange-900',
'alkali earth metal': 'bg-red-400 dark:bg-red-800',
'reactive non-metal': 'bg-blue-400 dark:bg-blue-800',
'unknown type': 'bg-gray-400 dark:bg-gray-700',
'transition metal': 'bg-violet-400 dark:bg-violet-800',
'noble gas': 'bg-fuchsia-400 dark:bg-fuchsia-800',
'post-transition metal': 'bg-teal-400 dark:bg-teal-800',
lanthanide: 'bg-sky-400 dark:bg-sky-800',
};
When I try to import it from a typescript constants file the classes get applied in the DOM but dont do anything style wise?
// Component.tsx
<Card className={`border-none p-1 col-span-1 ${ElementTypeColors[element.Type]}`}>
The code above only works if the ElementTypeColors
object is declared within the component itself and does not work when importing it for another file.
This feels unsustainable. If I decide to change the color setup for 1 key in this object I need to manually change it in all the instances of this object across my app.
I’m using Next.js 14 and React 18.
Does any one know how to fix if at all possible! cheeers!
2
Answers
My tailwind config file:
Also here is an element with correct tailwind classes in the DOM without any color classes taking effect (displays as transparent)
bg-blue-400 dark:bg-blue-800
:Thanks @Rico for giving a good hint on where the bug could be. After tinkering around with the
tailwind.config.ts
file I noticed that there is acontent
array in the config file which seems to set the directories from which tailwindCSS searches the classes for you JSX.TLDR;