【问题标题】:How to use material-ui (alpha) with styeld-components properly?如何正确使用带有样式组件的material-ui(alpha)?
【发布时间】:2017-12-13 14:33:48
【问题描述】:

我一直在尝试将 styled-components 与 alpha 版本的 ma​​terial-ui

一起使用

根据documentation,这应该是开箱即用的。

这段代码:

const StyledButton = styled(Button)`
   color: red;
   text-transform: uppercase;
`;

return <StyledButton>Button</StyledButton>;

会生成这样的东西:

<button tabindex="0" class="MuiButtonBase-root-3177716317 sc-bdVaJa sxRGN" type="button" role="button">
...
</button>

看起来不错。

但是,我遇到的唯一问题是注入的 CSS 样式的顺序 (pic)。来自 styled-components 的样式会在 MUI 的样式之前注入,这会降低它们的优先级。

有什么方法可以在不使用!important 的情况下解决这个问题?

【问题讨论】:

    标签: reactjs material-ui styled-components


    【解决方案1】:

    现在我们可以使用&lt;!-- material-ui --&gt; 来确保在它之后注入样式。

    默认情况下,Material-UI 会寻找一个命名为注入样式的 html 注释。通过调整此注释在 HTML 正文中的位置,您可以控制 CSS 规则应用于组件的顺序。 (ref)

    【讨论】:

      【解决方案2】:

      styled-components 通常与任何组件库兼容。当您编写styled(AnotherComponent) 时,我们会使用该组件并注入一个自动生成的类名。这意味着本质上它与写&lt;AnotherComponent className="sc-asdf123" /&gt;!

      material-ui 的当前版本特别难以自定义样式,因为它使用内联样式。来自MaterialUI documentation:

      请注意,内联定义的 CSS 属性优先于 CSS 类中定义的属性。您需要使用 !important 优先于内联样式。

      这意味着简单地使用styled(MaterialButton) 将不起作用,因为传入的样式大多会被忽略。您需要提高样式的特异性以覆盖 material-ui 附带的内联样式。 (如果您不熟悉细节,this article 是一本很好的特异性入门书)


      material-uialpha 版本的答案

      material-ui 的当前 alpha 版本已切换到在后台使用 JSS。 (即CSS in JS not inline styles,如styled-components)这意味着问题很可能是styled-components 样式是在默认material-ui 样式之后注入的。 (由JSS注入)

      JSS 支持 custom injection points,因此您可以在 HTML 的 HEAD 中添加 &lt;!-- jss --&gt; 注释,以确保 JSS 在 styled-components 注入的 CSS 之前注入其 CSS?


      material-ui当前版本的答案

      有两种方法可以提高styled-components 注入样式的特异性,一种更乏味,另一种更“hacky”。第一个是在所有样式的末尾添加!important:

      const Button = styled(MaterialButton)`
        color: blue!important;
      `
      

      虽然这在大多数情况下都有效,但当您在组件中有大量自定义样式时,它会很快变得乏味。更好的方法是使用类名hack:

      const Button = styled(MaterialButton)`
        &&& {
          color: blue;
        }
      
      `
      

      这些 & 符号被自动生成的类名替换,这意味着输出的 CSS 看起来像这样:

      .sc-asdf123.sc-asdf123.sc-asdf123 {
        color: blue;
      }
      

      这大大提高了特异性,从而覆盖了定义的内联样式,并且比必须在每个规则后面加上 !important 更不烦人。

      【讨论】:

      • 谢谢@mxstbr。我认为我的问题标题有点误导,因为它是使用 JSS 的 material-ui 的 alpha 版本。问题可能是“我们可以从material-ui 或styled-components 更改 中注入样式的顺序吗?”
      • 我已经用 alpha 版本的可能解决方案更新了答案,这行得通吗?
      【解决方案3】:

      在当前发布(即非 alpha)版本中,您所问的确实需要!important 基础:

      “请注意,内联定义的 CSS 属性优先于 CSS 类中定义的属性。您需要使用 !important 优先于内联样式。”

      参考:http://www.material-ui.com/#/customization/styles

      也许 Alpha 版还没有完全摆脱这种内联要求,或者它仍在进行中。

      当我需要这样的解决方案时,我自己为克服这种事情所做的就是(不幸地)在标准 &lt;button&gt; 元素上重新创建整个 CSS。这是我如何使用react-photonkit“主题”的示例

      // @flow
      import styled from 'styled-components';
      
      const PhotonStyledButton = styled.button`
        font-family: Arial, Roboto, Helvetica, sans-serif;
        height: 30px;
        display: inline-block;
        padding: 6px 12px;
        margin-bottom: 0;
        font-size: 12px !important;
        line-height: 1.4;
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        cursor: default;
        background-image: none;
        border: 1px solid transparent;
        border-radius: $default-border-radius;
        box-shadow: 0 1px 1px rgba(0,0,0,.06);
        -webkit-app-region: no-drag;
      
        &:focus {
          outline: none;
          box-shadow: none;
        }
      
        color: #333;
        border-top-color: #c2c0c2;
        border-right-color: #c2c0c2;
        border-bottom-color: #a19fa1;
        border-left-color: #c2c0c2;
        background-color: #fcfcfc;
        background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#fcfcfc), color-stop(100%,#f1f1f1));
        background-image: -webkit-linear-gradient(top, #fcfcfc 0%, #f1f1f1 100%);
        background-image: linear-gradient(to bottom, #fcfcfc 0%, #f1f1f1 100%);
      
        &:active {
          background-color: #ddd;
          background-image: none;
        }
      `;
      
      export default PhotonStyledButton;
      

      【讨论】:

        猜你喜欢
        • 2022-08-03
        • 2020-05-08
        • 2022-01-25
        • 1970-01-01
        • 2021-10-05
        • 2021-09-15
        • 2022-08-10
        • 2021-10-10
        • 2019-04-29
        相关资源
        最近更新 更多