css - 样式 Material UI 组件

标签 css reactjs typescript material-ui css-modules

不是真正的问题,但我不满意。我正在使用 react + typescript + css modules + https://material-ui-next.com/ .问题是,当我需要设置 Material ui 组件的样式时,我必须经常使用 !important。问题是是否有一种方法可以在没有 important 的情况下创建样式。我创建了一个示例元素来重现问题 https://github.com/halkar/test-css-modules

最佳答案

material-ui 公开了许多用于样式化的组件。有两种方法可以做到这一点。

全局应用样式

您可以全局设置组件样式并将其应用于主题。这方面的一个例子是这样的(从文档 http://www.material-ui.com/#/customization/themes 复制):

import React from 'react';
import {cyan500} from 'material-ui/styles/colors';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import AppBar from 'material-ui/AppBar';

// This replaces the textColor value on the palette
// and then update the keys for each component that depends on it.
// More on Colors: http://www.material-ui.com/#/customization/colors
const muiTheme = getMuiTheme({
  palette: {
    textColor: cyan500,
  },
  appBar: {
    height: 50,
  },
});

class Main extends React.Component {
  render() {
    // MuiThemeProvider takes the theme as a property and passed it down the hierarchy
    // using React's context feature.
    return (
      <MuiThemeProvider muiTheme={muiTheme}>
        <AppBar title="My AppBar" />
      </MuiThemeProvider>
    );
  }
}

export default Main;

正如您在这里看到的,appBar 组件的高度为 50px,这意味着每次您在应用 muiTheme 的树下向您的应用程序添加一个 appbar 组件时,它都会给它一个高度为 50px。这是您可以为每个组件应用的所有样式的列表 https://github.com/callemall/material-ui/blob/master/src/styles/getMuiTheme.js .

使用样式属性应用样式

要将样式应用于单个组件,您通常可以使用样式属性并将所需的样式传递给它。

这是文档中的另一个示例,其中 12 像素的边距应用于 RaisedButton。

import React from 'react';
import RaisedButton from 'material-ui/RaisedButton';

const style = {
  margin: 12,
};

const RaisedButtonExampleSimple = () => (
  <div>
    <RaisedButton label="Default" style={style} />
    <RaisedButton label="Primary" primary={true} style={style} />
    <RaisedButton label="Secondary" secondary={true} style={style} />
    <RaisedButton label="Disabled" disabled={true} style={style} />
    <br />
    <br />
    <RaisedButton label="Full width" fullWidth={true} />
  </div>
);

export default RaisedButtonExampleSimple;

现在,样式在同一个文件中定义,但您可以在单独的文件中定义它们并将它们导入到您使用组件的文件中。

如果您想应用多种样式,那么您可以像这样使用扩展运算符:style={{...style1,...style2}}

通常,您使用 style 属性为组件(根元素)中的特定事物设置样式,但某些组件有多个属性来为组件的不同元素设置样式。本页属性下http://www.material-ui.com/#/components/raised-button ,你可以看到有 style 属性,labelStyle 和 rippleStyle 来设置 RaisedButton 不同部分的样式。

检查您正在使用的组件下的属性,看看您可以使用哪个样式属性,否则检查您可以覆盖的可用全局样式属性。希望这对您有所帮助!

关于css - 样式 Material UI 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50074136/

相关文章:

php - 根据变量创建多个div

javascript - 在 Angular 2+ 中进行 DOM 操作的正确方法

javascript - 为什么在 JavaScript 中使用 TypeScript 类需要 `.default` ?

javascript - 返回子数组的父属性匹配条件

asp.net - 使用 Visual Studio 2015 在 gulp 中检测发布/调试

javascript - 在 jquery 不起作用的 html 表中对列进行排序和分页

html - Foundation - 对齐嵌套的 div

html - 电子邮件中的形状 - 一个好的做法?

javascript - reactjs 通过 refs 在组件之间进行通信

unit-testing - ReactJS:未经测试文件的 Jest 覆盖率报告显示覆盖率 0%