简体   繁体   English

更改抽屉中的背景颜色 - MUI

[英]changing background color in drawer - MUI

i cannot change backgroundColor in Drawer component.我无法在 Drawer 组件中更改 backgroundColor。 I do as I found in other refs and it is not working for me.我按照我在其他参考文献中发现的那样做,但它对我不起作用。 I created two classes我创建了两个类

import { makeStyles } from '@mui/styles'
import { Drawer, Typography } from '@mui/material';


const drawerWidth = 240;
const useStyles = makeStyles({
    drawer: {
        width: drawerWidth,    
      },
      drawerPaper: {
        width: drawerWidth,
        backgroundColor: "#fcba03" 
      },
  });
export default function Sidebar() {
  const classes=useStyles()
    return (   
           <>
            <Drawer
                className={classes.drawer}
                variant="permanent"
                anchor="left"
                 classes={{paper: classes.drawerPaper},
                 {root:classes.drawerRoot}}               
            >
                <div>
                    <Typography variant='h5'> Home </Typography>
                </div>

            </Drawer>
               
            </>
    )
}

thanks for answer!感谢您的回答!

You're not passing the classes in correct way.你没有以正确的方式通过课程。 The classes prop expects an object with keys containing the class names from makeStyle hook. classes道具需要一个带有包含来自makeStyle钩子的类名的keysobject The correct syntax should be like正确的语法应该是

classes={{ paper: classes.drawerPaper, root: classes.drawerRoot }}

The updated code could be更新后的代码可能是

import { makeStyles } from "@mui/styles";
import { Drawer, Typography } from "@mui/material";

const drawerWidth = 240;
const useStyles = makeStyles({
  drawer: {
    width: drawerWidth
  },
  drawerPaper: {
    "&&": {
      width: drawerWidth,
      backgroundColor: "#fcba03"
    }
  }
});

export default function Sidebar() {
  const classes = useStyles();
  return (
    <>
      <Drawer
        className={classes.drawer}
        variant="permanent"
        anchor="left"
        classes={{ paper: classes.drawerPaper, root: classes.drawerRoot }}
      >
        <div>
          <Typography variant="h5"> Home </Typography>
        </div>
      </Drawer>
    </>
  );
}

Note: && is added to increase the specificity of the applied styles to the paper.注意:添加&&是为了增加应用样式到论文的specificity

编辑材料演示(分叉)

Also, the support of makeStyles is going to be a legacy solution at sometime in future.此外,对makeStyles的支持将在未来某个时候成为遗留解决方案。 If possible, you should probably look at the newer styling option provided in the mui v5.如果可能,您应该查看 mui v5 中提供的更新样式选项

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

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