DEV Community

Cover image for πŸŒ™ Dark Mode in React, Angular & Vue – A Must-Have for Modern Apps!
DCT Technology Pvt. Ltd.
DCT Technology Pvt. Ltd.

Posted on

3 1 1 1 1

πŸŒ™ Dark Mode in React, Angular & Vue – A Must-Have for Modern Apps!

Dark mode is no longer just a "cool feature"β€”it’s an essential part of modern UI/UX.

It improves readability, reduces eye strain, and even helps save battery on OLED screens.

Image description
But how do you implement it in React, Angular, and Vue? Let’s dive in and build a seamless dark mode switcher in each of these frameworks!

🎯 Why Dark Mode?

Before we start coding, here’s why you should consider adding dark mode to your app:

βœ… User Preference: Many users prefer dark mode for extended reading.

βœ… Battery Efficiency: OLED screens consume less power in dark mode.

βœ… Modern Aesthetics: Apps with dark mode look sleek and future-ready.

βœ… Accessibility: Helps users with light sensitivity or visual impairments.

βš›οΈ Implementing Dark Mode in React

React’s useState & localStorage make it easy to toggle dark mode.

πŸ”Ή Steps:

  1. Store theme preference in localStorage.

  2. Toggle dark mode using CSS classes.

  3. Update state based on the saved preference.

πŸ”Ή React Dark Mode Code:


import React, { useState, useEffect } from "react"; 

const App = () => { 
  const [darkMode, setDarkMode] = useState( 
    localStorage.getItem("theme") === "dark" 
  ); 

  useEffect(() => { 
    document.body.classList.toggle("dark-mode", darkMode); 
    localStorage.setItem("theme", darkMode ? "dark" : "light"); 
  }, [darkMode]); 

  return ( 
    <div> 
      <button onClick={() => setDarkMode(!darkMode)}> 
        {darkMode ? "Switch to Light Mode" : "Switch to Dark Mode"} 
      </button> 
    </div> 
  ); 
}; 

export default App; 
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή CSS for Dark Mode:


body.dark-mode { 
  background-color: #121212; 
  color: #ffffff; 
} 
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Explore more React dark mode techniques: https://react.dev/

πŸ…°οΈ Implementing Dark Mode in Angular

Angular allows you to toggle dark mode using services and CSS variables.

πŸ”Ή Steps:

  1. Create a Theme Service to handle mode switching.

  2. Store the user preference in localStorage.

  3. Apply the selected theme dynamically using CSS variables.

πŸ”Ή Angular Dark Mode Code:


theme.service.ts 

import { Injectable } from '@angular/core'; 

@Injectable({ 
  providedIn: 'root' 
}) 
export class ThemeService { 
  private darkMode = false; 

  constructor() { 
    this.darkMode = localStorage.getItem("theme") === "dark"; 
    this.updateTheme(); 
  } 

  toggleTheme() { 
    this.darkMode = !this.darkMode; 
    localStorage.setItem("theme", this.darkMode ? "dark" : "light"); 
    this.updateTheme(); 
  } 

  updateTheme() { 
    document.body.classList.toggle("dark-mode", this.darkMode); 
  } 
} 

Enter fullscreen mode Exit fullscreen mode

app.component.ts


import { Component } from '@angular/core'; 
import { ThemeService } from './theme.service'; 

@Component({ 
  selector: 'app-root', 
  template: ` 
    <button (click)="toggleTheme()">Toggle Dark Mode</button> 
  ` 
}) 
export class AppComponent { 
  constructor(private themeService: ThemeService) {} 

  toggleTheme() { 
    this.themeService.toggleTheme(); 
  } 
} 

Enter fullscreen mode Exit fullscreen mode

styles.css


body.dark-mode { 
  --bg-color: #121212; 
  --text-color: #ffffff; 
} 

body { 
  background-color: var(--bg-color); 
  color: var(--text-color); 
} 

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Angular Dark Mode Best Practices: https://angular.io/guide/theming

πŸ”· Implementing Dark Mode in Vue

Vue’s reactive properties and Vuex make dark mode integration simple.

πŸ”Ή Steps:

  1. Store the theme preference in localStorage.

  2. Use Vue’s reactive state to track theme changes.

  3. Apply dark mode using CSS classes.

πŸ”Ή Vue Dark Mode Code:


<template> 
  <div :class="{ 'dark-mode': isDarkMode }"> 
    <button @click="toggleTheme"> 
      {{ isDarkMode ? "Switch to Light Mode" : "Switch to Dark Mode" }} 
    </button> 
  </div> 
</template> 

<script> 
export default { 
  data() { 
    return { 
      isDarkMode: localStorage.getItem("theme") === "dark" 
    }; 
  }, 
  methods: { 
    toggleTheme() { 
      this.isDarkMode = !this.isDarkMode; 
      localStorage.setItem("theme", this.isDarkMode ? "dark" : "light"); 
      document.body.classList.toggle("dark-mode", this.isDarkMode); 
    } 
  } 
}; 
</script> 

<style> 
.dark-mode { 
  background-color: #121212; 
  color: #ffffff; 
} 
</style> 

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Learn more about Vue theming: https://vuejs.org/guide/scaling-up/theming.html

πŸ”₯ Pro Tips for a Better Dark Mode Experience

**βœ… Use CSS Variables – **Makes it easier to switch themes dynamically.

βœ… Save User Preferences – Use localStorage to persist dark mode settings.

βœ… Provide a Toggle Button – Users should easily switch between light & dark.

βœ… Test Accessibility – Ensure proper contrast for readability.

πŸ”— Want to explore more advanced theming? Check out this CSS Dark Mode Guide.

πŸ’‘ Which Framework Handles Dark Mode Best?

βœ… Use React if you want a simple hook-based approach.

βœ… Choose Angular for enterprise apps with structured theming.

βœ… Go with Vue for a lightweight and reactive dark mode switcher.

πŸ’¬ Which framework do you prefer for dark mode?

Share your thoughts in the comments!

πŸ“’ Stay Updated with More Frontend Insights!

πŸ”” Follow DCT Technology for web development tips, design trends, and UI/UX guides! πŸš€

React #Angular #Vue #WebDevelopment #DarkMode #Frontend #UIUX #CSS #JavaScript #DevCommunity

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video πŸ“ΉοΈ

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

πŸ‘‹ Kindness is contagious

If you found this article helpful, a little ❀️ or a friendly comment would be much appreciated!

Got it