Pass props to makeStyles and use in CSS shorthand property in Material UI

EliteRaceElephant

I pass props to a button component:

const StoreButton = ({ storeColor }) => {
  const borderBottom = `solid 3px ${storeColor}`;
  const classes = useStyles({ borderBottom });

  return (
    <Button variant="outlined" size="small" className={classes.button}>
      Store
    </ Button>
  )
};

I create the borderBottom property before I use it in classes. I would like to construct the property inside makeStyles but this leads to an error:

const useStyles = makeStyles(theme => ({
  button: {
    borderBottom: props => props.borderBottom,
    // borderBottom: `solid 3px ${props => props.borderBottom}`; // function is pasted in
  },
}));

If I construct the CSS shorthand inside makeStyles it creates solid 3px solid 3px function(props){return props.borderBottom;}. when I inspect it in Chrome. Thus, this way the styles are invalid.

Is there a way to pass props to CSS shorthand properties without this workaround of creating it outside makeStyles?

Jap Mul

You almost had it right, the only thing to change is this:

borderBottom: props => `1px solid ${props.color}`,

Now the value is a proper function. Inside the function it constructs the correct string value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can you use the @supports css rule in material ui makeStyles?

Converting css styles to material ui makeStyles

How to use Media queries with Material UI makestyles

How can I use CSS @media for responsive with makeStyles on Reactjs Material UI?

makeStyles in Material-ui need to use so many ( !important <= css code ) to style mui components

Material UI makeStyles not changing the css properties of all elements

Material UI cant use Props

What is the use of '& .MuiTextField-root' in { makeStyles } of Material UI?

Makestyles hook is not working in Material Ui

Material UI Tab pass props to component

How to pass props to the inputComponent material-ui?

Pass Accessibility Props to Material UI Button

How to use 'theme' and 'props' in makeStyles?

how apply font feature settings in material UI makeStyles and change backgorundColor dynamically through props

How to pass multiple props to makeStyles() with TypeScript

makeStyles with TypeScript - pass props to make styles explanation

set with and height on makeStyles theme material-ui

Material ui: how to `makeStyles` with duplicate key?

Material-UI | Using `theme` in makeStyles

Material-ui makeStyles with both before and after

MakeStyles (Material UI) apply style to child element

Material-UI - Why should I use makeStyles instead of inline styles?

How do I make a reusable component for the Cards and how can I use the new makeStyles of material-ui?

Can you use a Sass variable in a CSS shorthand property?

How to change Material UI component property according to props

How in Material UI to pass a props or a condition to a nested style method

How to pass an array to material-ui dataSource props

How to pass a function as props to a component of Material-UI for React?

How to pass props to to material-ui styles in a sub-selector