Using cookies for dark mode theme selection in JavaScript

Dark mode themes have become increasingly popular on websites and applications, providing a sleek and easy-on-the-eyes user interface. One common way to implement dark mode is by using cookies to remember the user’s preference and apply the chosen theme on subsequent visits.

What are cookies?

Cookies are small text files that are stored on a user’s device by websites they visit. They hold data that can be accessed by the website or application to remember user preferences, track user behavior, and perform other useful tasks.

Implementing dark mode with cookies

To implement dark mode using cookies in JavaScript, follow these steps:

  1. Detect the user’s preference: Add a toggle button or any user interface element to allow the user to switch between light mode and dark mode. When the user changes the theme, capture their preference using JavaScript.
const changeTheme = () => {
  // Check if the current theme is dark or light
  const isDarkMode = document.body.classList.contains('dark-mode');

  // Toggle the theme class on the body element
  document.body.classList.toggle('dark-mode', !isDarkMode);

  // Save the preference in a cookie
  document.cookie = `theme=${isDarkMode ? 'light' : 'dark'}; expires=Sun, 31 Dec 2023 12:00:00 UTC; path=/`;
};

// Attach the changeTheme function to the toggle button's click event
document.getElementById('theme-toggle').addEventListener('click', changeTheme);
  1. Apply the stored preference: When the user visits the website again, retrieve the stored preference from the cookie and apply the appropriate theme.
const applyThemePreference = () => {
  // Retrieve the theme preference from the cookie
  const cookies = document.cookie.split(';').map(cookie => cookie.trim());
  const themeCookie = cookies.find(cookie => cookie.startsWith('theme='));
  const themePreference = themeCookie ? themeCookie.split('=')[1] : '';

  // Apply the stored theme preference
  document.body.classList.toggle('dark-mode', themePreference === 'dark');
};

// Call the applyThemePreference function on page load or DOM ready event
document.addEventListener('DOMContentLoaded', applyThemePreference);

Benefits of using cookies for dark mode

Conclusion

By using cookies to store and apply the user’s dark mode theme preference, you can offer a seamless and personalized experience to your website or application users. It’s a straightforward and effective method that ensures their preference is remembered across sessions. So go ahead and implement dark mode using cookies in your JavaScript project to enhance user experience and stay on-trend.

#webdevelopment #javascript #darkmode #cookies