Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing props to MUI styles

Given the Card code as in here. How can I update the card style or any material UI style as from:

const styles = theme => ({
  card: {
  minWidth: 275,
},

To such follows:

const styles = theme => ({
  card: {
  minWidth: 275, backgroundColor: props.color
},

when I tried the latest one, I got

Line 15:  'props' is not defined  no-undef

when I updated code to be :

const styles = theme =>  (props) => ({
  card: {
  minWidth: 275, backgroundColor: props.color
},

also

 const styles  = (theme ,props) => ({
   card: {
   minWidth: 275, backgroundColor: props.color
 },

Instead of

const styles = theme => ({
  card: {
  minWidth: 275, backgroundColor: props.color
},

I got the component card style at the web page messy.

By the way, I pass props as follows:

<SimpleCard backgroundColor="#f5f2ff" />

please help!

like image 272
Diamond Avatar asked Feb 20 '18 06:02

Diamond


People also ask

How do I pass props to MUI styles?

To pass props to React Material UI styles, we can call the useStyle hook returned by makeStyles with the props object. In makeStyles , we can set the style properties that takes the props as a parameter and return the value we want.

How do you pass props in style in react?

Use the React. CSSProperties type to pass CSS styles as props in React TypeScript, e.g. style: React. CSSProperties; . The CSSProperties type is used to type the style object that consists of CSS property names and values.

How do you override styles in MUI?

If you want to override a component's styles using custom classes, you can use the className prop, available on each component.


4 Answers

Deleted the old answer, because it's no reason for existence.

Here's what you want:

import React from 'react'; import { makeStyles } from '@material-ui/core';  const useStyles = makeStyles({     firstStyle: {         backgroundColor: props => props.backgroundColor,     },     secondStyle: {         color: props => props.color,     }, });  const MyComponent = ({children, ...props}) =>{     const { firstStyle, secondStyle } = useStyles(props);     return(         <div className={`${firstStyle} ${secondStyle}`}>             {children}         </div>     ) }  export default MyComponent; 

Now you can use it like:

<MyComponent color="yellow" backgroundColor="purple">     Well done </MyComponent> 

Official Documentation

like image 196
James Tan Avatar answered Sep 28 '22 10:09

James Tan


Solution for how to use both props and theme in material ui :

const useStyles = props => makeStyles( theme => ({     div: {         width: theme.spacing(props.units || 0)       } }));  export default function ComponentExample({ children, ...props }){     const { div } = useStyles(props)();     return (         <div className={div}>{children}</div>     ); } 
like image 25
Muhamed Avatar answered Sep 28 '22 10:09

Muhamed


Here's the official Material-UI demo.

And here's a very simple example. It uses syntax similar to Styled Components:

import React from "react";
import { makeStyles, Button } from "@material-ui/core";

const useStyles = makeStyles({
  root: {
    background: props => props.color,
    "&:hover": {
      background: props => props.hover
    }
  },
  label: { fontFamily: props => props.font }
});

export function MyButton(props) {
  const classes = useStyles(props);
  return <Button className={classes.root} classes={{ label: classes.label }}>My Button</Button>;
}


// and the JSX...
<MyButton color="red" hover="blue" font="Comic Sans MS" />

This demo uses makeStyles, but this feature is also available in styled and withStyles.

This was first introduced in @material-ui/styles on Nov 3, 2018 and was included in @material-ui/core starting with version 4.

like image 21
Dominus.Vobiscum Avatar answered Sep 28 '22 10:09

Dominus.Vobiscum


Here the Typescript solution:

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import {Theme} from '@material-ui/core';

export interface StyleProps {
    height: number;
}

const useStyles = makeStyles<Theme, StyleProps>(theme => ({
  root: {
    background: 'green',
    height: ({height}) => height,
  },
}));

export default function Hook() {

  const props = {
    height: 48
  }

  const classes = useStyles(props);
  return <Button className={classes.root}>Styled with Hook API</Button>;
}

If you want to play with it, try it in this CodeSandbox

like image 32
Jöcker Avatar answered Sep 28 '22 10:09

Jöcker