Categories
Material UI

Material UI — Buttons and Checkboxes

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to add buttons and checkboxes Material Design.

Disabled Elevation

We can disable the elevation of buttons with the disableElevation prop.

For instance, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <ButtonGroup disableElevation variant="contained" color="primary">
        <Button>foo</Button>
        <Button>bar</Button>
      </ButtonGroup>
    </div>
  );
}

to disable the elevation.

Checkbox

Checkboxes let us turn options on or off.

To create one, we can use the Checkbox component:

import React from "react";
import Checkbox from "@material-ui/core/Checkbox";

export default function App() {
  const [checked, setChecked] = React.useState(true);

  const handleChange = event => {
    setChecked(event.target.checked);
  };

  return (
    <div>
      <Checkbox
        checked={checked}
        onChange={handleChange}
        inputProps={{ "aria-label": "checkbox" }}
      />
    </div>
  );
}

We add the Checkbox component to add a checkbox.

It takes the checked prop to set the checked value.

onChange takes the checked value so that we can set the state.

And inputProps let us add attributes to the checkbox.

Checkbox with FormControlLabel

We can add a label to our checkbox with the FormControlLabel .

For instance, we can write:

import React from "react";
import Checkbox from "@material-ui/core/Checkbox";
import FormControlLabel from "@material-ui/core/FormControlLabel";

export default function App() {
  const [checked, setChecked] = React.useState(true);

  const handleChange = event => {
    setChecked(event.target.checked);
  };

  return (
    <div>
      <FormControlLabel
        control={
          <Checkbox
            checked={checked}
            onChange={handleChange}
            name="checked"
            color="primary"
          />
        }
        label="Primary"
      />
    </div>
  );
}

to create a checkbox with a label beside it.

We wrap the FormControlLabel outside our Checkbox so that’s we can add a label to it with the label prop.

Checkboxes with FormGroup

To add multiple checkboxes, we can put them into a FormGroup .

For instance, we can write:

import React from "react";
import Checkbox from "@material-ui/core/Checkbox";
import FormControl from "@material-ui/core/FormControl";
import FormLabel from "@material-ui/core/FormLabel";
import FormGroup from "@material-ui/core/FormGroup";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import FormHelperText from "@material-ui/core/FormHelperText";

export default function App() {
  const [checked, setChecked] = React.useState({
    foo: true,
    bar: false
  });

const handleChange = event => {
    setChecked({ ...checked, [event.target.name]: event.target.checked });
  };

return (
    <div>
      <FormControl component="fieldset">
        <FormLabel component="legend">check them all</FormLabel>
        <FormGroup>
          <FormControlLabel
            control={
              <Checkbox
                checked={checked.foo}
                onChange={handleChange}
                name="foo"
              />
            }
            label="foo"
          />
          <FormControlLabel
            control={
              <Checkbox
                checked={checked.bar}
                onChange={handleChange}
                name="bar"
              />
            }
            label="bar"
          />
        </FormGroup>
        <FormHelperText>Be careful</FormHelperText>
      </FormControl>
    </div>
  );
}

To create a group of checkb0oxes with the FormControl component.

We wrapped lots of things inside it.

We have a FormLabel to display the label at the top.

Then we have a FormGroup to hold tge checkboxes.

The FormControlLabel has the label for each checkbox.

FormHelperText has the small text we display at the bottom of the form.

Label Placement

The label placement can be changed.

To do that, we can use the labelPlacement prop to change it.

For instance, we can write:

import React from "react";
import Checkbox from "@material-ui/core/Checkbox";
import FormControlLabel from "@material-ui/core/FormControlLabel";

export default function App() {
  return (
    <div>
      <FormControlLabel
        value="bottom"
        control={<Checkbox color="primary" />}
        label="Bottom"
        labelPlacement="bottom"
      />
    </div>
  );
}

Now we have the label below the checkbox.

Other possible values include top , start , and end .

Customized Checkbox

We can apply our own styles to the checkbox.

For instance, we can write:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import clsx from "clsx";
import Checkbox from "@material-ui/core/Checkbox";

const useStyles = makeStyles({
  root: {
    "&:hover": {
      backgroundColor: "transparent"
    }
  },
  icon: {
    borderRadius: 3,
    width: 16,
    height: 16,
    backgroundColor: "#ebf1f5",
    "input:hover ~ &": {
      backgroundColor: "#ebf1f5"
    },
    "input:disabled ~ &": {
      boxShadow: "none"
    }
  },
  checkedIcon: {
    backgroundColor: "#137cbd",
    backgroundImage: "blue",
    "&:before": {
      display: "block",
      width: 16,
      height: 16,
      backgroundColor: "gray"
    },
    "input:hover ~ &": {
      backgroundColor: "#106ba3"
    }
  }
});

export default function App() {
  const classes = useStyles();

  return (
    <Checkbox
      className={classes.root}
      disableRipple
      color="default"
      checkedIcon={<span className={clsx(classes.icon, classes.checkedIcon)} />}
      icon={<span className={classes.icon} />}
      inputProps={{ "aria-label": "decorative checkbox" }}
    />
  );
}

to create a checkbox without a checkmark.

We set a grayish color for the unchecked state.

And we set the checked state to blue.

These are done according to the styles we passed into makeStyles .

Also, we used the clsx package to toggle between the checked and unchecked styles.

Conclusion

We can add checkboxes with Material UI.

Also, we can add labels and style them the way we want.

Categories
Material UI

Material UI — Buttons and Groups

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to add buttons Material Design.

Complex Buttons

We can add more complex buttons with the ButtonBase component.

For instance, we can write:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import ButtonBase from "@material-ui/core/ButtonBase";
import Typography from "@material-ui/core/Typography";

const useStyles = makeStyles(theme => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  },
  image: {
    position: "relative",
    height: 200,
    [theme.breakpoints.down("xs")]: {
      width: "100% !important",
      height: 100
    },
    "&:hover, &$focusVisible": {
      zIndex: 1,
      "& $imageBackdrop": {
        opacity: 0.15
      },
      "& $imageMarked": {
        opacity: 0
      },
      "& $imageTitle": {
        border: "4px solid currentColor"
      }
    }
  },
  focusVisible: {},
  imageButton: {
    position: "absolute",
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    color: theme.palette.common.white
  },
  imageSrc: {
    position: "absolute",
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    backgroundSize: "cover",
    backgroundPosition: "center 40%"
  },
  imageBackdrop: {
    position: "absolute",
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    backgroundColor: theme.palette.common.black,
    opacity: 0.4,
    transition: theme.transitions.create("opacity")
  },
  imageTitle: {
    position: "relative",
    padding: `${theme.spacing(2)}px ${theme.spacing(4)}px ${theme.spacing(1) +
      6}px`
  },
  imageMarked: {
    height: 3,
    width: 18,
    backgroundColor: theme.palette.common.white,
    position: "absolute",
    bottom: -2,
    left: "calc(50% - 9px)",
    transition: theme.transitions.create("opacity")
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <ButtonBase
        focusRipple
        className={classes.image}
        focusVisibleClassName={classes.focusVisible}
        style={{
          width: "200px"
        }}
      >
        <span
          className={classes.imageSrc}
          style={{
            backgroundImage: `url(http://placekitten.com/200/200)`
          }}
        />
        <span className={classes.imageBackdrop} />
        <span className={classes.imageButton}>
          <Typography
            component="span"
            variant="subtitle"
            color="inherit"
            className={classes.imageTitle}
          >
            {"Cat"}
            <span className={classes.imageMarked} />
          </Typography>
        </span>
      </ButtonBase>
    </div>
  );
}

We set the classes for the ButtonBase with the image class.

focusVisibleClassName has the focusVisible class.

imageBackdrop has its own class.

imageButton has the button class.

And imageMarked has the image class has the bar at the bottom.

We follow the styles from the Material UI documentation to start, and then we modify them from that.

We set many things to an absolute position to place them in the button base.

The image title is relative to the spacing.

Button Groups

We can add button groups to add buttons in one container.

For instance, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <ButtonGroup color="primary">
        <Button>foo</Button>
        <Button>bar</Button>
        <Button>baz</Button>
      </ButtonGroup>
    </div>
  );
}

We add our Button s in our ButtonGroup s to group them together.

Sizes and Colors of Button Groups

We can change the size and colors of button groups.

