React Material-UI useTheme palette not behaving as expected

Viewed 792

I have some React code implementing some Material-UI components and a theme. According to this "light: will be calculated from palette.primary.main". I have provided a specific hex color for main, and it works, as does the secondary color. However, theme.palette.primary.light doesn't provide a lighter color, just provides a lighter gray color. I output the theme JSON value near the bottom of the code below and it all looks good - there's a value of rgb(73, 100, 169) which looks like a legit light value of the theme.palette.primary.main value. But long story short, the useTheme() returns a legit theme, but the second button value of theme.palette.primary.light looks like a generic-ish gray color. So any idea why the light value isn't making it to the component?

THANKS!

import {
  AppBar,
  Button,
  IconButton,
  SwipeableDrawer,
  Toolbar,
  Typography,
} from "@material-ui/core";
import Divider from "@material-ui/core/Divider";
import { createStyles, makeStyles, useTheme } from "@material-ui/core/styles";
import MenuIcon from "@material-ui/icons/Menu";
import MenuBookRoundedIcon from "@material-ui/icons/MenuBookRounded";
import PropTypes from "prop-types";
import React, { useContext, useState } from "react";
import { AppContext } from "./../helpers/AppContext";
import isTouchDevice from "./../helpers/utilities";
import DrawerItems from "./drawerItems";

const styles = (theme) => ({
  root: {
    position: "fixed",
    top: 5,
    left: 5,
  },
  drawer: {
    width: 250,
  },

  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 200,
  },
});

const iOS = process.browser && /iPad|iPhone|iPod/.test(navigator.userAgent);

const useStyles = makeStyles((theme) =>
  createStyles({
    grow: {
      flexGrow: 1,
    },
    menuButton: {
      marginRight: theme.spacing(2),
    },
    drawerItem: {
      padding: 15,
    },
    titleBar: {
      fontWeight: 600,
    },
    title: {
      display: "none",
      [theme.breakpoints.up("sm")]: {
        display: "block",
        width: "100%", // todo: this can be done better - make it fit
      },
    },
  })
);

//const Dashboard = ({ classes, align = "left" }) => {
function Dashboard() {
  const theme = useTheme();
  const classes = useStyles();

  //const context = useContext(SettingsContext);
  const { orgSetting, setOrgSetting } = useContext(AppContext);

  const [open, setOpen] = useState(false);
  return (
    <div className={classes.root}>
      <div>{isTouchDevice}</div>
      {/* Drawer */}
      <IconButton
        color="inherit"
        aria-label="Menu"
        onClick={() => setOpen(true)}
      >
        <Typography>
          <MenuIcon />
        </Typography>
      </IconButton>
      **<div>
        <Button color="primary">First Button</Button>
      </div>
      <div>
        <Button color={theme.palette.primary.light} variant="contained">
          Second Button
        </Button>
      </div>**
      <div>{JSON.stringify(theme)}</div>
    </div>
  );
}

Dashboard.propTypes = {
  classes: PropTypes.object,
};

//export default withStyles(styles)(Dashboard);
export default Dashboard;

And here's the theme object from the stringify above.

{
   ...
   "overrides":{
      "MuiCssBaseline":{
         "@global":{
            "html":{
               "WebkitFontSmoothing":"auto"
            }
         }
      },
      "MuiButton":{
         "textTransform":"lower"
      }
   },
   "palette":{
      "common":{
         "black":"#000",
         "white":"#fff"
      },
      "type":"light",
      "primary":{
         **"main":"#1c3e94",
         "light":"rgb(73, 100, 169)",**
         "dark":"rgb(19, 43, 103)",
         "contrastText":"#fff"
      },
      "secondary":{
         "main":"#ee2e24",
         "light":"rgb(241, 87, 79)",
         "dark":"rgb(166, 32, 25)",
         "contrastText":"#fff"
      },
      "error":{
         "light":"#e57373",
         "main":"#f44336",
         "dark":"#d32f2f",
         "contrastText":"#fff"
      },
      "warning":{
         "light":"#ffb74d",
         "main":"#ff9800",
         "dark":"#f57c00",
         "contrastText":"rgba(0, 0, 0, 0.87)"
      },
      "info":{
         "light":"#64b5f6",
         "main":"#2196f3",
         "dark":"#1976d2",
         "contrastText":"#fff"
      },
      "success":{
         "light":"#81c784",
         "main":"#4caf50",
         "dark":"#388e3c",
         "contrastText":"rgba(0, 0, 0, 0.87)"
      },
      "grey":{
         "50":"#fafafa",
         "100":"#f5f5f5",
         "200":"#eeeeee",
         "300":"#e0e0e0",
         "400":"#bdbdbd",
         "500":"#9e9e9e",
         "600":"#757575",
         "700":"#616161",
         "800":"#424242",
         "900":"#212121",
         "A100":"#d5d5d5",
         "A200":"#aaaaaa",
         "A400":"#303030",
         "A700":"#616161"
      },
      "contrastThreshold":3,
      "tonalOffset":0.2,
      "text":{
         "primary":"rgba(0, 0, 0, 0.87)",
         "secondary":"rgba(0, 0, 0, 0.54)",
         "disabled":"rgba(0, 0, 0, 0.38)",
         "hint":"rgba(0, 0, 0, 0.38)"
      },
      "divider":"rgba(0, 0, 0, 0.12)",
      "background":{
         "paper":"#fff",
         "default":"#fafafa"
      },
      "action":{
         "active":"rgba(0, 0, 0, 0.54)",
         "hover":"rgba(0, 0, 0, 0.04)",
         "hoverOpacity":0.04,
         "selected":"rgba(0, 0, 0, 0.08)",
         "selectedOpacity":0.08,
         "disabled":"rgba(0, 0, 0, 0.26)",
         "disabledBackground":"rgba(0, 0, 0, 0.12)",
         "disabledOpacity":0.38,
         "focus":"rgba(0, 0, 0, 0.12)",
         "focusOpacity":0.12,
         "activatedOpacity":0.12
      }
   },
   "props":{
      **"MuiButton":{
         "variant":"contained",
         "color":"primary"** // maybe this has some impact, but it's just default, not an override
      }
   },
   "shadows":[
      "none",
      "0px 2px 1px -1px rgba(0,0,0,0.2),0px 1px 1px 0px rgba(0,0,0,0.14),0px 1px 3px 0px rgba(0,0,0,0.12)",
      "0px 3px 1px -2px rgba(0,0,0,0.2),0px 2px 2px 0px rgba(0,0,0,0.14),0px 1px 5px 0px rgba(0,0,0,0.12)",
      "0px 3px 3px -2px rgba(0,0,0,0.2),0px 3px 4px 0px rgba(0,0,0,0.14),0px 1px 8px 0px rgba(0,0,0,0.12)",
      "0px 2px 4px -1px rgba(0,0,0,0.2),0px 4px 5px 0px rgba(0,0,0,0.14),0px 1px 10px 0px rgba(0,0,0,0.12)",
      "0px 3px 5px -1px rgba(0,0,0,0.2),0px 5px 8px 0px rgba(0,0,0,0.14),0px 1px 14px 0px rgba(0,0,0,0.12)",
      "0px 3px 5px -1px rgba(0,0,0,0.2),0px 6px 10px 0px rgba(0,0,0,0.14),0px 1px 18px 0px rgba(0,0,0,0.12)",
      "0px 4px 5px -2px rgba(0,0,0,0.2),0px 7px 10px 1px rgba(0,0,0,0.14),0px 2px 16px 1px rgba(0,0,0,0.12)",
      "0px 5px 5px -3px rgba(0,0,0,0.2),0px 8px 10px 1px rgba(0,0,0,0.14),0px 3px 14px 2px rgba(0,0,0,0.12)",
      "0px 5px 6px -3px rgba(0,0,0,0.2),0px 9px 12px 1px rgba(0,0,0,0.14),0px 3px 16px 2px rgba(0,0,0,0.12)",
      "0px 6px 6px -3px rgba(0,0,0,0.2),0px 10px 14px 1px rgba(0,0,0,0.14),0px 4px 18px 3px rgba(0,0,0,0.12)",
      "0px 6px 7px -4px rgba(0,0,0,0.2),0px 11px 15px 1px rgba(0,0,0,0.14),0px 4px 20px 3px rgba(0,0,0,0.12)",
      "0px 7px 8px -4px rgba(0,0,0,0.2),0px 12px 17px 2px rgba(0,0,0,0.14),0px 5px 22px 4px rgba(0,0,0,0.12)",
      "0px 7px 8px -4px rgba(0,0,0,0.2),0px 13px 19px 2px rgba(0,0,0,0.14),0px 5px 24px 4px rgba(0,0,0,0.12)",
      "0px 7px 9px -4px rgba(0,0,0,0.2),0px 14px 21px 2px rgba(0,0,0,0.14),0px 5px 26px 4px rgba(0,0,0,0.12)",
      "0px 8px 9px -5px rgba(0,0,0,0.2),0px 15px 22px 2px rgba(0,0,0,0.14),0px 6px 28px 5px rgba(0,0,0,0.12)",
      "0px 8px 10px -5px rgba(0,0,0,0.2),0px 16px 24px 2px rgba(0,0,0,0.14),0px 6px 30px 5px rgba(0,0,0,0.12)",
      "0px 8px 11px -5px rgba(0,0,0,0.2),0px 17px 26px 2px rgba(0,0,0,0.14),0px 6px 32px 5px rgba(0,0,0,0.12)",
      "0px 9px 11px -5px rgba(0,0,0,0.2),0px 18px 28px 2px rgba(0,0,0,0.14),0px 7px 34px 6px rgba(0,0,0,0.12)",
      "0px 9px 12px -6px rgba(0,0,0,0.2),0px 19px 29px 2px rgba(0,0,0,0.14),0px 7px 36px 6px rgba(0,0,0,0.12)",
      "0px 10px 13px -6px rgba(0,0,0,0.2),0px 20px 31px 3px rgba(0,0,0,0.14),0px 8px 38px 7px rgba(0,0,0,0.12)",
      "0px 10px 13px -6px rgba(0,0,0,0.2),0px 21px 33px 3px rgba(0,0,0,0.14),0px 8px 40px 7px rgba(0,0,0,0.12)",
      "0px 10px 14px -6px rgba(0,0,0,0.2),0px 22px 35px 3px rgba(0,0,0,0.14),0px 8px 42px 7px rgba(0,0,0,0.12)",
      "0px 11px 14px -7px rgba(0,0,0,0.2),0px 23px 36px 3px rgba(0,0,0,0.14),0px 9px 44px 8px rgba(0,0,0,0.12)",
      "0px 11px 15px -7px rgba(0,0,0,0.2),0px 24px 38px 3px rgba(0,0,0,0.14),0px 9px 46px 8px rgba(0,0,0,0.12)"
   ],
   ...
}
1 Answers
<Button color={theme.palette.primary.light} variant="contained">

This won't work. The color prop only accepts the following values:

'default'
| 'inherit'
| 'primary'
| 'secondary'

If you want to use the primary light color then you need to change the style in makeStyles.

Something like:

const useStyles = makeStyles((theme) => ({
  buttonLight: {
    color: theme.palette.getContrastText(theme.palette.primary.light),
    backgroundColor: theme.palette.primary.light,
    '&:hover': {
      backgroundColor: theme.palette.primary.light,
    },
  },
}))

<Button className={classes.buttonLight} variant="contained">
Related