MUI是一个非常愉快的合作伙伴。在React样式和布局方面,似乎“人人都有自己的方式”。作为一个库开发者,管理这种灵活性的任务必定非常巨大。
我的自定义MUI的方式是使用全局主题。因此,我创建了类名来添加到MuiContainer
中。我依赖于overrides
中的选择器,现在转而使用mui组件特定的styleOverrides
属性。
我的选择器无法正常工作,原因如下:
// v4 class names
.MuiContainer-root
// v5 class names
.css-1oqqzyl-MuiContainer-root
有没有一种方法可以让主题引擎以相同的方式呈现类名?例如,这是否表明mui依赖于
emotion
?另外,当在v4中呈现时,类名包括我的自定义类。
.MuiContainer-root.Luci-AppLayout.root
根据这个附录,在v5中,对于给定的元素有3组类名:
- 没有前缀的mui
- 带前缀的mui(在v4中不存在)
- 我的自定义类“按原样”
附录
根据@Ryan Cogswell的评论,两个版本的类名都会呈现在html中。然而,在开发工具的检查窗口中,用于样式化元素的唯一类是带有前缀的类。以下是我在沙盒中尝试复制问题的第一个版本。请查看开发工具检查器。 https://codesandbox.io/embed/sad-varahamihira-pv73t5?fontsize=14&hidenavigation=1&theme=dark 以下是来自沙盒的代码:
import Button from "@mui/material/Button";
import "./styles.css";
import { createTheme } from "@mui/material/styles";
import { ThemeProvider } from "@mui/styles";
const theme = createTheme({
ccomponents: {
// Name of the component
MuiButton: {
styleOverrides: {
// Name of the slot
root: {
// Some CSS
color: "red",
"&.Custom": {
color: "green"
}
}
}
}
}
});
export default function App() {
return (
<ThemeProvider theme={theme}>
<div>
<h1>Hello CodeSandbox</h1>
<Button className="Custom">Testing</Button>
</div>
</ThemeProvider>
);
}
.MuiContainer-root
)在v5中仍然存在,除此之外还有由emotion生成的类名。 - Ryan Cogswell