For example, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <ButtonGroup size="large" color="primary">
        <Button>foo</Button>
        <Button>bar</Button>
        <Button>baz</Button>
      </ButtonGroup>
    </div>
  );
}

to make the size large.

Vertical Group

To make the button group vertical, we can set the orientation to vertical .

For example, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <ButtonGroup orientation="vertical" color="primary">
        <Button>foo</Button>
        <Button>bar</Button>
        <Button>baz</Button>
      </ButtonGroup>
    </div>
  );
}

Split Button

We can make a split button by putting 2 buttons together.

For example, we can write:

import React from "react";
import Button from "@material-ui/core/Button";
import ButtonGroup from "@material-ui/core/ButtonGroup";
import { makeStyles } from "@material-ui/core/styles";
import ArrowDropDownIcon from "@material-ui/icons/ArrowDropDown";

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <ButtonGroup color="primary">
        <Button>foo</Button>
        <Button color="primary" size="small">
          <ArrowDropDownIcon />
        </Button>
      </ButtonGroup>
    </div>
  );
}

We add the ArrowDropDownIcon to add an icon to the r8ight button.

size is small to make it smaller than the left one.

Conclusion

We can make complex buttons with the ButtonBase component.

And we can group buttons together with the ButtonGroup component.

Categories
Material UI

Material UI — Buttons

Material UI is a Material Design library made for React. It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to add buttons Material Design.

Button

We can add buttons with the Button component.

For instance, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Button variant="contained">Default</Button>
      <Button variant="contained" color="primary">
        Primary
      </Button>
      <Button variant="contained" color="secondary">
        Secondary
      </Button>
      <Button variant="contained" disabled>
        Disabled
      </Button>
      <Button variant="contained" color="primary" href="#contained-buttons">
        Link
      </Button>
    </div>
  );
}

to add a variety of buttons.

We just add the Button prop with the variant to set the variant of the button.

color lets us change the color.

disabled lets us disabled a button.

href lets us add a URL for the link.

We added some spaces to the buttons with the theme.spacing in the makeStyles call.

Text Buttons

We can create text buttons to remove the outline.

For instance, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Button>Default</Button>
      <Button color="primary">Primary</Button>
      <Button color="secondary">Secondary</Button>
      <Button disabled>Disabled</Button>
      <Button href="#text-buttons" color="primary">
        Link
      </Button>
    </div>
  );
}

to add a variety of text buttons.

We removed the variant to make them text buttons.

Outlined Buttons

We can make buttons display an outline only with the variant prop.

For example, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Button variant="outlined">Default</Button>
      <Button variant="outlined" color="primary">
        Primary
      </Button>
      <Button variant="outlined" color="secondary">
        Secondary
      </Button>
      <Button variant="outlined" disabled>
        Disabled
      </Button>
      <Button variant="outlined" color="primary" href="#outlined-buttons">
        Link
      </Button>
    </div>
  );
}

to make them display an outline only.

variant is set to outline to make them that.

Handling Clicks

To handle clicks, we can pass in an event handler to the onClick prop.

For example, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Button
        onClick={() => {
          alert("clicked");
        }}
      >
        click me
      </Button>
    </div>
  );
}

Then we see the clicked alert when we click on the button.

We can add a button for uploading files.

To add one, we write:

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

const useStyles = makeStyles(theme => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  },
  input: {
    display: "none"
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <input
        accept="image/*"
        className={classes.input}
        id="button-file"
        multiple
        type="file"
      />
      <label htmlFor="button-file">
        <Button variant="contained" color="primary" component="span">
          Upload
        </Button>
      </label>
    </div>
  );
}

We hide the regular HTML file upload input with the display: 'none' .

Then we add the file input and the button in the JSX expression.

The id should match the htmlFor value. This way, we can see the file selection dialog when we click on the button.

Sizes

Buttons can have a variety of sizes.

For instance, we can write:

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

const useStyles = makeStyles(theme => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Button size="small" className={classes.margin}>
        Small
      </Button>
      <Button size="medium" className={classes.margin}>
        Medium
      </Button>
      <Button size="large" className={classes.margin}>
        Large
      </Button>
    </div>
  );
}

We use the size prop with the small , medium or large values to change the sizes of the buttons.

Conclusion

We can do many things with buttons.

