如何使用类和CSS模块覆盖样式? [英] How to overwrite styles with classes and css modules?

查看:67
本文介绍了如何使用类和CSS模块覆盖样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

集中检查的类似乎无法正常工作.我需要始终使用!important或使用material-ui提供的jss样式.例如:


评论中的后续问题:

您能解释一下为什么我不能使用课程吗? classes = {{checked:styles.AnyClass}} 是否应该成功覆盖样式?我知道为什么不这样做,但是类的目的是什么?

您可以使用 classes 道具,但是在这种情况下,没有任何令人信服的理由这样做.只要样式的声明相同(使用两个CSS类),那么下面的内容也将起作用,从而使您具有适当的特异性:

 <复选框类= {{已选中:styles.checkboxtest}}/> 

就您问题的第二部分(类的目的是什么?")而言,使用 classes 道具的主要原因有两个. classes 道具的一些CSS类将应用于组件内的不同元素,而某些CSS类仅在组件处于特定状态或组件上已使用某些道具时才应用.在这种情况下,您尝试将这些样式定位为组件的特定状态.在Material-UI的第3版中,您需要使用 classes 道具来提供仅应用于 checked 状态的类名,但在第4版的Material-UI中,UI切换为对这些状态使用全局类名称(此处有更多详细信息: https://material-ui.com/customization/components/#pseudo-classes ),例如 Mui-checked .这使得通过单个生成的类更容易定位这些状态(此更改的一个主要驱动因素是使使用 styled-components 定制Material-UI组件更加容易).

Focused, checked classes seems to not work properly. I need to always use !important or use jss styles provided by material-ui. For example:

https://codesandbox.io/s/css-modules-nvy8s?file=/src/CssModulesButton.js

CssModulesButton.module.css

.checkboxtest {
  color: blue;
}

.checkboxtestworking {
  color: blue !important;
}

CssModulesButton.js

import React from "react";
// webpack, parcel or else will inject the CSS into the page
import styles from "./CssModulesButton.module.css";
import Checkbox from "@material-ui/core/Checkbox/Checkbox";

export default function CssModulesButton() {
  return (
    <div>
      <Checkbox classes={{ checked: styles.checkboxtest }}>testasd</Checkbox>
      <Checkbox classes={{ checked: styles.checkboxtestworking }}>
        testasd
      </Checkbox>
    </div>
  );
}

index.js

import React from "react";
import { render } from "react-dom";
import { StylesProvider } from "@material-ui/core/styles";
import CssModulesButton from "./CssModulesButton";
import CssBaseline from "@material-ui/core/CssBaseline";

const App = () => <CssModulesButton />;

render(<App />, document.querySelector("#root"));

I cant overwrite Checkbox color in any other way than using !important. The problem persists when using classes={{focused:styles.focused}} and many others.

Is there a way to simply overwrite components with classes without !important? Sometimes even !important doesnt work

inb4 im using injectFirst

解决方案

You mentioned injectFirst in your question, though your sandbox example wasn't using it. You should use it since this will make sure that the CSS from your CSS modules is inserted after the Material-UI CSS in the <head> element. When specificity is the same, the styles that occur later will win over earlier styles.

The default styles for the checkbox colors are as follows:

  colorSecondary: {
    '&$checked': {
      color: theme.palette.secondary.main,
      '&:hover': {
        backgroundColor: fade(theme.palette.secondary.main, theme.palette.action.hoverOpacity),
        // Reset on touch devices, it doesn't add specificity
        '@media (hover: none)': {
          backgroundColor: 'transparent',
        },
      },
    },
    '&$disabled': {
      color: theme.palette.action.disabled,
    },
  },

Below is the corresponding CSS generated by JSS:

.MuiCheckbox-colorSecondary.Mui-checked {
  color: #f50057;
}
.MuiCheckbox-colorSecondary.Mui-disabled {
  color: rgba(0, 0, 0, 0.26);
}
.MuiCheckbox-colorSecondary.Mui-checked:hover {
  background-color: rgba(245, 0, 87, 0.04);
}
@media (hover: none) {
  .MuiCheckbox-colorSecondary.Mui-checked:hover {
    background-color: transparent;
  }
}

The important aspect to notice is that the checked style is done via two classes. In order to override these styles you need that same degree of specificity or greater.

Below is a full working example overriding the checked color of the checkbox:

index.js (important part here is <StylesProvider injectFirst>)

import React from "react";
import { render } from "react-dom";
import { StylesProvider } from "@material-ui/core/styles";
import CssModulesButton from "./CssModulesButton";

const App = () => (
  <StylesProvider injectFirst>
    <CssModulesButton />
  </StylesProvider>
);

render(<App />, document.querySelector("#root"));

CssModulesButton.module.css

  • Uses two classes in the style declaration -- the checkboxtest CSS module class and Mui-checked (the global class which Material-UI adds for the checked state)

.checkboxtest:global(.Mui-checked) {
  color: blue;
}

CssModulesButton.js

import React from "react";
import styles from "./CssModulesButton.module.css";
import Checkbox from "@material-ui/core/Checkbox";

export default function CssModulesButton() {
  return (
    <div>
      <Checkbox className={styles.checkboxtest} />
    </div>
  );
}


Follow-up question from the comments:

Could you explain why can't I use classes? Shouldn't classes={{checked:styles.AnyClass}} overwrite the styles successfully? I understand why it doesn't, but what is the purpose of classes then?

You can use the classes prop, but there isn't any compelling reason to do so in this case. The following will also work, so long as the declaration for the styles is the same (using two CSS classes) so that you have the appropriate degree of specificity:

<Checkbox classes={{ checked: styles.checkboxtest }} />

As far as the second part of your question ("what is the purpose of classes then?"), there are two main reasons to use the classes prop. Some of the CSS classes for the classes prop get applied to different elements within the component, and some only get applied when the component is in a particular state, or when certain props have been used on the component. In this case, you are trying to target these styles for a particular state of the component. In v3 of Material-UI, you would have needed to use the classes prop to provide a class name that would only be applied for the checked state, but in v4 Material-UI switched to using global class names for these states (more details here: https://material-ui.com/customization/components/#pseudo-classes) such as Mui-checked. This makes it easier to target these states via a single generated class (a big driver of this change was to make it easier to customize Material-UI components using styled-components).

这篇关于如何使用类和CSS模块覆盖样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