Skip to content

Instantly share code, notes, and snippets.

@Mazyod
Created March 12, 2025 08:46
Show Gist options
  • Save Mazyod/ae0c48500983558921a115912d30b7b2 to your computer and use it in GitHub Desktop.
Save Mazyod/ae0c48500983558921a115912d30b7b2 to your computer and use it in GitHub Desktop.
Material UI Theming Guide

I'll help you create a guide on enhancing your MUI React app with colors and theming. Let me search for the latest information to ensure I provide up-to-date advice.

Leveling Up Your MUI React App with Colors and Theming

Based on the latest information about Material UI, I'll guide you through simple yet effective ways to enhance your app with colors and theming. This guide focuses on minimal changes that will have maximum visual impact without getting into complex customizations.

1. Creating a Custom Theme

The foundation of any MUI styling is the theme. Let's start by setting up a custom theme with your brand color:

import { createTheme, ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';

// Define your brand color here
const brandColor = '#3f51b5'; // Example: Indigo

const theme = createTheme({
  palette: {
    primary: {
      main: brandColor,
      // Material UI will automatically generate light and dark variants
    },
    background: {
      default: '#f5f5f5', // Light gray background for your app
      paper: '#ffffff',   // White background for Paper components
    },
  },
});

function App() {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline /> {/* This normalizes styles across browsers */}
      <YourAppContent />
    </ThemeProvider>
  );
}

The CssBaseline component is important as it applies a consistent baseline of styles and fixes common browser inconsistencies.

2. Adding Background Colors to Different Sections

One of the quickest ways to add visual depth is by using different background colors for different sections of your app:

import { Box, Container, Paper } from '@mui/material';

function HomePage() {
  return (
    <Box sx={{ bgcolor: 'background.default', minHeight: '100vh' }}>
      {/* Hero section with primary color background */}
      <Box sx={{ bgcolor: 'primary.main', color: 'primary.contrastText', py: 6 }}>
        <Container>
          <h1>Welcome to My App</h1>
          <p>This section has your brand color as background</p>
        </Container>
      </Box>
      
      {/* Content section with paper background */}
      <Container sx={{ py: 4 }}>
        <Paper sx={{ p: 3, mb: 4 }}>
          <h2>Section Title</h2>
          <p>This content appears on a paper background with subtle elevation</p>
        </Paper>
        
        {/* Secondary content with light gray background */}
        <Paper sx={{ p: 3, bgcolor: 'grey.50' }}>
          <h2>Another Section</h2>
          <p>This section has a light gray background for subtle differentiation</p>
        </Paper>
      </Container>
    </Box>
  );
}

3. Using Elevation to Create Depth

Material UI's Paper component has built-in elevation that creates shadow effects. This adds visual hierarchy to your app:

<Box sx={{ display: 'flex', gap: 2, flexWrap: 'wrap', p: 2 }}>
  <Paper sx={{ p: 2, width: 200 }} elevation={0}>
    Flat (elevation 0)
  </Paper>
  <Paper sx={{ p: 2, width: 200 }} elevation={1}>
    Subtle (elevation 1)
  </Paper>
  <Paper sx={{ p: 2, width: 200 }} elevation={3}>
    Medium (elevation 3)
  </Paper>
  <Paper sx={{ p: 2, width: 200 }} elevation={6}>
    Pronounced (elevation 6)
  </Paper>
</Box>

4. Adding Subtle Color Accents

Introduce your brand color in small doses throughout the interface for a cohesive look:

<Paper 
  sx={{ 
    p: 3, 
    mb: 2,
    borderTop: 3, 
    borderColor: 'primary.main' 
  }}
>
  <h2>Section with Accent</h2>
  <p>This section has a subtle brand color accent on the top border</p>
</Paper>

<Paper 
  sx={{ 
    p: 3,
    mb: 2,
    borderLeft: 3, 
    borderColor: 'primary.main' 
  }}
>
  <h2>Side Accent</h2>
  <p>This section has a subtle brand color accent on the left</p>
</Paper>

5. Creating Visual Separation with Background Colors

Use different background colors to separate functional areas of your app:

<Box sx={{ display: 'flex', minHeight: '100vh' }}>
  {/* Sidebar with darker background */}
  <Box 
    sx={{ 
      width: 240, 
      bgcolor: 'grey.100', 
      p: 2,
      borderRight: 1,
      borderColor: 'grey.300'
    }}
  >
    Sidebar content
  </Box>
  
  {/* Main content area */}
  <Box sx={{ flexGrow: 1, bgcolor: 'background.default', p: 3 }}>
    <Paper sx={{ p: 3 }}>
      Main content goes here
    </Paper>
  </Box>
</Box>

6. Applying Color to Interactive Elements

Make interactive elements stand out with your brand color:

<Button variant="contained" color="primary">
  Primary Action
</Button>

<Button 
  variant="outlined" 
  sx={{ 
    borderColor: 'primary.main', 
    color: 'primary.main',
    '&:hover': {
      bgcolor: 'primary.light',
      borderColor: 'primary.main',
    }
  }}
>
  Secondary Action
</Button>

7. Creating Component-Specific Themes

You can create component-specific themes for consistent styles across your app:

// In your theme definition
const theme = createTheme({
  palette: {
    primary: { main: brandColor },
  },
  components: {
    // Style all Cards
    MuiCard: {
      styleOverrides: {
        root: {
          borderRadius: 8,
          boxShadow: '0 2px 8px rgba(0,0,0,0.08)',
        },
      },
    },
    // Style all Buttons
    MuiButton: {
      styleOverrides: {
        root: {
          borderRadius: 4,
          textTransform: 'none', // Prevents all-caps text
        },
      },
    },
  },
});

8. Using Color for States and Feedback

Apply colors to indicate different states or provide feedback:

<Alert severity="success" sx={{ mb: 2 }}>
  This is a success message!
</Alert>

<Alert severity="info" sx={{ mb: 2 }}>
  This is an informational message.
</Alert>

<Alert severity="warning" sx={{ mb: 2 }}>
  This is a warning message.
</Alert>

<Alert severity="error" sx={{ mb: 2 }}>
  This is an error message!
</Alert>

9. Dark Mode Support (Optional)

If you want to support both light and dark modes, you can create a theme that responds to the user's preference:

import { useState, useMemo } from 'react';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import { CssBaseline, IconButton } from '@mui/material';
import Brightness4Icon from '@mui/icons-material/Brightness4';
import Brightness7Icon from '@mui/icons-material/Brightness7';

function App() {
  const [mode, setMode] = useState('light');
  
  const theme = useMemo(() => createTheme({
    palette: {
      mode,
      primary: {
        main: brandColor,
      },
      background: {
        default: mode === 'light' ? '#f5f5f5' : '#121212',
        paper: mode === 'light' ? '#ffffff' : '#1e1e1e',
      },
    },
  }), [mode]);

  const toggleColorMode = () => {
    setMode(prevMode => prevMode === 'light' ? 'dark' : 'light');
  };

  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <IconButton onClick={toggleColorMode} color="inherit">
        {mode === 'dark' ? <Brightness7Icon /> : <Brightness4Icon />}
      </IconButton>
      <YourAppContent />
    </ThemeProvider>
  );
}

10. Best Practices for Minimal but Effective Theming

  1. Start with a single brand color - Let Material UI generate complementary shades
  2. Use elevation strategically - Create hierarchy with different shadow levels
  3. Apply color sparingly - Use your brand color as an accent, not overwhelming backgrounds
  4. Leverage background colors - Different sections can have different backgrounds for better visual separation
  5. Maintain contrast - Ensure text has sufficient contrast with background colors for readability
  6. Be consistent - Use the same color patterns throughout your app
  7. Style components globally - Use the theme's components property for app-wide styles
  8. Think about visual hierarchy - Use color and elevation to guide users' attention

Summary

By implementing these simple theming techniques, you can transform your flat MUI app into a visually appealing interface with depth and personality. The key is to use your brand color strategically as an accent, leverage different background colors for visual separation, and use elevation to create depth.

These minimal changes will make a significant impact on the visual appeal of your app without requiring extensive customization or redesign of component shapes and typography.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment