【问题标题】:How can I change Material UI Select or Menu MenuItem hover style using theme如何使用主题更改 Material UI Select 或 Menu MenuItem 悬停样式
【发布时间】:2018-08-28 21:22:57
【问题描述】:

我创建了自己的主题 从 'material-ui/styles' 导入 { createMuiTheme };

export const MyTheme = createMuiTheme({
    palette: {
        primary: {
            light: '#757ce8',
            main: '#3f50b5',
            dark: '#002884',
            contrastText: '#fff',
        },
        secondary: {
            light: '#ff7961',
            main: '#f44336',
            dark: '#ba000d',
            contrastText: '#000',
        },
        error: {
            light: '#FF5F57',
            main: '#CE160C',
            dark: '#380300',
            //contrastText: will be calculated to contast with palette.primary.main
        }
    }
});

在我的应用中使用它

<MuiThemeProvider theme={MyTheme}>
    <AppContainer>
        <BrowserRouter children={ routes } basename={ baseUrl } />
    </AppContainer>
</MuiThemeProvider>

但是如何使用主题更改MenuSelect 中的MenuItem 悬停样式?

【问题讨论】:

    标签: css reactjs typescript material-ui


    【解决方案1】:

    使用主题实现悬停和选定项目样式

    export const MyTheme = createMuiTheme({
        palette: {
            action: {
                selected: '#E7A615',
                hover: '#FFD371',
                disabled: '#9B9B9B'
            }
        }
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用简单的 CSS 技巧来更改 MenuItem 悬停时的颜色

      这里是示例代码

      import React from 'react';
      import './testJS.css'
      import Menu from 'material-ui/Menu';
      import MenuItem from 'material-ui/MenuItem';
      class TestJS extends React.Component {
          constructor(props) {
          super(props);
      }
      render() {
          return(
              <div id="root">
                  <Menu>
                      <MenuItem primaryText="Maps" className="menu-item" />
                      <MenuItem primaryText="Books" className="menu-item" />
                      <MenuItem primaryText="Flights" className="menu-item" />
                      <MenuItem primaryText="Apps" className="menu-item" />
                  </Menu>
              </div>
          )};
      }
      export default TestJS;
      

      并在您的 CSS 文件中添加以下行,需要使用 !important 以覆盖默认的材质-UI CSS

      .menu-item{
         background: #dcdcdc !important;
      }
      .menu-item:hover{
         background: #0e7be9 !important;
      }
      

      【讨论】:

      • 是的,我知道。但是我特意问了能不能用一个主题来实现。
      • 可能是这样的,你可以试试菜单项stackoverflow.com/questions/46173259/…
      • !important 绝不应该成为解决方案的一部分,因为它与所涉及的技术相悖。是的,mui 可能很难使用,但是 !important 是不对的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      相关资源
      最近更新 更多