【问题标题】:Text color not working in Material-UI Theme文本颜色在 Material-UI 主题中不起作用
【发布时间】:2019-05-21 10:07:37
【问题描述】:

使用 Material-UI 创建颜色主题时,我将对比文本设置为白色 (#fff)。它适用于具有主要颜色的按钮,但不是次要颜色。

尝试过如下所述的覆盖:Material UI: Unable to change button text color in theme。如果覆盖可以解决它,那么我需要帮助编写一个。

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

期望两个按钮都有白色文本。取而代之的是一个黑色的按钮:

编辑:我创建了主题并在父级上渲染了 Material UI 的 SimpleModal 组件,将主题道具传递给了子级。按钮呈现在孩子身上。

父母:

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

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

孩子:

            <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>

【问题讨论】:

  • 您使用什么代码来创建按钮和使用主题?
  • 我认为颜色受背景影响。所以你必须选择正确的颜色和正确的背景。看到这个:material-ui.com/style/color

标签: javascript reactjs material-ui


【解决方案1】:

为了两种颜色都有白色文本,您需要:

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

contrastText 必须在每个颜色意图中指定。

这是一个完整的例子:

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

【讨论】:

  • 这很清楚,我将删除我的评论。在不清楚之前我在哪里搜索文档 - - 但我不记得那些确切的页面。
【解决方案2】:

尝试添加一个单独的 contrastText 并更改它直到匹配,因为颜色受背景影响。所以你必须选择正确的颜色和正确的背景。看到这个:https://material-ui.com/style/color/

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

    }
})

对于上面的两种颜色,请使用以下代码:

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

    }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2019-12-29
    • 2021-03-07
    • 1970-01-01
    • 2021-07-10
    • 2019-11-29
    • 2021-04-08
    相关资源
    最近更新 更多