Material UI Adding Custom Color in Theme Not Recognized

sgrozz

I am trying to create a custom theme with custom colors using Material UI in React. When I create the custom theme and view it in the console, I can see the new color with its value. But when I try to reference it in a component, in my case a Switch, I get the following error

index.js:1 Warning: Failed prop type: Invalid prop color of value neutral supplied to ForwardRef(Switch), expected one of ["default","primary","secondary"]

When I set the color to "primary" or "secondary", I get the expected color. It just seems to not allow for the new color I created.

Here is my theme:

import { createMuiTheme } from "@material-ui/core/styles";

const customTheme = createMuiTheme({
    palette: {
        primary: {
            main: "#33d9b2"
        },
        secondary: {
            main: "#40407a"
        },
        neutral: {
            main: "#ff793f"
        }
    }
});

export default customTheme;

Here is my Switch component:

<FormGroup>
   <FormControlLabel
        control={<Switch color="neutral" checked={this.props.isChecked} onChange={this.handleToggleChecked} />}
        label="2018 Tracks">
   </FormControlLabel>
</FormGroup>
Giovanni Esposito

Ciao, according to Material UI Switch docs, color prop accepts only default, primary or secondary values.

If you want to make a switch with your neutral color, you have to make a custom switch. Something like:

import { withStyles } from '@material-ui/core/styles';
...
const NeutralSwitch = withStyles({
    switchBase: {
        color: "#808080",  //grey if unchecked
        '&$checked': {
            color: "#ff793f",   //neutral color
        },
        '&$checked + $track': {
            backgroundColor: "#ff793f",   //neutral color
        },
    },
    checked: {},
    track: {},
})(Switch);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Adding breakpoint to custom theme in Material UI Next (reactjs)

Material ui custom theme implementation

Material UI default theme overrides custom theme

Custom JSS theme overwrites material UI theme?

Set new color for material-ui theme

material-ui : Extract color from theme

Text color not working in Material-UI Theme

Where is color property defined when creating a custom theme for Material-UI

Change custom theme Material-UI

How to apply custom theme in material ui

Using Material-ui custom theme with redux

Angular Material Custom Theme from Material Color Tool

Reference to theme's primary color instead of a specific color in Material UI

Adding custom CSS styling to Material UI KeyboardDatePicker

Material UI Palette custom color with ThemeProvider

Change root background color with Material-UI theme

Is there a way to update Material UI theme palette primary and secondary color at runtime?

Material-UI - How to change default color for dark theme?

Material UI: Unable to change button text color in theme

How to define a custom theme in angular material as to control the color of the button text?

React / Material-UI: Use variable reference in custom theme

How to access Material-UI custom theme inside children of ThemeProvider

Deprecated typography warning when using custom theme in Material-UI

How to import and use a custom font in a material-ui theme?

Material UI Next Theme

Material UI autocomplete not being recognized

MudBlazor custom color theme

Custom Color Flutter Theme

material ui theme palette does not apply the primary color to the primary button background color