简体   繁体   English

Material-UI菜单的自定义背景色

[英]Custom background color for Material-UI menu

I'm new to material-ui. 我是Material-UI的新手。 I tried adding the menu component into my project and I was able to display it. 我尝试将菜单组件添加到我的项目中,并且能够显示它。 The menu's background was white and black text color. 菜单的背景是白色和黑色的文本颜色。 I want to change the colors to match my application. 我想更改颜色以匹配我的应用程序。

I copied the working component code from this link - http://www.material-ui.com/#/components/menu 我从此链接复制了工作组件代码-http: //www.material-ui.com/#/components/menu

I've been adding backgroundColor to different parts of my style object but no luck 我一直在向我的样式对象的不同部分添加backgroundColor,但是没有运气

import React from 'react';
import Menu from 'material-ui/lib/menus/menu';
import MenuItem from 'material-ui/lib/menus/menu-item';
import Divider from 'material-ui/lib/divider';
import FontIcon from 'material-ui/lib/font-icon';
import ContentCopy from 'material-ui/lib/svg-icons/content/content-copy';
import ContentLink from 'material-ui/lib/svg-icons/content/link';
import Delete from 'material-ui/lib/svg-icons/action/delete';
import Download from 'material-ui/lib/svg-icons/file/file-download';
import PersonAdd from 'material-ui/lib/svg-icons/social/person-add';
import RemoveRedEye from 'material-ui/lib/svg-icons/image/remove-red-eye';

const style = {
  menu: {
    marginRight: 32,
    marginBottom: 32,
    float: 'left',
    position: 'relative',
    zIndex: 0,
    width: 235,
  },
  rightIcon: {
    textAlign: 'center',
    lineHeight: '24px',
  },
  bg: {
    backgroundColor: '#364150',
    color: 'white'
  }
};

const MenuExampleIcons = () => (
  <div style={style.bg}>
    <Menu autoWidth={false} style={style.menu}>
      <MenuItem primaryText="Preview" leftIcon={<RemoveRedEye />} />
      <MenuItem primaryText="Share" leftIcon={<PersonAdd />} />
      <MenuItem primaryText="Get links" leftIcon={<ContentLink />} />
      <Divider />
      <MenuItem primaryText="Make a copy" leftIcon={<ContentCopy />} />
      <MenuItem primaryText="Download" leftIcon={<Download />} />
      <Divider />
      <MenuItem primaryText="Remove" leftIcon={<Delete />} />
    </Menu>
  </div>
);

export default MenuExampleIcons;

您还必须向每个菜单项添加一个样式对象。

<MenuItem key={item.key} value={value} style={{backgroundColor: 'red', color: 'white'}} primaryText={item.name}/>

Although this is a fairly older post, what isn't new to React and Material UI, right? 尽管这是一篇比较老的文章,但是React和Material UI并不是什么新东西,对吗? So here is my solution in case anyone comes across this. 所以这是我的解决方案,以防有人碰到。

div[role="menu"] > div > span {
    background: red !important;
    color: white !important;
}

Keep in mind, this will affect ALL of your menus. 请记住,这将影响您所有的菜单。 But that isn't necessarily a bad thing for consistency purposes. 但是,出于一致性目的,这不一定是一件坏事。 If you want to have different menus then you'll have to style each and every individual menu item in each menu. 如果要使用不同的菜单,则必须为每个菜单中的每个菜单项设置样式。 This is by far the most ridiculous thing Materieal UI has done yet. 这是迄今为止Materieal UI所做的最荒谬的事情。 So I'm hoping Material Next will have a better fix 所以我希望Material Next将有一个更好的解决方案

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

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