-
Create a react app (if you already have a project go to the next section):
$ yarn create react-app <appName>
-
Run the app:
$ yarn start
-
Install dependency
$ yarn add @material-ui/core
-
Material-UI uses Roboto Font and font icons so we'll have to add their stylesheets in
index.html(optional)<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" /> <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
-
To use pre-built SVG icons provided by Material-UI install the following dependancy (optional)
$ yarn add @material-ui/icons
-
Using a button component from Material-UI
- Create a component called
ButtonDemo.jsImport the component
import { Button } from "@material-ui/core";
- Use the component
<Button> I am a button </Button>
-
Import
<ButtonDemo/>inapp.jsand render it -
Customize the button Documentation
<Button variant="contained" color="secondary" size="medium"> I am contained button </Button> <Button variant="outlined" color="primary" size="medium" fullWidth> I am a outlined button </Button> <Button variant="text" color="secondary" size="medium"> I am a text button </Button>
- Create a component called
-
Using
Typographyin Material-UI- Create a component called
TypographDemo.jsand ImportTypography
import { Typography } from "@material-ui/core";
- Use
Typography
<Typography>Any text here</Typography>
-
Import
<TypographDemo/>inapp.jsand render it -
Customize Typography using options
<Typography align="center" > Any text here </Typography> <Typography align="left" variant="h1" > Any text here </Typography> <Typography align="right" variant="h1" gutterBottom color="primary" > Any text here </Typography>
- Create a component called
-
Look at the default theme for Material-UI:
- The theme defines the screen breakpoints, components directions, colors , typography and many other things
-
To change the theme first create a
theme.jsfile in your project directory -
In the
theme.jsfile, import MuiTheme to customize cssimport { createMuiTheme } from "@material-ui/core/styles";
-
Define a constant
themeand create a styled component for your project using the same optionsMaterial-UIuses to override their stylesconst theme = createMuiTheme({ palette: { primary: { main: "#32CD32", }, secondary: { main: "#FF4500", }, }, }); export default theme;
-
Now we need to pass a ThemeProvider to our project
- In
index.jsimport the following
import { ThemeProvider } from "@material-ui/core/styles";
- Import your theme
import theme from "./theme.js";
- Wrap the
<App>with theThemeProviderand pass it thetheme
<ThemeProvider theme={theme}> <App /> </ThemeProvider>
- In
-
Import
styledfromt material-ui'import { styled } from "@material-ui/core/styles";
-
Import the component/components you're going to style
import { Button } from "@material-ui/core";
-
Customize you styled component
const MyButton = styled(Button)({ background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)", border: 0, borderRadius: 3, boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)", color: "white", height: 48, padding: "0 30px", });
-
Use the Styled component in your code
<MyButton>Styled Components</MyButton>
-
To pass props to a styled component for multiple use
- Change the code above to look like the following in
ButtonDemo.js
const MyButton = styled(Button)({ background: (props) => props.customcolor ?? "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)", border: 0, borderRadius: 3, boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)", color: "white", height: 48, padding: "0 30px", });
- Then change the buttons rendered to look like the following
<MyButton customcolor={"#CCB647"}>I am contained button</MyButton> <MyButton customcolor={"#FFFF00"} variant="contained" color="secondary"> I am a outlined button </MyButton> <Button variant="text" color="secondary"> I am a text button </Button>
- Change the code above to look like the following in
-
Cards
-
Create a component called
CardDemo.jsImport theCardcomponent and it's childrenimport { Card, CardHeader, CardMedia, CardContent, Typography, Avatar, } from "@material-ui/core";
-
Import
styledfor styled components
import { styled } from "@material-ui/core/styles";
-
Style the components
const MyCardMedia = styled(CardMedia)({ height: 30, paddingTop: "56.25%", // 16:9 });
-
Using the imported components options
<Card> <CardHeader title="Food" subheader="Everyone loves food" /> <MyCardMedia image="https://upload.wikimedia.org/wikipedia/commons/6/6d/Good_Food_Display_-_NCI_Visuals_Online.jpg" title="All types of food" /> <CardContent> <Typography paragraph color="secondary"> This impressive paella is a perfect party dish and a fun meal to cook together with your guests. Add 1 cup of frozen peas along with the mussels, if you like. </Typography> </CardContent> </Card>
-
Import
<CardDemo/>intoapp.jsand render it
-
-
Grid
-
Create a component called
GridDemo.jsand Import theGridcomponent and Card component from the previous secitionimport { Grid } from "@material-ui/core"; import CardDemo from "./CardDemo.js";
-
Using the
Gridoptions<Grid container spacing={8}> <Grid item xs={12}> <CardDemo /> </Grid> <Grid item xs={6}> <CardDemo /> </Grid> <Grid item xs={6}> <CardDemo /> </Grid> <Grid item md={4} xs={6}> <CardDemo /> </Grid> <Grid item md={4} xs={6}> <CardDemo /> </Grid> <Grid item md={4} xs={6}> <CardDemo /> </Grid> <Grid item md={4} xs={6}> <CardDemo /> </Grid> </Grid>
-
Import
<GridDemo/>intoapp.jsand render it
-
-
Containers
-
Create a component called
ContainerDemo.jsImport theContainercomponentimport { Container } from "@material-ui/core"; import GridDemo from "./GridDemo.js";
-
Using the
Containeroptions<Container maxWidth="xl"> <GridDemo> </Container>
-
Import
<ContainerDemo/>intoapp.jsand render it
-