React MUI Dark mode

Dark Mode lets users change the theme color of the website/mobile apps to black or a shade closer to black. Material-UI provides a customizable Dark Mode theme where you can customize your background color according to your need.

Syntax:

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

const darkTheme = createTheme({
  palette: {
    mode: 'dark',
  },
});

function App() {
  return (
    <ThemeProvider theme={darkTheme}>
      <CssBaseline />
      <h1> Dark Mode</h1>
    </ThemeProvider>
  );
}
export default App;

Installing React App::

    npx create-react-app dark-mode-react

cd  dark-mode-react

Installing Material-UI:   To install Material-UI in your project run the command below using npm:

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

or using yarn:

yarn add @mui/material @emotion/react @emotion/styled

Now we need some content on our webpage to see the dark mode effect so, let’s first add some stuff.

Step1:  Create new file AppBar.js  in the src folder of your project and import the basic App bar components from the Material Ui website.

//AppBar.js

import * as React from 'react';
import AppBar from '@mui/material/AppBar';
import Box from '@mui/material/Box';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import Button from '@mui/material/Button';


export default function ButtonAppBar() {
  return (
    <Box sx={{ flexGrow: 1 }}>
      <AppBar position="static">
        <Toolbar>
          <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
            News
          </Typography>
          <Button color="inherit">Login</Button>
        </Toolbar>
      </AppBar>
    </Box>
  );
}

Example:

Step2:   Import AppBar.js in the App.js  file and then run the app.

import * as React from "react";
import ButtonAppBar from "./AppBar";
function App() {
  return (
      <div className="App">
        <ButtonAppBar/>
      </div>
  );
}
export default App;

Output:

Step3:  Now it’s time to create a switch that toggles from light to dark and dark to light.

Update the AppBar.js by replacing the button components to switch components which requires two imports.

import Switch from '@mui/material/Switch';
const label = { inputProps: { 'aria-label': 'Switch demo' } };

//<Button color="inherit">Login</Button> 
<Switch {...label}/>

Here, we make a card to add to our webpage, it makes a pretty cool webpage and looks like a website.

Example:

     

                    
//Card.js

import * as React from 'react';
import Card from '@mui/material/Card';
import CardActions from '@mui/material/CardActions';
import CardContent from '@mui/material/CardContent';
import CardMedia from '@mui/material/CardMedia';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';
import img from 'https://media.geeksforgeeks.org/wp-content/uploads/20220930110109/download-200x200.png'
export default function MediaCard() {
  return (
    <Card sx={{ maxWidth: 500, marginTop:5, marginLeft:70 }}>
      <CardMedia
        component="img"
        height="140"
        image={img}
        alt="green iguana"
      />
      <CardContent>
        <Typography gutterBottom variant="h5" component="div">
          Lizard
        </Typography>
        <Typography variant="body2" color="text.secondary">
          Lizards are a widespread group of squamate reptiles, with over 6,000
          species, ranging across all continents except Antarctica
        </Typography>
      </CardContent>
      <CardActions>
        <Button size="small">Share</Button>
        <Button size="small">Learn More</Button>
      </CardActions>
    </Card>
  );
}

Step4: Now, we have to make some changes in our App.js file. First of all, we have to make some imports.

import { ThemeProvider, createTheme } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import { useState } from "react";

const [darkMode, setDarkMode] = useState(false)
  const darkTheme = createTheme({
    palette: {
      mode: darkMode?"dark":"light",
    },
  });

//App.js

import "./App.css";
import { useState } from "react";
import ButtonAppBar from "./AppBar";
import * as React from "react";
import MediaCard from "./Card";
import { ThemeProvider, createTheme } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";



function App() {
  
  const [darkMode, setDarkMode] = useState(true)
  const darkTheme = createTheme({
    palette: {
      mode: darkMode?"dark":"light",
    },
  });
  return (
    <ThemeProvider theme={darkTheme}>
      <CssBaseline />
      <div className="App">
        <ButtonAppBar check={darkMode} change={()=>setDarkMode(!darkMode)}/>
        <h1>Dark Mode</h1>
        <MediaCard />
      </div>
    </ThemeProvider>
  );
}

export default App;

Step5: It’s time to update AppBar.js firstly we have to get props like checked and change from App.js  and pass them to the Switch components. 

//AppBar.js


import * as React from 'react';
import AppBar from '@mui/material/AppBar';
import Box from '@mui/material/Box';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import Switch from '@mui/material/Switch';
const label = { inputProps: { 'aria-label': 'Switch demo' } };


export default function ButtonAppBar({checked, change}) {
  return (
    <Box sx={{ flexGrow: 1 }}>
      <AppBar position="static">
        <Toolbar>
          <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
            Welcome to GFG
          </Typography>
          <Switch {...label} defaultChecked color="default"  onChange={change} checked={checked} />
        </Toolbar>
      </AppBar>
    </Box>
  );
}

Step6: Now, it’s all done, Let’s run the command to start React server

npm run start

You can open the app link and enjoy the dark theme on your website by clicking on the toggle button to switch between light and dark themes.

Output: