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.
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:
Store theme preference in localStorage.
Toggle dark mode using CSS classes.
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;
πΉ CSS for Dark Mode:
body.dark-mode {
background-color: #121212;
color: #ffffff;
}
π 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:
Create a Theme Service to handle mode switching.
Store the user preference in localStorage.
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);
}
}
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();
}
}
styles.css
body.dark-mode {
--bg-color: #121212;
--text-color: #ffffff;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
}
π 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:
Store the theme preference in localStorage.
Use Vueβs reactive state to track theme changes.
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>
π 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! π
Top comments (0)