【问题标题】:Material-ui : darkTheme does not affect wrapped childrenMaterial-ui : darkTheme 不影响被包裹的孩子
【发布时间】:2017-03-07 04:45:03
【问题描述】:

我的问题很简单,我想在我的应用程序的一部分中使用 Material-ui 默认的 darkTheme。这是一个代码示例:

<div>
  <MuiThemeProvider muiTheme={getMuiTheme(darkBaseTheme)}>
    <div>
      <AppBar title="I am dark" />
      <MyCustomComponent label="I should be dark but I am not" />
    </div>
  </MuiThemeProvider>
  <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
      <p>I am in the lightBaseTheme (default theme)</p>
  </MuiThemeProvider>
</div>

应用的第一部分必须是深色主题(即左侧菜单),第二部分必须是浅色主题(即应用本身)。

作为MuiThemeProvider 的直接子代的AppBar 确实是深色的,但是,MyCustomComponent 及其子代(即使它们是基础 Material-ui 组件,例如 RaisedButton)并没有使用深色主题。

MyCustomComponents 及其所有子子项也使用深色主题的最简单方法是什么?

【问题讨论】:

    标签: reactjs themes components material-ui


    【解决方案1】:

    您需要将MuiThemeProvider 中的所有内容包装到一个元素中。

    <MuiThemeProvider muiTheme={getMuiTheme(darkBaseTheme)}>
      <div>
        <AppBar title="I am dark" />
        <MyCustomComponent label="I should be dark but I am not" />
      </div>
    </MuiThemeProvider>
    

    实际上你必须有一个像

    这样的错误
    MuiThemeProvider.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
    

    当然只有MyCustomComponent 中的Material-UI 组件会有主题外观。您需要手动制作的所有其他内容:以 Jeff McCloud 展示的方式或使用如下上下文:

    function MyCustomComponent (props, context) {
        const { palette } = context.muiTheme;
        // Now you have access to theme settings. for example: palette.canvasColor
    }
    
    MyCustomComponent.contextTypes = {
        muiTheme: React.PropTypes.object.isRequired,
    };
    

    另一种风格化纯 HTML 或 React 组件的方法是将它们包装到 react-theme-provider。像这样:

    import ThemeProvider from 'react-theme-provider';
    
    <MuiThemeProvider muiTheme={getMuiTheme(darkBaseTheme)}>
      <div>
        <AppBar title="I am dark" />
        <ThemeProvider>
          <MyCustomComponent label="I should be dark and I am!" />
        </ThemeProvider>
      </div>
    </MuiThemeProvider>
    

    参考:https://github.com/callemall/material-ui/blob/master/src/styles/MuiThemeProvider.js#L7

    【讨论】:

    • 嗨,在我的实际代码中,我确实将我的组件放入了一个 div,我编辑了这个问题。关键是MyCustomComponent 内部的Material-UI 不是主题,尽管它们确实应该。也许我犯了一些错误,你能给我一个非常简单的例子吗?
    • 好的,我想我明白了,Material-UI 确实是主题,但是它们没有提供默认背景颜色。在这里,我的深色主题 MenuItem 显示为白底白字,我认为这是样式问题。
    • 是的,有时您需要设置自己的元素(如divs color/background-color),如答案所示。如果您仍然有问题最好检查您的代码,或者您可以使用this tool 快速测试不同的主题。
    • 我也将 react-theme-provider 示例添加到我的答案中
    【解决方案2】:

    您需要指定您的自定义组件是“可主题化的”。为此,您可以将组件包装在“muiThemeable”高阶组件中:

    import React from 'react';
    import muiThemeable from 'material-ui/styles/muiThemeable';
    
    class MyCustomComponent extends React.Component {
    // ... your component will now have access to "this.props.muiTheme"
    }
    
    
    export default muiThemeable()(MyCustomComponent);
    

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

    【讨论】:

    • 您好,我理解您的回答,但是我应该如何处理组件中的“this.props.muiTheme”?用 &lt;MuiThemeProvider&gt; 包装自己的 Material-ui 子级?问题不在于我的自定义组件,我知道无法自动设置样式,但是 Material-UI 孙子也没有主题。
    • 大概,您正在 MyCustomComponent 中创建某种 HTML“小部件”?假设小部件有几个 DIV、SPAN 和一个按钮 - 并且您希望它们与整体 Material-ui 外观和感觉相匹配...您可以使用 muiTheme 上的属性来匹配颜色、填充、边框样式等。 . 您可以在 DIV 的样式中使用“this.props.muiTheme.palette.accent1Color”或“this.props.muiTheme.appBar.height”或“this.props.muiTheme.spacing.iconSize”等值, SPAN 等组成您的组件。希望有帮助!
    猜你喜欢
    • 2020-02-25
    • 2023-04-04
    • 2014-02-20
    • 2019-09-25
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多