Tuesday, August 2, 2022
HomeWordPress DevelopmentStyled Elements in Materials UI (MUI) with Styled Utility

Styled Elements in Materials UI (MUI) with Styled Utility


If we’re already accustomed to styled parts, we’d wish to use that together with the MUI or if we wish to present some customized model to any MUI element, then we will additionally think about using the styled() utility. The official documentation of MUI does not have sufficient examples with the styled() utility. This text’s goal is to offer sufficient examples of styled() utility with each Javascript object syntax & CSS like syntax.

 

📑Desk of Contents

 



Primary

 

Import
 

// You need to import 'styled' utility kind mui
import { styled } from '@mui/materials/types';

// Additionally, you should import the parts which you might be gonna use with the styled utility
import Field from '@mui/materials/Field';
Enter fullscreen mode

Exit fullscreen mode

 

JavaScript Object Syntax

 

Let’s create a element with styled utility through the use of JavaScript object syntax:

const Primary = styled(Field)({

  backgroundColor: 'aliceblue',
  shade: 'darkslategray',
  padding: '2rem',
  textAlign:'heart'

})
Enter fullscreen mode

Exit fullscreen mode

Notice:

  • Right here, we’re utilizing the Field element however as a substitute of the Field element, we will use every other MUI element. We simply have to import that element.
  • As a substitute of a element we will additionally use an HTML tag. To make use of HTML tag, we’ve to place the HTML tag inside quote, styled('div').
  • Right here, the variable title is Primary. All the time make it possible for the primary letter of the variable is capital as a result of it should work like a element and a element title has to start out with a capital letter.

 

CSS Like Syntax

 

As a substitute of JavaScript object syntax, we will even have CSS-like syntax:

const Primary = styled(Field)`
  background-color: aliceblue;
  shade: darkslategray;
  padding: 2rem;
  text-align: heart;
`;
Enter fullscreen mode

Exit fullscreen mode

Notice:

 



MUI Theme within the styled() Utility

 

Check out the default theme of MUI.

 

JavaScript Object Syntax

 

const UsingTheme = styled(Field)(
  ({ theme }) => ({
    backgroundColor: theme.palette.major.mild,
    shade: theme.palette.gray[900],
    padding: theme.spacing(2),
    textAlign: 'heart',
    ...theme.typography.h6,
  })
)

Enter fullscreen mode

Exit fullscreen mode

The next image is exhibiting the MUI default theme. Within the image, we will see that the h6 is an object. It has fontFamily, fontWeight, fontSize, lineHeight & letterSpacing properties. We wish all of them. So, we’re destructuring it (...theme.typography.h6,).

Image description

 

CSS Like Syntax

 

const UsingTheme = styled(Field)(
  ({ theme }) => `
  background-color: ${theme.palette.major.mild};
  shade: ${theme.palette.gray[900]};
  padding: ${theme.spacing(2)};
  text-align: heart;


  ${ /* right here, we won't destructure like javascript object syntax. So, we have to manually entry all of the properties of 'h6'  */'' }

  font-size: ${theme.typography.h6.fontSize};
  font-weight: ${theme.typography.h6.fontWeight};
  font-family: ${theme.typography.h6.fontFamily};
  line-height: ${theme.typography.h6.lineHeight};
  letter-spacing: ${theme.typography.h6.letterSpacing};
 `,
)

Enter fullscreen mode

Exit fullscreen mode

 



Youngster Part and Youngster Factor

 

JSX

 

Suppose, we would like the next JSX:

<ParentComponent>

      <div>Hello</div>

     <Field className='childComponent'> Hiya </Field>

</ParentComponent>

Enter fullscreen mode

Exit fullscreen mode

So, we have to create the ParentComponent element and likewise have to model the kid ingredient div and youngster element Field.

 

JavaScript Object Syntax

 


