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';
Â
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'
})
Notice:
- Right here, we’re utilizing the
Field
element however as a substitute of theField
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;
`;
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,
})
)
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,
).
Â
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};
`,
)
Â
Youngster Part and Youngster Factor
Â
JSX
Â
Suppose, we would like the next JSX
:
<ParentComponent>
<div>Hello</div>
<Field className='childComponent'> Hiya </Field>
</ParentComponent>
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]
},
})
)
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]};
};
`,
)
Â
Pseudo lessons
Â
JSX
Â
Suppose, we would like the next JSX
:
<PseudoClasses>
<div>Hello</div>
<Field className='childComponent'> Hiya </Field>
</PseudoClasses>
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,
},
})
)
Â
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};
};
`,
)
Â
Sibling Part
Â
JSX
Â
Suppose, we would like the next JSX
:
<>
<MainComponent> Hiya </MainComponent>
<Field className='siblingComponent'> Hello </Field>
</>
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]
},
})
)
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]};
};
`,
)
Â
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>
</>
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'
}));
What is that this
shouldForwaredProp
?We might already know that MUI5 makes use of emotion as a default model engine. This
shouldForwardProp
is coming fromemotion
.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'}
`);
Â
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'
}));
Â
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'};
`);
Â
Default Prop Worth
Â
We are able to additionally cross the default worth for the props.
TestingProps.defaultProps = {
darkish: false,
border: false
}
Â
Â
That is it. 😃 Thanks for studying. 🎉