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 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>
);
} This page is Material UI v6 and v7. makeStyles and @mui/styles are the v4 API. New code uses sx, styled, and the theme. A small theme const theme = createTheme({
palette: {
mode: "light",
primary: { main: "#007FFF" },
secondary: { main: "#9c27b0" },
},
shape: { borderRadius: 10 },
spacing: 8,
}); 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. 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,
})); 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> 💬 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 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> 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> Reach an inner element <TextField
label="Title"
slotProps={{
input: { "aria-label": "Book title" },
formHelperText: { sx: { minHeight: 20 } },
}}
/> Date pickers, charts, and Data Grid are MUI X (@mui/x-date-pickers, @mui/x-data-grid). They are not in @mui/material. 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. |