我是 Material UI 和 React JS 的新手,我只是尝试添加 2 个按钮,一个是原色的,另一个是原色的。我怎样才能做到这一点
我正在使用情感库。这是我尝试过的。我不想改变主光的默认颜色,我想对我的按钮使用默认的主光颜色
<Button variant="contained" >Primary Button</Button>
<Button variant="contained" color="secondary" >secondary Button</Button>
以上 2 个按钮按预期工作。
<Button variant="contained" color="primary-light" >secondary Button</Button> //I know the syntax is wrong.
如何做到这一点?
<Button
style={{ backgroundColor: "#f8ad33" }}
variant="contained"
// color="primary"
onClick={handleSubmit(onSubmit)}
>
Register
</Button>
“primary-light”不是 MUI 按钮支持的颜色之一。 您可以在此处的文档中看到它here
您有几个选择:
primary.light
颜色。 (我不确定这在 Material UI 版本 5 中是否已经过时,但在 v4 中通常是这样做的)。在您的情况下,快速查看文档可能是最简单的Here,其中显示了如何使用主题和从 mui/material/styles 导入的
styled
函数自定义按钮上的颜色。
我必须在主题对象中创建“primaryLight”颜色属性,以便我可以使用。还必须添加类型以便 Button 可以接受“primaryLight”。
我用了这个文档
一个 hacky 解决方法是使用定义的主题颜色,然后用 sx 属性覆盖。
<Button
color={'primary'}
variant='contained'
onClick={() => toggle(true)}
sx={{width: '100%', bgcolor: 'primary.dark', '&:hover': {bgcolor: 'primary.light'}}}
>
comments
</Button>
您可以在主题文件中使用类似的内容:
declare module '@mui/material/Button' {
interface ButtonPropsColorOverrides {
'primary.dark': true
'primary.light': true
}
}
另外这个答案帮助了我