【问题标题】:Material-UI Color Prop OptionsMaterial-UI 颜色道具选项
【发布时间】:2021-05-18 18:09:17
【问题描述】:

我目前使用material-ui有以下主题:

import { createMuiTheme } from "@material-ui/core/styles";
import { blue, green } from "@material-ui/core/colors";

const theme = createMuiTheme({
  palette: {
    primary: blue,
    secondary: green,
  },
});

export default theme;

现在,如果我对从 material-ui 颜色导入的 green 对象执行 console.log,我会得到这样的颜色对象:

green = {
  50: "#e8f5e9"
  100: "#c8e6c9"
  200: "#a5d6a7"
  300: "#81c784"
  400: "#66bb6a"
  500: "#4caf50"
  600: "#43a047"
  700: "#388e3c"
  800: "#2e7d32"
  900: "#1b5e20"
  A100: "#b9f6ca"
  A200: "#69f0ae"
  A400: "#00e676"
  A700: "#00c853"
}

因此,我认为我可以将AppBar 组件设置为如下样式:<AppBar color="secondary.800">

但是,这不起作用。我也试过了:<AbbBar color="secondary[800]"> 也不管用。

我什至尝试过这个主题的变化:

import { createMuiTheme } from "@material-ui/core/styles";
import { blue, green } from "@material-ui/core/colors";

const theme = createMuiTheme({
  palette: {
    primary: blue,
    secondary: {...green},
  },
});

export default theme;

这也行不通。

有没有办法让我访问green 颜色对象相对于AppBar 组件中的color 属性的不同颜色变化?

如果有,怎么做?

谢谢。

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    Material UI 无法实现您想要做的事情。您只能设置颜色变体(主要或次要),颜色将从您的主题中获取。

    要手动设置不同的颜色,您需要覆盖组件的样式。

    import { withStyles } from "@material-ui/core/styles";
    import { blue } from "@material-ui/core/colors";
    
    const MyChildComponent = ({ classes }) => <AppBar classes= {{ root: classes.root }} />
    
    const styles = () => ({
      root: {
         backgroundColor: blue[400],
      }
    });
    
    export default withStyles(styles)(MyChildComponent);
    
    --------
    
    import { withStyles } from "@material-ui/core/styles";
    import { green } from "@material-ui/core/colors";
    
    const MyParentComponent = ({ classes }) => <MyChildComponent classes={{ root: classes.someClass}} />
    
    const styles = () => ({
      someClass: {
         backgroundColor: green[400],
      }
    });
    
    export default withStyles(styles)(MyParentComponent);
    

    当然,您可以定义一些内部逻辑,根据您传递的一些道具来决定在您的子组件中应用哪个类。

    import clsx from "clsx";
    import { withStyles } from "@material-ui/core/styles";
    import { red, green, blue } from "@material-ui/core/colors";
    
    const MyChildComponent = ({ color, classes }) => <AppBar className={clsx({
       [classes.red]: color === "red",
       [classes.blue]: color === "blue,
       [classes.green]: color === "green",
    })}/>
    
    const styles = () => ({
      red: {
         backgroundColor: red[400],
      },
      green: {
         backgroundColor: green[400],
      },
      blue: {
         backgroundColor: blue[400],
      },
    });
    
    export default withStyles(styles)(MyChildComponent);
    
    --------
    
    const MyParentComponent = () => <MyChildComponent color="red"/>
    
    export default MyParentComponent;
    

    就我个人而言,我只想手动覆盖上面显示的类。

    【讨论】:

    • 谢谢,这正是我需要知道的。
    【解决方案2】:

    根据 Material UI documentation,您可以使用数组表示法访问颜色变化并定义主题,如

    const theme = createMuiTheme({
      palette: {
        primary: {
          main: blue[600]
        },
        secondary: {
          main: green[800]
        }
      }
    });
    

    工作代码沙盒链接:https://codesandbox.io/s/material-colors-9hnqh?file=/src/App.js

    【讨论】:

    • 谢谢,但这不是我想要完成的。我知道我可以从primarysecondary 选项中创建一个main 变体。不过,我想做的是创建一个50100200 变体,然后将它们与组件中的color 属性一起使用。我想知道这是否可能。
    • 在这里查看material-ui.com/customization/default-theme/#explore 可能的调色板选项。您不能使用主题创建上述变体。您需要具体使用提供的调色板主要/次要选项
    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 2020-08-29
    • 2020-08-06
    相关资源
    最近更新 更多