简体   繁体   English

在 Mui 中,如何在我的 CSS 中使用主题值

[英]In Mui, how do I use theme values in my css

My React app is using mui themes;我的 React 应用程序正在使用 mui 主题; index.js contains: index.js 包含:

let theme = createTheme({
  palette: {
    primary: {
      main: "#00aaa0",
      contrastText: '#fcf4d9',
    },
    secondary: {
      main: "#D55B3E",
      contrastText: '#fcf4d9',
    },
  },
});

theme = responsiveFontSizes(theme);

ReactDOM.render(
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <App />
    </ThemeProvider>
  document.getElementById('root')
);

In one of my components (footer) I want to be able to add a top border which is in the primary colour.在我的一个组件(页脚)中,我希望能够添加一个原色的顶部边框。 So far footer.tsx contains:到目前为止 footer.tsx 包含:

const Footer= () => {

  return (
    <div className="pageFooter">Footer Text</div>
  );
};

export default Footer;

I want to style "pageFooter" so that it uses theme.palette.primary.main as a top border colour.我想设置“pageFooter”的样式,以便它使用 theme.palette.primary.main 作为顶部边框颜色。 With regular css I would link in my css file containing:使用常规 css,我将链接到我的 css 文件中,其中包含:

.pageFooter {
  border-top: 2px solid "#00aaa0";
}

but I want to make sure that the colour is always the same as the primary colour, so I want to do something like this:但我想确保颜色始终与原色相同,所以我想做这样的事情:

.pageFooter {
  border-top: 2px solid theme.palette.primary.main;
}

This doesn't work, though, presumably because the theme is not available to the css file.但是,这不起作用,大概是因为主题不适用于 css 文件。 I've read the docs and I can't really follow them.我已经阅读了文档,但我无法真正理解它们。 Can anyone explain what I should be doing here?谁能解释我应该在这里做什么?

import { makeStyles } from '@material-ui/core'

const useStyles = makeStyles(theme => ({ 
   pageFooter: {
     borderColor: theme.palette.primary.main,
   }
});

const Footer= () => {
    const classes = useStyles()

  return (
    <div className={classes.pageFooter}>Footer Text</div>
  );
};

export default Footer;

according to the MUI docs:根据 MUI 文档:

@mui/styles is the legacy styling solution for MUI. @mui/styles 是 MUI 的遗留样式解决方案。 It depends on JSS as a styling solution, which is not used in the @mui/material anymore, deprecated in v5.它依赖于 JSS 作为样式解决方案,@mui/material 中不再使用它,在 v5 中已弃用。

and also并且

@mui/styles is not compatible with React.StrictMode or React 18. @mui/styles 与 React.StrictMode 或 React 18 不兼容。

MUI use JSS for styling. MUI 使用 JSS 进行样式化。 so we don't have access to theme in CSS files.所以我们无法访问 CSS 文件中的主题。 my solution: you can define a CSS variable same color as your theme palette color.我的解决方案:您可以定义一个与主题调色板颜色相同颜色的 CSS 变量。 then use it in CSS files.然后在 CSS 文件中使用它。

:root {
    --primary-main: #00aaa0;
  }
.pageFooter {
  border-top: 2px solid var(--primary-main);
}

It's supported now by MUI v5.6.0 as an experimental feature. MUI v5.6.0 现在作为一项实验性功能支持它。 They export theme variables as CSS variables.它们将主题变量导出为 CSS 变量。 Read the docs .阅读文档

Example:例子:

/* external-scope.css */
.external-section {
  background-color: var(--mui-palette-grey-50);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM