I am trying to pass a context variable as a value to tailwind className in react. As per tailwind documentation bg-[hex-val] is used to pass custom color codes to background color.
I'm using Template literals to pass context variable as value.
NavBar.js
import { useContext } from 'react';
import { AiOutlineMenu } from 'react-icons/ai';
import ThemeToggle from './ThemeToggle';
import ThemeContext from '../context/ThemeContext';
const NavBar = () => {
const { colors } = useContext(ThemeContext);
return <div className="w-screen h-46 bg-secondary-dark grid grid-cols-6 gap-4 content-center">
{//below line is not working}
<p className={`bg-[${colors.secondary}] text-text-white`}>Some words</p>
<AiOutlineMenu style={{color:"white",fontSize:"1.5rem"}} className='ms-4 place-self-start col-span-5 '/>
<ThemeToggle />
</div>
}
export default NavBar;
ThemeContext.js
import { createContext,useState } from "react"
const ThemeContext = createContext();
const ThemeProvider = ({ children })=>{
const [darkTheme, setTheme] = useState(true);
const colors = {
primary: darkTheme ? "#282828" : "#E8E8E8",
secondary: darkTheme ? "#FFFFFF" : "#FFFFFF",
secondary2: darkTheme ? '#232323' : '#ECECEC',
card: darkTheme ?'#383838' : 'F3EFEF',
buttons: darkTheme ? '#504D4D' : '#C0C0C0',
buttonActive: darkTheme ? '#A9A9A9' : '#828282'
}
const handleTheme = (themeParam)=>{
setTheme(themeParam);
}
return<>
<ThemeContext.Provider value={{darkTheme, handleTheme, colors}}>
{children}
</ThemeContext.Provider>
</>
}
export {ThemeProvider};
export default ThemeContext;
I have tried using classNames npm package to combine context variable and other classnames but no luck!