简体   繁体   English

如何在 React + Material-UI 项目中使用 CSS?

[英]How to use CSS inside a React + Material-UI project?

I'm working on a project written in React (using TypeScript) with the Material-UI library.我正在使用 Material-UI 库用 React(使用 TypeScript)编写一个项目。

I'd like to use an animated submit button, replacing the default button of the library.我想使用动画提交按钮,替换库的默认按钮。 To do so, I adapted this button , which I found here , to work inside React: I put the CSS file inside a "styles" folder and then I imported it in the.tsx file of my Button.为此,我调整了我在此处找到的这个按钮,以便在 React 中工作:我将 CSS 文件放在“styles”文件夹中,然后将其导入到按钮的 .tsx 文件中。 I adapted the HTML code to JSX so my render method looks like that:我将 HTML 代码改编为 JSX,所以我的渲染方法如下所示:

return(
    <button className={`submit-button ${btnState}`} onClick={handleClickOpen}>
        <span className="pre-state-msg">Submit</span>
        <span className="current-state-msg hide">Sending...</span>
        <span className="done-state-msg hide">Done!</span>
    </button>
);

The button works just fine, problem is importing the CSS breaks other things in other parts of the application (from what I saw, it breaks the Container component whenever the container doesn't render a component which includes the custom button).该按钮工作得很好,问题是导入 CSS 会破坏应用程序其他部分的其他内容(据我所知,只要容器不呈现包含自定义按钮的组件,它就会破坏容器组件)。 Now, I guess it has something to do with how Material-UI works, but I don't really know how to solve this problem.现在,我想这与 Material-UI 的工作原理有关,但我真的不知道如何解决这个问题。

I know Material-UI suggests to use CSS-in-JS through useStyles hook, withStyles or similar things.我知道 Material-UI 建议通过 useStyles hook、withStyles 或类似的东西来使用 CSS-in-JS。 The fact is I don't know how to port that CSS file in order to use it like that, I don't even know if this solution supports every CSS feature, such as element>element selector, classList.add, etc.事实是我不知道如何移植 CSS 文件以便像这样使用它,我什至不知道这个解决方案是否支持每个 CSS 功能,例如元素>元素选择器、classList.add 等。

So, in order to solve this problem, is there a way to use the CSS file without breaking anything?那么,为了解决这个问题,有没有办法在不破坏任何东西的情况下使用 CSS 文件? This doc suggests its possible but I had no luck trying. 该文档表明它是可能的,但我没有运气尝试。 If it's not possible to use pure CSS, is it possible to convert that code to CSS-in-JS, in a way I can either programmatically modify the className of an element or use the classList.add method?如果无法使用纯 CSS,是否可以将该代码转换为 CSS-in-JS,以某种方式我可以以编程方式修改元素的 className 或使用 classList.add 方法?

Thanks in advance to anyone who'll spend some time to read this question and try to answer.提前感谢任何愿意花一些时间阅读这个问题并尝试回答的人。

It is definitely possible to turn that CSS into JSS which MUI uses under the hood.绝对有可能将 CSS 转换为 MUI 在后台使用的JSS It's not that hard if you toy around in the JSS playground and see the generated CSS.如果您在JSS 操场上玩耍并查看生成的 CSS,这并不难。 For instance:例如:

keyframes short_press {
  to: { transform: rotate(360deg); }
}

.submit-button {
  display: block;
}

.submit-button:hover, .submit-button:focus {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.submit-button > span {
  display: block;
}

.submit-button.animated {

}

Will be:将会:

import { makeStyles } from '@material-ui/styles';

let useStyle = makeStyles({
  '@keyframes short_press': {
    to: { transform: 'rotate(360deg)' },
  },
  button: {
    display: 'block',
    '&:hover, &:focus': {
       boxShadow: '0 5px 15px rgba(0, 0, 0, 0.1)',
    },
    '& > span': {
       display: 'block',
    },
    '&.animated': {
      animation: '$short_press 1s infinite linear',
    }
  },});

function YourButton() {
  let css = useStyle();
  return (
    <button className={`${css.button} ${animated ? 'animated' : ''}`}>
      Click
    </button>
   );
}


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

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