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

Reinvent your career. Join DEV.

It takes one minute and is necessary in the AI era.

Get started

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay