【发布时间】:2021-05-17 05:58:23
【问题描述】:
您好,我有一个用于 Material-UI 应用程序的定制芯片组件,我正在使用灰色对象更改芯片的背景和边框颜色
现在当我通过全局主题切换到暗模式时 调色板:{ 类型:“黑暗” } 那些颜色不变。如果我们处于浅色或深色模式,是否有某种方法可以更改这些自定义颜色?
import React, { useState } from 'react';
import grey from'@material-ui/core/colors/grey';
import {Chip} from "@material-ui/core";
import {withStyles} from "@material-ui/core/styles";
const MyChip = withStyles(theme =>({
root: {
backgroundColor:grey[100],
borderStyle: "solid",
borderWidth: "1px",
borderColor: grey[300]
},
}))(Chip);
const ChipComponent = ({...props}) => {
return <MyChip {...props} />
}
export default ChipComponent;
【问题讨论】:
-
“通过主题切换到深色模式”具体是什么意思?我问的原因是我想知道你是否可以这样做:
backgroundColor: theme.darkMode ? grey[800] : grey[100], -
@BenStephens,感谢您的澄清,在我的应用程序中,我有一个自定义主题,该主题放置在单独的 Theme.js 文件中,并通过
在这个全局主题中,我可以通过调色板在暗模式和亮模式之间切换:{ type: "light", } 它可以设置为 type: "dark", -
@BenStephens 现在我将您的建议添加到我的自定义芯片组件 backgroundColor: theme.darkMode ? gray[800] : gray[100] 看起来它没有识别darkMode(即使它设置在 Theme,js 文件中,因为它只应用了正确的参数 gray[100]
-
类似:
backgroundColor: theme.palette.type === 'dark' ? grey[800] : grey[100]怎么样?希望这只是在主题中找到正确的属性来判断您是否处于黑暗模式的问题。
标签: reactjs material-ui