我是 Material-UI 的新手,对 React 和 Web 开发也很陌生。我试图了解如何在 MUI 框架中使用 Theme、createStyles 和 JSS。
我直接从此处的文档中获取了响应式组件的代码。使用文件顶部的样式,组件按预期工作。但是,当我尝试以图标为目标来更改颜色时,没有任何东西可以渲染我选择的颜色。
这是单独文件中的 JSS: 此文件称为:.src/shared/components/AppFrame/styles.js
import { createStyles } from '@material-ui/core';
const drawerWidth = 240;
export const styles = (theme) => createStyles({
root: {
display: 'flex',
},
appBar: {
zIndex: theme.zIndex.drawer + 1,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
},
appBarShift: {
marginLeft: drawerWidth,
width: `calc(100% - ${drawerWidth}px)`,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
},
menuButton: {
marginLeft: 12,
marginRight: 36,
color: theme.palette.common.white,
},
hide: {
display: 'none',
},
drawer: {
width: drawerWidth,
flexShrink: 0,
whiteSpace: 'nowrap',
color: 'white',
},
drawerOpen: {
paddingTop: 64,
backgroundColor: theme.palette.primary.dark,
color: theme.palette.common.white,
width: drawerWidth,
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
},
drawerClose: {
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
paddingBottom: 64,
backgroundColor: theme.palette.primary.dark,
color: theme.palette.common.white,
overflowX: 'hidden',
width: theme.spacing.unit * 7 + 1,
[theme.breakpoints.up('sm')]: {
width: theme.spacing.unit * 9 + 1,
},
},
topToolbar: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'flex-end',
padding: '0 8px',
margin: '24px',
...theme.mixins.toolbar,
},
toolbar: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'flex-end',
padding: '0 8px',
...theme.mixins.toolbar,
},
content: {
flexGrow: 1,
padding: theme.spacing.unit * 3,
},
listItem: {
color: theme.palette.common.white,
},
icon: {
color: theme.palette.common.white,
}
});
组件文件如下所示: .src/shared/components/AppFrame/index.js
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { withStyles } from '@material-ui/core/styles';
import {
Drawer, AppBar, Toolbar,
List, Typography, Divider, IconButton,
ListItemText, ListItemIcon, ListItem,
CssBaseline
} from '@material-ui/core';
import {
Menu as MenuIcon,
ChevronLeft as ChevronLeftIcon,
ChevronRight as ChevronRightIcon,
Inbox as InboxIcon
} from '@material-ui/icons';
import {
Group as GroupIcon,
Apps as AppsIcon
} from '@material-ui/icons'
import { styles } from './styles'
class MiniDrawer extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
};
}
handleDrawerOpen = () => {
this.setState({ open: true });
};
handleDrawerClose = () => {
this.setState({ open: false });
};
render() {
const { classes, theme } = this.props;
return (
<div className={classes.root}>
<CssBaseline />
<AppBar
position="fixed"
className={classNames(classes.appBar, {
[classes.appBarShift]: this.state.open,
})}
>
<Toolbar disableGutters={!this.state.open}>
<IconButton
color="inherit"
aria-label="Open drawer"
onClick={this.handleDrawerOpen}
className={ classNames(classes.menuButton, {
[classes.hide]: this.state.open,
}, classes.icon )}
>
<MenuIcon className={classes.icon} />
</IconButton>
<Typography variant="h6" color="inherit" noWrap>
Evolv
</Typography>
</Toolbar>
</AppBar>
<Drawer
variant="permanent"
className={classNames(classes.drawer, {
[classes.drawerOpen]: this.state.open,
[classes.drawerClose]: !this.state.open,
})}
classes={{
paper: classNames({
[classes.drawerOpen]: this.state.open,
[classes.drawerClose]: !this.state.open,
}),
}}
open={this.state.open}
>
<div className={classes.toolbar}>
<IconButton onClick={this.handleDrawerClose} classes={classes.icon}>
{theme.direction === 'rtl' ? <ChevronRightIcon /> : <ChevronLeftIcon />}
</IconButton>
</div>
<Divider />
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <GroupIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <GroupIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</Drawer>
<main className={classes.content}>
<div className={classes.toolbar} />
<Typography paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Rhoncus dolor purus non enim praesent elementum facilisis leo vel.
</Typography>
<Typography paragraph>
Consequat mauris nunc congue nisi vitae suscipit. Fringilla est ullamcorper eget nulla facilisi etiam dignissim diam. Pulvinar elementum integer enim neque volutpat ac tincidunt. Ornare suspendisse sed nisi lacus sed viverra tellus. Purus sit amet volutpat
consequat mauris.
</Typography>
</main>
</div>
);
}
}
MiniDrawer.propTypes = {
classes: PropTypes.object.isRequired,
theme: PropTypes.object.isRequired,
};
export default withStyles(styles, { withTheme: true })(MiniDrawer);
问题是我试图让 sideDrawer 中的图标和文本显示为白色。非常感谢任何提示、建议或资源以更好地了解样式如何与 MUI 一起使用。