The size can be changed.

We can also add outlines and fill for our buttons.

And upload button can be added.

Categories
Material UI

Material UI — Grids

Material UI is a Material Design library made for React. It’s a set of React components that have Material Design styles. In this article, we’ll look at how to create layouts Material Design.

Auto-layout

We can create auto layouts if we don’t specify a column width for our breakpoints.

For instance, we can write:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import Grid from "@material-ui/core/Grid";

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: "center",
    color: theme.palette.text.secondary
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Grid container spacing={3}>
        <Grid item xs>
          <Paper className={classes.paper}>foo</Paper>
        </Grid>
        <Grid item xs>
          <Paper className={classes.paper}>bar</Paper>
        </Grid>
        <Grid item xs>
          <Paper className={classes.paper}>baz</Paper>
        </Grid>
      </Grid>
    </div>
  );
}

to create our layout.

We have xs without any breakpoints, so we’ll let Material UI determine the sizing of the columns.

Nested Grid

We can create a nested grid with the Grid component.

For instance, we can write:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Grid from "@material-ui/core/Grid";
import Paper from "@material-ui/core/Paper";

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1
  },
  paper: {
    padding: theme.spacing(1),
    textAlign: "center",
    color: theme.palette.text.secondary
  }
}));

export default function App() {
  const classes = useStyles();

return (
    <div className={classes.root}>
      <Grid container spacing={1}>
        <Grid container item xs={3} spacing={1}>
          <Paper className={classes.paper}>foo</Paper>
        </Grid>
        <Grid container item xs={3} spacing={1}>
          <Paper className={classes.paper}>bar</Paper>
        </Grid>
        <Grid container item xs={3} spacing={1}>
          <Paper className={classes.paper}>baz</Paper>
        </Grid>
      </Grid>
    </div>
  );
}

to create a grid within a grid.

In the nested grid item, we put the Paper as the content.

xs lets us sets the column width.

spacing lets us set the spacing between the columns.

Grid List

Grid lists lets us display a collection of images in an organized grid.

For example, we can write:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import GridList from "@material-ui/core/GridList";
import GridListTile from "@material-ui/core/GridListTile";

const tileData = [
  { img: "http://placekitten.com/200/200", title: "cat" },
  { img: "http://placekitten.com/200/200", title: "cat" }
];

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexWrap: "wrap",
    justifyContent: "space-around",
    overflow: "hidden",
    backgroundColor: theme.palette.background.paper
  },
  gridList: {
    width: 500,
    height: 450
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <GridList cellHeight={160} className={classes.gridList} cols={3}>
        {tileData.map(tile => (
          <GridListTile key={tile.img} cols={tile.cols || 1}>
            <img src={tile.img} alt={tile.title} />
          </GridListTile>
        ))}
      </GridList>
    </div>
  );
}

We have the GridList component with some data inside.

We have the GridListTile to show our item.

It has an img element that displays images.

cols has the column width.

alt has the alt text.

Grid List with Title Bars

We can add the GridListTitleBar component to add the title bar to an image tile.

For instance, we can write:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import GridList from "@material-ui/core/GridList";
import GridListTile from "@material-ui/core/GridListTile";
import GridListTileBar from "@material-ui/core/GridListTileBar";
import IconButton from "@material-ui/core/IconButton";
import InfoIcon from "@material-ui/icons/Info";

const tileData = [
  { img: "[http://placekitten.com/200/200](http://placekitten.com/200/200)", title: "cat", author: "john smith" },
  { img: "[http://placekitten.com/200/200](http://placekitten.com/200/200)", title: "cat", author: "mary smith" }
];

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexWrap: "wrap",
    justifyContent: "space-around",
    overflow: "hidden",
    backgroundColor: theme.palette.background.paper
  },
  gridList: {
    width: 500,
    height: 450
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <GridList cellHeight={160} className={classes.gridList} cols={3}>
        {tileData.map(tile => (
          <GridListTile key={tile.img} cols={tile.cols || 1}>
            <img src={tile.img} alt={tile.title} />
            <GridListTileBar
              title={tile.title}
              subtitle={<span>by: {tile.author}</span>}
              actionIcon={
                <IconButton
                  aria-label={`info about ${tile.title}`}
                  className={classes.icon}
                >
                  <InfoIcon />
                </IconButton>
              }
            />
          </GridListTile>
        ))}
      </GridList>
    </div>
  );
}

