在 MUI v5.0+ 中,您可以通过以下方式轻松实现:
<Button type="submit" color="primary" sx={ { borderRadius: 28 } }>Enter</Button>
如果你想重复使用相同的样式,你可以从外部文件中导入它,你的代码会是这样的:
<Button type="submit" color="primary" sx={ { ...styles.button.rounded } }>Enter</Button>
或者通过主题(MUI v5)全局影响所有按钮:
import { createTheme } from '@mui/material';
const theme = createTheme({
components: {
MuiButton: {
styleOverrides: {
root: {
borderRadius: 28,
},
},
},
},
});
我也尝试过创建一个新变体(v5.0 中的新变体),但我认为更复杂,因为您必须通过添加的每个道具添加许多样式:
创建主题功能
MuiButton: {
defaultProps: {
variant: 'contained',
color: 'inherit'
},
styleOverrides: {
containedInherit: {
backgroundColor: '#fff'
}
},
variants: [
{
props: { variant: 'rounded' },
style: {
borderRadius: 28
}
},
{
props: { variant: 'rounded', color: 'primary' },
style: {
color: 'white',
backgroundColor: '#01697d'
}
}
]
}
此外,通过 sx prop 解决方案,您可以将变体与圆形样式(轮廓和包含)结合起来。