简体   繁体   English

如何覆盖使用 makeStyles 创建的 CSS 类

[英]How to override CSS classes made with makeStyles

Note, I'm using MUI 4.12.3.注意,我使用的是 MUI 4.12.3。 In file AI have this (simplified) besides a functional component.在文件 AI 中,除了功能组件外,还有这个(简化的)。 Inside the functional component's return statement I also apply it to a JSX element (not shown).在功能组件的 return 语句中,我还将它应用于 JSX 元素(未显示)。 This works well.这很好用。

const useStyles = makeStyles((t) => ({
  content: {
    minHeight: '100vh',
  },
}));

In file B, I would like to override the CSS class content based on isDesktop .在文件 B 中,我想覆盖基于isDesktop的 CSS 类content Is this possible/desirable?这可能/可取吗? Something like this, but it does not work:像这样的东西,但它不起作用:

const useStyles = makeStyles({
content: {
    minHeight: (props) => (props.isDesktop ? '100vh' : '112vh'),
  },
});

//And inside my functional component:
const isDesktop = useMediaQuery(Theme.breakpoints.up('sm'));
const classes = useStyles({ isDesktop });

Note that the intent is to not render the JSX component in file B, only to override the CSS class content in file B is desirable.请注意,目的是呈现文件 B 中的 JSX 组件,只需要覆盖文件 B 中的 CSS 类content ( classes is unused in my sample.) (我的示例中未使用classes 。)

This can be done with few hooks.Let say my functional componet name is "Mycomponent".my material component is "materialcomponent".we need to import useWindowSize hook.That helps us to get the window size.so that we can check our screen size is desktop or mobile.in the makestyle we have to create two classes for desktop and mobile minHeight.with the simple if else checking we can pass this two classes conditionally to materialcomponent className prop.Below is the code.这可以通过几个钩子来完成。假设我的功能组件名称是“Mycomponent”。我的材料组件是“materialcomponent”。我们需要导入 useWindowSize 钩子。这有助于我们获取窗口大小。这样我们就可以检查我们的屏幕size 是桌面或移动。在 makestyle 中,我们必须为桌面和移动 minHeight 创建两个类。通过简单的 if else 检查,我们可以有条件地将这两个类传递给 materialcomponent className prop。下面是代码。

1.create two classes with the makestyles 1.用makestyles创建两个类

const useStyles = makeStyles((t) => ({
  contentDesktop: {
    minHeight: '100vh',
  },
contentMobile:{
 minHeight: '110vh',
}

}));

2.import the useWindowSize hook 2.导入useWindowSize钩子

import useWindowSize from "hooks/useWindowSize";

3.functinal componet code. 3.functinal组件代码。

const Mycomponent = () => {

  const classes = useStyles();
  let myclass="";
  const width = useWindowSize();
  const isDesktop = width >=1024;
  const mobile= width <= 600;

if(isDesktop){

myclass=classes. contentDesktop;

}
if(mobile){
myclass=classes.contentMobile;
}


return (
 
<materialcomponent className={`${myclass}`}

);


}

You can export this function outside of your functional component您可以将此功能导出到功能组件之外

export const getStyles = (isDesktop) => {
  return {
    content: {
      minHeight: isDesktop ? "100vh" : "112vh",
    },
  };
};

And then wherever you want your styling applied然后在任何你想应用你的样式的地方

const isDesktop = useMediaQuery(Theme.breakpoints.up('sm'));

...
  <SomeMuiComponent sx={getStyles(isDekstop)} />

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

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