const ParentComponent = styled(Field)(

  ({ theme }) => ({

    backgroundColor: theme.palette.major.mild,
    shade: theme.palette.gray[900],
    padding: theme.spacing(2),
    textAlign: 'heart',


    // Youngster ingredient
    "> div": {
      backgroundColor: theme.palette.error.darkish,
      shade: theme.palette.gray[100]
    },


    // Youngster Part (We have to choose the category or id which is used within the youngster element)
    "> .childComponent": {
      backgroundColor: theme.palette.success.darkish,
      shade: theme.palette.gray[100]
    },

  })
)

Enter fullscreen mode

Exit fullscreen mode

Notice:

  • I have not discovered a approach to choose an MUI element with out the category or an id that’s utilized in that youngster element. If you realize any approach different, please write it down within the remark part.

 

CSS Like Syntax

 

const ParentComponent = styled(Field)(
  ({ theme }) => `
  background-color: ${theme.palette.major.mild};
  shade: ${theme.palette.gray[900]};
  padding: ${theme.spacing(2)};
  text-align: heart;


  ${ /* Youngster ingredient  */'' }

  > div  {
    background-color: ${theme.palette.error.darkish};
    shade: ${theme.palette.gray[100]};
  };


  ${ /* Youngster Part (We have to choose the category or id which is used within the youngster element)  */'' }

   > .childComponent {
    background-color: ${theme.palette.success.darkish};
    shade: ${theme.palette.gray[100]};
  };


 `,
)

Enter fullscreen mode

Exit fullscreen mode

 



Pseudo lessons

 

JSX

 

Suppose, we would like the next JSX:

<PseudoClasses>

      <div>Hello</div>

     <Field className='childComponent'> Hiya </Field>

</PseudoClasses>
Enter fullscreen mode

Exit fullscreen mode

So, we have to create the PseudoClasses element and likewise have to model the kid ingredient div and youngster element Field with pseudo-classes.

 

JavaScript Object Syntax

 

const PseudoClasses = styled(Field)(

  ({ theme }) => ({

    backgroundColor: theme.palette.major.mild,
    shade: theme.palette.gray[900],
    padding: theme.spacing(2),
    textAlign: 'heart',

    ":hover": {
      backgroundColor: theme.palette.major.darkish,
      shade: theme.palette.gray[100],
    },

    ":energetic": {
      backgroundColor: theme.palette.warning.darkish,
      shade: theme.palette.gray[100],
    },


    // Pseudo choose youngster ingredient
    ":hover > div": {
      backgroundColor: theme.palette.error.darkish,
    },


    // Pseudo choose youngster element (We have to choose the category or id which is used within the youngster element) 
    ":hover > .childComponent": {
      backgroundColor: theme.palette.success.darkish,
    },

  })
)
Enter fullscreen mode

Exit fullscreen mode

 

CSS Like Syntax

 

const PseudoClasses = styled(Field)(
  ({ theme }) => `
  background-color: ${theme.palette.major.mild};
  shade: ${theme.palette.gray[900]};
  padding: ${theme.spacing(2)};
  text-align: heart;


  :hover {
    background-color: ${theme.palette.major.darkish};
    shade: ${theme.palette.gray[100]};
  };


  :energetic {
    background-color: ${theme.palette.warning.darkish};
    shade: ${theme.palette.gray[100]};
  };


  ${ /* Pseudo choose youngster ingredient  */'' }

  :hover > div  {
    background-color: ${theme.palette.error.darkish};
  };


  ${ /* Pseudo choose youngster element (We have to choose the category or id which is used within the youngster element)   */'' }

  :hover > .childComponent {
    background-color: ${theme.palette.success.darkish};
  };


 `,
)
Enter fullscreen mode

Exit fullscreen mode

 



Sibling Part

 

JSX

 

Suppose, we would like the next JSX:

<>
  <MainComponent> Hiya </MainComponent>
  <Field className='siblingComponent'> Hello </Field>
</>
Enter fullscreen mode

Exit fullscreen mode

So, we have to create the MainComponent and likewise have to model the sibling element Field.

 

JavaScript Object Syntax

 

const MainComponent = styled(Field)(

  ({ theme }) => ({

    backgroundColor: theme.palette.major.mild,
    shade: theme.palette.gray[900],

    // Adjoining Sibling Part (We have to use class or id of the Sibling element)
    "+ .siblingComponent": {
      backgroundColor: theme.palette.success.darkish,
      shade: theme.palette.gray[100]
    },

  })
)
Enter fullscreen mode

