Skip to main content
Quick Reference

Material UI Cheatsheet

Material UI on one page — the theme, the sx prop, Box, Stack, and Grid, buttons and text fields, dialogs, typography and icons, slots, and the traps that show up in interviews.

10 sections 10 tables 16 code snippets

Install & wrap the app

Packages

npm install @mui/material @emotion/react @emotion/styled
npm install @mui/icons-material

One provider at the root

const theme = createTheme({
  palette: { mode: "light", primary: { main: "#007FFF" } },
});

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <Button variant="contained">Save</Button>
    </ThemeProvider>
  );
}

What you actually added

PackageRole
@mui/materialThe components. Button, TextField, Box, Dialog
@emotion/react and @emotion/styledThe default style engine. Both are required
@mui/icons-materialIcons. Each icon is its own import
ThemeProviderPuts your theme on React context. Without it, components use the default theme
CssBaselineRemoves the body margin, sets border-box, and paints the theme background

This page is Material UI v6 and v7. makeStyles and @mui/styles are the v4 API. New code uses sx, styled, and the theme.

Theme

A small theme

const theme = createTheme({
  palette: {
    mode: "light",
    primary: { main: "#007FFF" },
    secondary: { main: "#9c27b0" },
  },
  shape: { borderRadius: 10 },
  spacing: 8,
});

Names you will look up

PathMeaning
palette.primary.mainThe brand color. .light, .dark, and .contrastText are generated
palette.modelight or dark. CssBaseline follows it
palette.text.secondaryThe quieter text color
palette.background.paperCard and dialog surface
spacingDefault step is 8px. theme.spacing(2) is 16
breakpointsxs 0, sm 600, md 900, lg 1200, xl 1536

Read the theme with useTheme() inside the provider. A color such as "primary.main" in sx is a theme path, not a CSS color name.

The sx prop

One-off styles

<Box
  sx={{
    p: 2,
    mt: { xs: 1, md: 3 },
    display: "flex",
    gap: 2,
    bgcolor: "background.paper",
    color: "text.primary",
    borderRadius: 1,
  }}
>
  <Button variant="contained">Save</Button>
</Box>

A reused piece

const Pad = styled(Paper)(({ theme }) => ({
  padding: theme.spacing(2),
  borderRadius: theme.shape.borderRadius,
}));

Which tool

ToolWhen
sxStyles for this one element. It wins over styled on the same component
styledA component you will render in many places
System propsp, mt, and display on Box. Same scale as sx
p: 2Two spacing steps. With the default theme, 16px
mt: { xs: 1, md: 3 }A different value from the md breakpoint up
Callback formsx={(theme) => ({ color: theme.palette.primary.main })} when the path string is not enough

Box, Stack & Grid

Row, then a grid

<Stack direction={{ xs: "column", sm: "row" }} spacing={2}>
  <Button variant="contained">Save</Button>
  <Button variant="outlined">Cancel</Button>
</Stack>

<Grid container spacing={2} sx={{ width: "100%" }}>
  <Grid size={{ xs: 12, md: 6 }}>
    <Paper sx={{ p: 2 }}>One</Paper>
  </Grid>
  <Grid size={{ xs: 12, md: 6 }}>
    <Paper sx={{ p: 2 }}>Two</Paper>
  </Grid>
</Grid>

Layout pieces

ComponentUse
BoxA div with system props. component="section" changes the tag
StackFlex plus gap. direction and spacing are the props you want
Grid containerThe parent. spacing belongs here
size={{ xs: 12, md: 6 }}The current Grid. v7 imports it from @mui/material/Grid. v6 calls it Grid2
sx={{ width: "100%" }}A v6/v7 container does not stretch on its own. The old Grid did
<Grid item xs={12}>The v5 Grid. Those props are not the v7 API
ContainerA centered column with a max width

Buttons & text fields

The three button looks

<Button variant="text">Text</Button>
<Button variant="outlined">Outlined</Button>
<Button variant="contained" color="primary">
  Save
</Button>
<Button variant="contained" disabled>
  Wait
</Button>

A controlled field

const [title, setTitle] = useState("");

<TextField
  label="Title"
  value={title}
  onChange={(event) => setTitle(event.target.value)}
  fullWidth
  required
  error={title.length === 0}
  helperText={title.length === 0 ? "Title is required" : " "}
/>

Props that change the look

PropEffect
variant on Buttontext is the default. contained is the filled one
variant on TextFieldoutlined is the default. Also filled and standard
colorprimary, secondary, error, warning, info, success
href on ButtonRenders an anchor. Use this for links, or the router Link via component
value + onChangeControlled. Keep value a string once you start, or React warns
helperText=" "A blank line reserves the error space so the form does not jump

