【发布时间】:2021-11-21 16:48:47
【问题描述】:
TL;DR:滚动到 EDIT2
MUIv4 从我的嵌套主题中为我创建了以下类:
<label class="MuiFormLabel-root-121
MuiInputLabel-root-113
MuiInputLabel-formControl-115
MuiInputLabel-animated-118
MuiInputLabel-shrink-117
MuiInputLabel-marginDense-116
MuiInputLabel-outlined-120
Mui-disabled
Mui-disabled
MuiFormLabel-filled-123"
data-shrink="true">Email</label>
现在我有兴趣更改以下课程:
.MuiInputLabel-outlined-120.MuiInputLabel-shrink-117 {
transform: translate(14px, -6px) scale(0.75);
}
因此我有一个主题文件 [复制只是为了展示我尝试过的内容]:
createTheme({
overrides: {
MuiInputLabel: {
outlined: {
color: red[500],
backgroundColor:purple[600],
MuiInputLabel: {
shrink: {
transform: "translate(0px, -15px) scale(0.75) !important",
}
},
"&.MuiInputLabel-shrink": {
transform: "translate(0px, -15px) scale(0.75) !important",
},
"&[class*='MuiInputLabel-shrink']":{
transform: "translate(0px, -15px) scale(0.75) !important",
},
}
},
},
})
但是所有规则都不起作用? 我究竟做错了什么? 如何查看 createTheme 生成的类名?
编辑 - 我能够使用 CSS Wrapper 实现想要的效果:
const MUIWrapper = styled.div`
[class*="MuiInputLabel-outlined"][class*="MuiInputLabel-shrink"] {
transform: translate(0px, -15px) scale(0.75);
}
}
`
但其实我并不想这样实现
编辑 2:
正如@hotcakedev 所指出的,可以:
createTheme({
overrides: {
...
MuiFormLabel: {
outlined: {
'.MuiInputLabel-shrink*': { // or '&.MuiInputLabel-shrink*'
transform: translate(14px, -6px) scale(0.75);
}
},
}
},
})
但我还是想知道为什么不可能这样写:
createTheme({
overrides: {
...
MuiFormLabel: {
outlined: {
MuiInputLabel: {
shrink: {
transform: translate(14px, -6px) scale(0.75);
}
}
},
}
},
})
这就是我想写的,因为它清晰易读!
【问题讨论】:
-
你使用哪个版本的 MUI,4 还是 5?
-
@hotcakedev 谢谢你的提示。我添加了版本信息以及 EDIT
标签: javascript css reactjs material-ui