In an attempt to create a Material UI theme using existing colors defined as CSS variables in my-palette.scss
, the following code is utilized:
:root {
--primary-color: '#FF0000';
...
}
The goal is to incorporate these colors like so:
import { createMuiTheme } from '@material-ui/core/styles';
export const muiTheme = createMuiTheme({
palette: {
primary: {
main: 'var(--primary-color)',
},
},
});
This command triggers an error message:
Error: Material-UI does not support the color format
var(--primary-color)
. Supported formats include: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla().
A Github thread suggests that this feature is currently unsupported: Support CSS variables as theme option.
Is there a workaround available to utilize var(--primary-color)
as the primary color in a Material UI createMuiTheme
?
The objective is to easily override default Material UI component colors with custom ones such as primary, secondary, etc.
<Radio color="primary" />
Alternative attempts have included extracting colors from the palette like this:
const cssVariables = {
primaryColor: getComputedStyle(document.documentElement).getPropertyValue('var(--primary-color)'),
};
However, utilizing cssVariables.primaryColor
has been ineffective and non-intuitive.
The final proposed solution involves manually recreating the palette as a standard object and using it directly, but this method seems cumbersome for maintenance purposes.