Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text color not working in Material-UI Theme

When creating a theme for colors with Material-UI, I set contrast text to white (#fff). It is working for the button with primary color, but not secondary.

Tried overrides as described here: Material UI: Unable to change button text color in theme. If an override will solve it, then I need help writing one.

const colortheme = createMuiTheme({
    palette: {
        primary: { main: '#e91e63' },
        secondary: { main: '#03a9f4' },
        contrastText: '#fff',
    }
});

Expecting both buttons to have white text. Instead got one button black:

enter image description here

Edit: I created the theme and rendered Material UI's SimpleModal component on the parent, passing theme props to the child. The button is rendered on the child.

parent:

const blues = createMuiTheme({
    palette: {
        primary: { main: '#00e5ff' },
        secondary: { main: '#2979ff' },
        contrastText: '#fff'
    }
})

 <SimpleModal label="content" theme={blues} color="primary" document="content" />

child:

            <div>
                <MuiThemeProvider theme={this.props.theme}>
                    <Button className={classes.margin} variant="contained" color={this.props.color} onClick={this.handleOpen}>{this.props.label}</Button>
                </MuiThemeProvider>
                <Modal open={this.state.open} onClose={this.handleClose}>
                    <div style={getModalStyle()} className={classes.paper}>
                        <Typography variant="h6" id="modal-title">{this.state.reference}</Typography>
                        <Typography variant="subtitle1" id="simple-modal-description">{this.state.content}</Typography>
                    </div>
                </Modal>
            </div>
like image 846
cDub Avatar asked Dec 20 '18 21:12

cDub


2 Answers

In order to have white text for both colors, you want:

const colortheme = createMuiTheme({
  palette: {
    primary: { main: "#e91e63", contrastText: "#fff" },
    secondary: { main: "#03a9f4", contrastText: "#fff" }
  }
});

The contrastText must be specified within each color intention.

Here's a full example showing this:

Edit 81xpxy6312

Documentation: https://material-ui.com/customization/palette/#providing-the-colors-directly

like image 160
Ryan Cogswell Avatar answered Nov 03 '22 00:11

Ryan Cogswell


Try to add a separate contrastText and change it until it matches because the color is affected by the background. So you have to choose the right color with the right background. See this:https://material-ui.com/style/color/

const blues = createMuiTheme({
    palette: {
        primary: { main: '#00e5ff',contrastText: '#fff', },
        secondary: { main: '#2979ff',contrastText: '#000', },

    }
})

For the two color above use this code:

const blues = createMuiTheme({
    palette: {
        primary: { main: '#e91e63' },
        secondary: { main: '#0277bd' },

    }
})
like image 33
I_Al-thamary Avatar answered Nov 03 '22 01:11

I_Al-thamary