【问题标题】:Material-ui does not change Typography color according to themeMaterial-ui 不会根据主题改变 Typography 颜色
【发布时间】:2020-04-30 10:10:56
【问题描述】:

我正在尝试material-ui,所以我创建了两个主题:

const darkTheme = createMuiTheme({
  palette: {
    type: "dark"
  }
});

const lightTheme = createMuiTheme({
  palette: {
    type: "light"
  }
});

但是当我使用Typography 组件时,它的颜色属性不会改变。甚至更多——颜色是从html 继承的,所以Typography 不知道当前的主题。 有没有办法在创建主题或使用默认值时配置排版颜色? 我曾尝试将 color 道具放在托盘对象中,如下所示:

const darkTheme = createMuiTheme({
  palette: {
    type: "dark",
    typography: {
       body1: {
           color: '#fff'
       }
    }

  }
});

但是没有运气。我创建了codepen。在那里我发现如果我将material-ui 降级为3.1 它可以正常工作-.MuiTypography-body1 类设置与主题相对应的颜色属性。

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    Typography 的默认行为是不使用颜色。原因是通常颜色由控制背景颜色的相同组件控制。例如,如果将Typography 元素放在Paper 元素中,Paper 将同时控制background-colorcolor。为了让 html 和 body 元素符合您的主题,您需要使用 CssBaseline

    Typography 提供 color 属性来显式控制颜色。使用color="textPrimary" 将与set the color of the Typography 一样使用CssBaseline sets the color for the body element

    以下是演示该行为的工作示例:

    import React from "react";
    import { createMuiTheme, MuiThemeProvider } from "@material-ui/core/styles";
    import Typography from "@material-ui/core/Typography";
    import CssBaseline from "@material-ui/core/CssBaseline";
    import Paper from "@material-ui/core/Paper";
    import Button from "@material-ui/core/Button";
    
    const darkTheme = createMuiTheme({
      palette: {
        type: "dark"
      }
    });
    
    const lightTheme = createMuiTheme({
      palette: {
        type: "light"
      }
    });
    
    export default function App() {
      const [topLevelDark, setTopLevelDark] = React.useState(false);
      return (
        <MuiThemeProvider theme={topLevelDark ? darkTheme : lightTheme}>
          <CssBaseline />
          <Button
            variant="contained"
            color="primary"
            onClick={() => {
              setTopLevelDark(!topLevelDark);
            }}
          >
            Toggle Themes
          </Button>
          <div>
            <Typography variant="body1">I'm within the top-level theme</Typography>
            <MuiThemeProvider theme={topLevelDark ? lightTheme : darkTheme}>
              <Paper>
                <Typography variant="body1">
                  I'm in a Paper within the nested theme
                </Typography>
              </Paper>
              <Typography variant="body1" color="textPrimary">
                I'm in the nested theme with textPrimary color, but outside of a
                Paper. This makes me hard to read since nothing is setting the
                background-color to something contrasting.
              </Typography>
              <Typography variant="body1">
                I'm in the nested theme outside of a Paper with no explicit color.
              </Typography>
            </MuiThemeProvider>
          </div>
        </MuiThemeProvider>
      );
    }
    


    关于使用多个主题的说明

    在您的代码沙箱示例中,您有两个同级 ThemeProvider 元素,但在使用您自己的自定义主题时,您的自定义主题必须位于顶层。如果您为页面的一部分使用不同的主题,它应该嵌套在您的顶级主题中。如果您有两个顶级 ThemeProvider 元素(即没有一个嵌套在另一个中),它们都将试图影响全局 Material-UI CSS 类名称。这意味着只有其中一个会获胜,而另一个似乎根本不起作用。当 Material-UI 检测到您在嵌套的 ThemeProvider 中时,它将在嵌套主题中使用不同(后缀)的类名,并且嵌套主题将按预期工作。

    相关答案:

    【讨论】:

    猜你喜欢
    • 2021-05-29
    • 2020-12-06
    • 2020-03-27
    • 2019-10-07
    • 2019-03-05
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2019-04-19
    相关资源
    最近更新 更多