DEV Community

Sachin
Sachin

Posted on

2

Dark Mode In React (vite)

**create a fresh folder
run the commands npm create vite@latest (in the folder directory)
also cd to the directory and run the command npm install**
Enter fullscreen mode Exit fullscreen mode
step 1
`Code in App.jsx`
import React, { useState } from 'react';
import { FaSun, FaMoon } from 'react-icons/fa'; // Import Sun and Moon icons
import './App.css';

function App() {
  const [darkMode, setDarkMode] = useState(false);

  // Toggle dark mode
  const toggleTheme = () => {
    setDarkMode(!darkMode);
  };

  return (
    <div className={darkMode ? 'dark' : 'light'}>
      <div className="container">
        <h1>{darkMode ? 'Dark Mode' : 'Light Mode'}</h1>
        <button className="toggle-button" onClick={toggleTheme}>
          {darkMode ? <FaMoon className="icon" /> : <FaSun className="icon" />}
          {darkMode ? ' Dark Mode' : ' Light Mode'}
        </button>
      </div>
    </div>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode
Code in App.css
`body {
    margin: 0;
    font-family: Arial, sans-serif;
  }

  .container {
    text-align: center;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  /* Light Mode Styles */
  .light {
    background-color: #f9f9f9;
    color: #333;
  }

  /* Dark Mode Styles */
  .dark {
    background-color: #333;
    color: #f9f9f9;
  }

  /* Toggle Button Styles */
  .toggle-button {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    background-color: #007bff;
    color: white;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;
  }

  .toggle-button:hover {
    background-color: #0056b3;
    transform: scale(1.05);
  }

  .icon {
    font-size: 20px;
  }`
Enter fullscreen mode Exit fullscreen mode

Final Output

Image description

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more