我想声明一些css变量,我将在我的组件中重用它们。这就是你如何用普通的css做到这一点:
:root {
--box-shadow: 0 2px 5px -1px rgba(0, 0, 0, 0.3);
}然后将按如下方式使用:
.my-class {
box-shadow: var(--box-shadow);
}怎样才能用useStyles实现同样的效果?我尝试了以下几种方法,但都没有效果:
const theme = createMuiTheme({
shadowing: {
boxShadow: "0 2px 5px -1px rgba(0, 0, 0, 0.3)",
}
});我的主应用程序包含在
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>我尝试在我的组件中使用它:
const useStyles = makeStyles(theme => ({
workImage: {
boxShadow: theme.shadowing,
},
}));但它不起作用。
发布于 2019-12-08 10:39:16
"createMuiTheme“函数接受具有有限键集的对象。(调色板,字体,spacing...etc)
你可以只使用普通对象。
const theme = {
shadowing: {
boxShadow: "0 2px 5px -1px rgba(0, 0, 0, 0.3)",
}
};发布于 2019-12-08 10:50:35
在我的例子中,我必须同时使用createMuiTheme和自定义变量。为了实现这一点,我做了以下工作。
首先,我用createMuiTheme创建了一个主题
const theme = createMuiTheme({
typography: {
fontFamily: "verdana",
},
});然后我创建了一个单独的对象,我在其中添加了我的变量:
const cssVariables = {
shadowing: {
boxShadow: "0 2px 5px -1px rgba(0, 0, 0, 0.3)",
}
};然后将这两个对象传递给我的ThemeProvider:
<ThemeProvider theme={{ ...theme, ...cssVariables }}>最后,访问变量:
const useStyles = makeStyles(theme => ({
workImage: {
boxShadow: theme.shadowing.boxShadow,
},
}));https://stackoverflow.com/questions/59231839
复制相似问题