Exit fullscreen mode

Notice:

  • As a substitute of a sibling element, if there was a sibling HTML tag, we may have a method that too ("+ div").

  • The adjoining sibling selector (+) is used to pick a component that’s immediately after one other particular ingredient.

 

CSS Like Syntax

 

const MainComponent= styled(Field)(

  ({ theme }) => `
  background-color: ${theme.palette.major.mild};
  shade: ${theme.palette.gray[900]};


  ${ /* Adjoining Sibling Part (We have to use class or id of the Sibling element) */'' }

   + .siblingComponent {
    background-color: ${theme.palette.success.darkish};
    shade: ${theme.palette.gray[100]};
  };

 `,
)
Enter fullscreen mode

Exit fullscreen mode

 



Props

 

 

JSX

 

Suppose, we wish to have a element (TestingProp) the place we will cross two props: darkish & border. The worth of each props is boolean & the worth will of those props controls the model of the element.

   <>

   <TestingProps border={true} darkish={true}>Hiya
   </TestingProps>

  </>
Enter fullscreen mode

Exit fullscreen mode

So, we have to create the TestingProps and likewise want work with the prop darkish & border.

 

JavaScript Object Syntax (With out MUI Theme)

 

const TestingProps = styled(Field, {

    // Configure which props must be forwarded on DOM
   shouldForwardProp: (prop) => prop !== 'darkish' && prop!== 'border'

   })

   (({ darkish, border }) => ({

   backgroundColor: darkish? "black" : "white",
   shade: darkish? "white" : "black",
   border: border? "1rem stable pink" : 'none'

   }));

Enter fullscreen mode

Exit fullscreen mode

What is that this shouldForwaredProp?

We might already know that MUI5 makes use of emotion as a default model engine. This shouldForwardProp is coming from emotion. shouldForwaredProp is used to cross prop. In an instance within the official documentation, shouldForwaredProp is used, you’ll be able to verify the instance if you need.

 

CSS Like Syntax (With out MUI Theme)

 

const TestingProps4 = styled(Field, {

  // Configure which props must be forwarded on DOM
  shouldForwardProp: prop => prop !== 'darkish',

  })


  (({ darkish, border }) => `

    background-color: ${darkish? "black" : "white"};
    shade: ${darkish? "white" : "black"};
    border: ${border? "1rem stable pink" : 'none'}

  `);

Enter fullscreen mode

Exit fullscreen mode

 

JavaScript Object Syntax (With MUI Theme)

 

const TestingProps = styled(Field, {

   // Configure which props must be forwarded on DOM
  shouldForwardProp: (prop) => prop !== 'darkish' && prop!== 'border'

  })

  (({ darkish, border, theme }) => ({

  backgroundColor: darkish? theme.palette.gray[900] : theme.palette.gray[100],
  shade: darkish? theme.palette.gray[100] : theme.palette.gray[900],
  border: border? `1rem stable ${theme.palette.major.principal}` : 'none'

  }));

Enter fullscreen mode

Exit fullscreen mode

 

CSS Like Syntax (With MUI Theme)

 

const TestingProps = styled(Field, {

   // Configure which props must be forwarded on DOM
   shouldForwardProp: (prop) => prop !== 'darkish' && prop!== 'border'

  })


  (({ darkish, border, theme }) => `

    background-color: ${darkish? theme.palette.gray[900] : theme.palette.gray[100]};
    shade: ${darkish? theme.palette.gray[100] : theme.palette.gray[900]};
    border: ${border? `1rem stable ${theme.palette.major.principal}` : 'none'};

  `);

Enter fullscreen mode

Exit fullscreen mode

 

Default Prop Worth

 

We are able to additionally cross the default worth for the props.

TestingProps.defaultProps = {
    darkish: false,
    border: false
  }

Enter fullscreen mode

Exit fullscreen mode

 


 

That is it. 😃 Thanks for studying. 🎉

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments