Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change paper color Material-UI

I'm developing a React project using the material-ui library. I'm currently trying to add a drawer which is working fine for me. However, I'm trying to change the background color of this drawer. I've heard that the way to do this is by changing the color of the drawer's paper. I've tried adding the following tag to my CSS object:

const styles = theme => ({
    background:"BLUE"

Then I reference this object in my render function using the classNames library:

  render(){
        const { classes } = this.props;
        return(
    <div className={styles.root}>
    <CssBaseline />
    <Drawer 
    variant="permanent" 
    className={classNames(classes.drawer, {
        [classes.drawerOpen]: this.state.open,
        [classes.drawerClose]: !this.state.open
    })}
    classes = {{
        paper: classNames({
            background:classes.background,
            [classes.drawerOpen]: this.state.open,
            [classes.drawerClose]: !this.state.open
        })
    }}

However, when I run this on localhost, the paper is still a plain old white. Am I missing something about the classNames library or is a special case of the paper tag? Thanks in advance and let me know if I should supply more info than this.

like image 253
Sean Avatar asked Apr 06 '19 20:04

Sean


People also ask

How do you change the color of the paper in material UI?

To set a background color on Material UI's Paper, you simply need to apply the background-color CSS property to the root element of the Paper. Setting the styles on the root element of any Material UI component can be done in multiple ways, but the most common is to use the useStyles hook.

How do I change primary and secondary color in material UI?

If you want to use a custom color, put it in the main property of an object. MUI will use that color to calculate the dark, light and contrastText values besides the main one. dark , light : a darker and lighter versions of the main color. contrastText : the color of the text if the background color is the main color.

Why do we use paper in material UI?

In Material Design, the physical properties of paper are translated to the screen. The background of an application resembles the flat, opaque texture of a sheet of paper, and an application's behavior mimics paper's ability to be re-sized, shuffled, and bound together in multiple sheets.

Can I customize material UI?

Material UI provides several different ways to customize a component's styles. Your specific context will determine which one is ideal. From narrowest to broadest use case, here are the options: One-off customization.


1 Answers

You have a couple issues in the code shown in your question.

For your styles, you need something more like the following:

const styles = theme => ({
    drawerPaper: { background: "blue" }
});

In this case, "drawerPaper" is the key for my class name and then the object to the right contains the CSS properties for that class. When passed into withStyles, this will generate CSS like the following:

<style>
.classname-generated-for-drawerPaper-key: {
  background: blue;
}
</style>

You had a class name key of "background" with the string "BLUE" as the CSS properties which will end up with CSS like the following:

<style>
.classname-generated-for-background-key: {
  0: B;
  1: L;
  2: U;
  3: E;
}
</style>

which of course is not valid CSS and will have no effect on the paper.

The second issue is in how you specify the classes:

    classes = {{
        paper: classNames({
            background:classes.background,
            [classes.drawerOpen]: this.state.open,
            [classes.drawerClose]: !this.state.open
        })
    }}

When you pass an object to classNames, the keys of the object are the class names and the associated value controls (based on whether it is falsey or truthy) whether the class name should be included. With the syntax you used, classes.background will always be truthy which means that the class "background" (rather than the generated class name in classes.background) will be included which will have no effect since a "background" class hasn't been defined.

Instead you should have:

    classes = {{
        paper: classNames(classes.drawerPaper, {
            [classes.drawerOpen]: this.state.open,
            [classes.drawerClose]: !this.state.open
        })
    }}

which will unconditionally include classes.drawerPaper.

Here is a modified version of one of the Drawer demos, but with the background color of the drawer changed to blue: https://codesandbox.io/s/wqlwyk7p4l

like image 110
Ryan Cogswell Avatar answered Oct 18 '22 05:10

Ryan Cogswell