React Material UI: How to give a button a custom color when disabled?

前端 未结 3 2131
面向向阳花
面向向阳花 2021-02-14 11:23

I\'m building a React App using Material UI.

If the button is disabled, it is grey and opaque. I\'d like it to be in my themes primary color and opaque.

So here

相关标签:
3条回答
  • 2021-02-14 11:44

    You can define a class to be applied for disabled buttons:

    const styles = theme => ({
      disabledButton: {
        backgroundColor: theme.palette.primary || 'red'
      }
    });
    

    And then, use it like this:

    <Button
      variant="contained"
      color="secondary"
      disabled
      classes={{ disabled: classes.disabledButton }}
    >
      Disabled
    </Button>
    

    You can find in the documentation all the classes that you can override.

    0 讨论(0)
  • 2021-02-14 11:46

    Neps answer is correct, but I will add more details.

    First of all you should import createMuiTheme and ThemeProvider:

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

    Create theme:

    const theme = createMuiTheme({
      palette: {
        action: {
          disabledBackground: 'set color of background here',
          disabled: 'set color of text here'
        }
      }
    });
    

    And wrap your button:

    <ThemeProvider theme={theme}>
      <Button color="primary">Primary</Button>
    </ThemeProvider>
    
    0 讨论(0)
  • 2021-02-14 11:50

    Or you can try createMuiTheme and customize the following property:

    import { createMuiTheme } from '@material-ui/core/styles'
    
    const theme = createMuiTheme({
      palette: {
        action: {
          disabledBackground: 'set color of background here',
          disabled: 'set color of text here'
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题