Dialog, Alert & Snackbar

A dialog you open yourself

<Dialog open={open} onClose={close}>
  <DialogTitle>Delete book</DialogTitle>
  <DialogContent>This cannot be undone.</DialogContent>
  <DialogActions>
    <Button onClick={close}>Cancel</Button>
    <Button color="error" variant="contained" onClick={confirm}>
      Delete
    </Button>
  </DialogActions>
</Dialog>

A toast

<Snackbar
  open={saved}
  autoHideDuration={4000}
  onClose={close}
>
  <Alert severity="success" onClose={close}>
    Saved
  </Alert>
</Snackbar>

State owns the visibility

FactWhy it matters
open is yoursDialog and Snackbar do not toggle themselves
onClose on DialogBackdrop click and Escape call it. Ignore the reason when you still need to confirm
onClose on SnackbarThe timer and click-away call it. The Alert close button needs its own onClose
PortalThe dialog DOM is under document.body. Parent overflow does not clip it
severitysuccess, info, warning, error. It sets color and the default icon

Typography & icons

Style and tag are separate

<Typography variant="h1" component="h2" gutterBottom>
  Books
</Typography>
<Typography variant="body2" color="text.secondary">
  12 titles
</Typography>

An icon on a button

import SaveIcon from "@mui/icons-material/Save";

<Button variant="contained" startIcon={<SaveIcon />}>
  Save
</Button>

Type rules

ChoiceResult
variantThe look: h1 through h6, body1, body2, button, caption
componentThe real HTML tag. An h1 look can be an h2 in the outline
color="text.secondary"A theme path. A CSS color also works
gutterBottomAdds the theme margin under the heading
Icon import@mui/icons-material/Save, not a barrel of every icon
RobotoRecommended, not included. Load it yourself or set theme.typography.fontFamily

App bar & links

A top bar

<AppBar position="static" color="primary">
  <Toolbar>
    <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
      Shop
    </Typography>
    <Button color="inherit" href="/books">Books</Button>
  </Toolbar>
</AppBar>

Bar choices

PropEffect
position="static"In normal flow. The page does not slide under it
position="fixed"Stuck to the viewport. Add a Toolbar spacer, or content hides underneath
color="inherit"On a colored bar, the button takes the bar’s text color
Button hrefA plain anchor
Router link<Button component={RouterLink} to="/books">. component swaps the element
Tabs / TabA value you control: value and onChange

Slots

Reach an inner element

<TextField
  label="Title"
  slotProps={{
    input: { "aria-label": "Book title" },
    formHelperText: { sx: { minHeight: 20 } },
  }}
/>

The rename

CurrentOlder name you still see
slotPropscomponentsProps, and on TextField also InputProps
slotscomponents. Use it to replace an inner component
slotProps.htmlInputThe real <input>. slotProps.input is the outlined wrapper
slots.rootReplace the outermost element when component is not enough

Date pickers, charts, and Data Grid are MUI X (@mui/x-date-pickers, @mui/x-data-grid). They are not in @mui/material.

Interview traps

Traps worth memorising

TrapWhat happensPrefer
Custom theme, no ThemeProviderEvery component stays on the default paletteWrap the tree once, above the routes
makeStylesThat package is the v4 enginesx or styled
v5 item and xs on a v7 GridThe props are ignored or typed as errorssize={{ xs: 12, md: 6 }}
spacing on a Grid childNothing happens. Spacing is the container’s gapspacing on container
useMediaQuery during SSRServer and client disagree, then React warnsA breakpoint inside sx for layout
value={undefined} then value=""The field flips from uncontrolled to controlledStart at "" and stay there
Dialog open with no onCloseEscape and the backdrop do nothing usefulSet state in onClose
Importing every icon from the barrelThe bundle includes the whole icon setA path import, @mui/icons-material/Save
Typography variant="h1" as the only headingThe page outline can skip or repeat levelscomponent set to the real heading level
Expecting a DatePicker in @mui/materialThere is no such exportMUI X date pickers
Next.js App Router and EmotionStyles missing on the first paintThe cache provider from @mui/material-nextjs
sx and a CSS file on the same classsx usually wins, so the file looks brokenOne place for that property

Breakpoint in sx

<Box sx={{ p: { xs: 1, md: 3 } }} />

Keep the field controlled

const [title, setTitle] = useState("");

How to use this page: install the three packages, wrap the root in ThemeProvider and CssBaseline, and render one contained button. Then put two Paper blocks in a Grid with size, and open a Dialog from a second button.