简体   繁体   English

Material-ui 覆盖反应情绪规则

[英]Material-ui overrides react emotion rules

In my project I use Material-ui components along with react-emotion.在我的项目中,我使用 Material-ui 组件和 react-emotion。

Let me give you an example that is problematic.让我给你举一个有问题的例子。 I have this element:我有这个元素:

<CardHeader title={stat} classes={{ root: statNumber }}/>

where哪里

const statNumber = css`padding: 0;`

this way I get to override the default padding (16px) of CardHeader with 0 that is my intention.这样我就可以用 0 覆盖 CardHeader 的默认填充(16px),这是我的意图。

In development mode everything works as expected but in production the padding: 0 rule gets overridden by the default padding 16px.在开发模式下,一切都按预期工作,但在生产中,padding: 0 规则被默认的 padding 16px 覆盖。

The reason this happens is that in development mode the styles are added in header dynamically.发生这种情况的原因是在开发模式下,样式是动态添加到标题中的。 First come the Material-UI styles and then the emotion styles.首先是 Material-UI 样式,然后是情感样式。 Like this:像这样: 开发模式

But in production the styles are laid out the other way around但是在生产中,样式是相反的

生产方式

This is the reason styles are overridden in production mode.这就是样式在生产模式下被覆盖的原因。

Material ui provides an explanation on this https://material-ui.com/styles/advanced/#css-injection-order Material ui 对此提供了解释https://material-ui.com/styles/advanced/#css-injection-order

but with the solution proposed I cannot change the order of emotion and material-ui styles are ordered.但是对于提出的解决方案,我无法改变情绪和材料 UI 样式的顺序。 I can only change material ui and move it further down我只能更改材质 ui 并将其进一步向下移动

Anyone has an idea how to fix this??任何人都知道如何解决这个问题?

The issue was resolved by changing the order of rendering the style rules.该问题已通过更改呈现样式规则的顺序解决。 I created a wrapper that needs to wrap the entire app.我创建了一个需要包装整个应用程序的包装器。

import React from 'react'
import { create } from 'jss'
import JssProvider from 'react-jss/lib/JssProvider'
import { createGenerateClassName, jssPreset } from 'material-ui/styles'

const styleNode = document.createElement('style')
styleNode.id = 'insertion-point-jss'
document.head.insertBefore(styleNode, document.head.firstChild)

// Configure JSS
const jss = create(jssPreset())
jss.options.createGenerateClassName = createGenerateClassName
jss.options.insertionPoint = document.getElementById('insertion-point-jss')

function Provider (props) {
  return <JssProvider jss={jss}>{props.children}</JssProvider>
}

export default Provider

The wrapper creates an element as the first child inside head.包装器创建一个元素作为 head 内的第一个子元素。 All material ui styles are instructed to be placed there thus they are first in order and can be overridden by emotion rules that come next.所有材料 ui 样式都被指示放置在那里,因此它们是按顺序排列的,并且可以被接下来的情感规则覆盖。

The official documentation now shows a very simple way to do it:官方文档现在展示了一种非常简单的方法:

https://material-ui.com/styles/advanced/#css-injection-orderhttps://material-ui.com/styles/advanced/#css-injection-order

By default, the style tags are injected last in the element of the page.默认情况下,样式标签最后注入到页面元素中。 They gain more specificity than any other style tags on your page eg CSS modules, styled components.它们比页面上的任何其他样式标签(例如 CSS 模块、样式组件)获得更多的特异性。

injectFirst先注入

The StylesProvider component has an injectFirst prop to inject the style tags first in the head (less priority): StylesProvider组件有一个injectFirst道具来首先在头部注入样式标签(优先级较低):

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

<StylesProvider injectFirst>
  {/* Your component tree.
      Styled components can override Material-UI's styles. */}
</StylesProvider>

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

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