We have the GridListTileBar component that takes a title prop for the title.

subtitle has the subtitle for the tile.

To include the icons, we’ve installed the @material-ui/icons package to add the icons.

Conclusion

We can create a grid with tiles to display text and images. The image can go with the title and subtitle. The layout can be automatically set.

Categories
Material UI

Getting Started with Material UI

Material UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to get started with Material Design.

Installation

We can install the package by running:

npm install @material-ui/core

with NPM or:

yarn add @material-ui/core

to install it with Yarn.

We can also add the Roboto font by writing:

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />

in our HTML.

Material design icons can be added with:

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />

We can then get started by using some components from the package.

Box

We can make our layout with the Box component.

For example, we can write:

import React from "react";
import Box from "@material-ui/core/Box";
import Button from "@material-ui/core/Button";

export default function App() {
  return (
    <Box component="span" m={1}>
      <Button>foo</Button>
    </Box>
  );
}

to add the Box component for layout with a button inside.

component specifies which component to render as.

We can use clone to call React.cloneElement to clone elements:

import React from "react";
import Box from "@material-ui/core/Box";
import Button from "@material-ui/core/Button";

export default function App() {
  return (
    <Box color="text.primary" clone>
      <Button>foo</Button>
    </Box>
  );
}

Box can also accept a render props function:

import React from "react";
import Box from "@material-ui/core/Box";
import Button from "@material-ui/core/Button";

export default function App() {
  return (
    <Box color="text.primary">{props => <Button {...props}>foo</Button>}</Box>
  );
}

Container

We can add a container with the Container component.

For instance, we can write:

import React from "react";
import Container from "@material-ui/core/Container";

export default function App() {
  return <Container maxWidth="sm">foo</Container>;
}

We can add a Container component.

maxWidth specifies the max-width of the container.

sm means it’s a small container.

It can also have a fixed size:

import React from "react";
import Container from "@material-ui/core/Container";

export default function App() {
  return <Container fixed>foo</Container>;
}

We made it fixed with the fixed prop.

Grid

We can create our own grid with the Grid component.

For instance,e we can use as follows:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Grid from "@material-ui/core/Grid";
import Paper from "@material-ui/core/Paper";

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1
  },
  paper: {
    height: 140,
    width: 200
  },
  control: {
    padding: theme.spacing(2)
  }
}));
export default function App() {
  const [spacing] = React.useState(2);
  const classes = useStyles();

  return (
    <Grid container className={classes.root} spacing={2}>
      <Grid item xs={12}>
        <Grid container justify="center" spacing={spacing}>
          {[0, 1, 2].map(value => (
            <Grid key={value} item>
              <Paper className={classes.paper} />
            </Grid>
          ))}
        </Grid>
      </Grid>
    </Grid>
  );
}

We have the Grid components.

The outer one is used as a container.

We pass in a class that’s created by makeStyles to it to set the flexbox to expand.

spacing is set to 2 to give us a margin.

In the inner Grid , we add the item prop to indicate that it’s a grid item.

justify lets us set the alignment according to the flexbox.

Then we add the Grid to display some boxes inside the container.

Fluid Grids

We can make the grid fluid with some breakpoints:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import Grid from "@material-ui/core/Grid";

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: "center",
    color: theme.palette.text.secondary
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Grid container spacing={3}>
        <Grid item xs={12} sm={3}>
          <Paper className={classes.paper}>foo</Paper>
        </Grid>
        <Grid item xs={12} sm={3}>
          <Paper className={classes.paper}>bar</Paper>
        </Grid>
        <Grid item xs={12} sm={6}>
          <Paper className={classes.paper}>baz</Paper>
        </Grid>
      </Grid>
    </div>
  );
}

We create a grid with the makeStyles function to create the styles for our grid.

We have the root class to make the grid fill the container.

Then we have some breakpoint props to let us set the width for each breakpoint.

xs is extra small and it’ll set the column size for that screen size.

sm is small and it’ll set the column size for that screen size and up.

Conclusion

We can create basic layouts with Material UI.

It’s can be fixed size or responsive.