我正在使用Material-UI,并且有一个组件是它们的ExpansionPanel。我还使用createMuiTheme创建了默认主题,我希望大多数样式都来自此。我想将扩展面板的颜色设置为在createMuiTheme中定义的灰色,但是在扩展面板中既不使用MUI className也不使用style都不能成功。这是我的面板的代码:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import ExpansionPanel from '@material-ui/core/ExpansionPanel';
import ExpansionPanelSummary from '@material-ui/core/ExpansionPanelSummary';
import ExpansionPanelDetails from '@material-ui/core/ExpansionPanelDetails';
import Typography from '@material-ui/core/Typography';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';

const styles = theme => ({
  root: {
    width: '100%',
  },
  heading: {
    fontSize: theme.typography.pxToRem(15),
    fontWeight: theme.typography.fontWeightRegular,
  },
  backgroundColor: theme.palette.secondary.grey
});

function SimpleExpansionPanel(props) {
  const { classes } = props;
  console.log(`theme.palette.secondary.grey=${classes.backgroundColor}`);
  return (
    <div className={classes.root}>
      <ExpansionPanel className={classes.backgroundColor}
                      style={{ backgroundColor: classes.backgroundColor }}>
        <ExpansionPanelSummary expandIcon={<ExpandMoreIcon />}>
          <Typography className={classes.heading}>More Info</Typography>
        </ExpansionPanelSummary>
        <ExpansionPanelDetails>
          <Typography>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex,
            sit amet blandit leo lobortis eget.
          </Typography>
        </ExpansionPanelDetails>
      </ExpansionPanel>
    </div>
  );
}

SimpleExpansionPanel.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles, { withTheme: true })(SimpleExpansionPanel);


具体来说,行console.log( theme.palette.secondary.grey = $ {classes.backgroundColor} );打印:SimpleExpansionPanel-backgroundColor-332
这是我的createMuiTheme的代码:

import { createMuiTheme } from '@material-ui/core/styles';
import blue from '@material-ui/core/colors/blue';

const MaterialBlueTheme = createMuiTheme({
  palette: {
    primary: blue,
    secondary: {
      main: '#f50057',
      grey: '#BDBDBD'
    }
  },
  typography: {
    useNextVariants: true,
  }
});

export default MaterialBlueTheme;


最后,我使用MUI上下文来提供主题:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import MuiThemeProvider from '@material-ui/core/styles/MuiThemeProvider';
import App from './App';
import MaterialBlueTheme from './components/layout/MaterialBlueTheme.js';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<MuiThemeProvider theme={MaterialBlueTheme}>
  <App />
</MuiThemeProvider>, document.getElementById('root'));
serviceWorker.unregister();


如何真正使用createMuiTheme中我想要的颜色?

最佳答案

您的styles对象中的每个键都是一个CSS类名。每个值都必须是有效的CSS属性对象。因此:

const styles = theme => ({
  root: {
    width: '100%',
  },
  heading: {
    fontSize: theme.typography.pxToRem(15),
    fontWeight: theme.typography.fontWeightRegular,
  },
  // should use a better name for this class i.e. use a semantic name
  backgroundColor: {
    backgroundColor: theme.palette.secondary.grey,
  }
});

关于javascript - 如何从Material-UI`ExpansionPanel`中的`createMuiTheme`中提取属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53352361/

10-09 22:04