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

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

๐Ÿ‘‹ Kindness is contagious

If you found this post helpful, please leave a โค๏ธ or a friendly comment below!

Okay