材料中基于类的样式 - ui

时间:2017-08-02 07:28:03

标签: javascript html css reactjs material-ui

我是关于material-ui的新手,在我第一次测试之后(材料v0.18.7和反应v15.6.1),我发现它为每个组件使用内联样式。 我测试了放置相同的组件10次,没有基于类的样式(使用CSS),它只是重复内联样式。 在真正的网页中,可能有很多组件具有相同的样式,有没有更好的方法来停止这种冗余样式并添加一些css或类似的东西,这可以减少传递给用户的数据的大小每页加载?

我的意思是任何可以帮助我减少内联样式的数量的东西,可以用另一种方式处理它们

这是我的示例代码

<div>
    <Paper style={style} zDepth={1} ><FlatButton label="exit" /></Paper>
    <Paper style={style} zDepth={1} ><FlatButton label="exit" /></Paper>
    <Paper style={style} zDepth={1} ><FlatButton label="exit" /></Paper>
</div>

我用react渲染这个简单的组件,输出的html文件有三次以下div

<div style="color: rgba(0, 0, 0, 0.87); background-color: rgb(255, 255, 255); transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; box-sizing: border-box; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px; border-radius: 2px; height: 100px; width: 100px; margin: 20px; text-align: center; display: inline-block;">
    <button tabindex="0" type="button" style="border: 10px; box-sizing: border-box; display: inline-block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: inherit; font-weight: inherit; position: relative; z-index: 1; height: 36px; line-height: 36px; min-width: 88px; color: rgba(0, 0, 0, 0.87); transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; border-radius: 2px; user-select: none; overflow: hidden; background-color: rgba(0, 0, 0, 0); text-align: center;">
        <div>
            <span style="position: relative; padding-left: 16px; padding-right: 16px; vertical-align: middle; letter-spacing: 0px; text-transform: uppercase; font-weight: 500; font-size: 14px;">exit</span>
        </div>
    </button>
</div>

确切的div被添加到我的代码中三次,包含所有内联样式。

我正在寻找一种方法来减少这些内联样式的数量,并使用一些css代码更改它们。

感谢

3 个答案:

答案 0 :(得分:1)

我知道这是对此的公开讨论,而且从我所读过的内容来看,他们还没有决定实现外部CSS:https://github.com/callemall/material-ui/issues/1132

您还可以查看MUI的主题提供程序。 https://www.npmjs.com/package/react-theme-provider

答案 1 :(得分:1)

好的,我想我明白你要做什么。如果你的目的是减少材料ui为材料ui相关组件调用的内联样式的使用,你将不得不直接编辑材料ui css插件文件......但我不建议这样做。 您始终可以使用!important覆盖css样式,但不会删除material-ui传递的其他参数,它将简单地覆盖现有的参数。 在您的情况下,我只看到两个选项:您是使用ui已经为您完成的材料自己创建一种新类型的<FlatButton/>,并仅针对您的页面进行调整(但可能会占用大量时间),或者你只是使用ui提供的材料滚动并覆盖困扰你的元素。 不过要说实话,我不确定您在浏览器中看到的内联样式在页面加载时间上有相当大的减速潜力。 希望我回答你的问题! D.

答案 2 :(得分:-1)

为什么不将您希望提供相同内联样式的所有元素传递到同一个类中?然后在css中,您可以通过此类简单地定义与这些组件的样式相关的所有参数。例如:

HTML

<div class = "thing"></div>

CSS

.thing {
background-color: red;
}