How to Implement Dark Mode in your application in 3 easy steps

In this article, we're going to see how we can implement dark mode in our website. Before we get into the meat and potatoes of the article, lets try and understand why we might want to implement Dark Mode.

Date Posted : 13 November 21

Time to read: 6 mins


How to Implement Dark Mode in your application in 3 easy steps image

In this article, we’re going to see how we can implement dark mode on our website. Before we get into the meat and potatoes of the article, let’s try and understand why we might want to implement Dark Mode.

Why Dark Mode anyway?

Reduces Strain on the Eyes

Reduces Blue Light Exposure at night

More Users prefer it

Doesn’t Eat your Battery

{{< image title=“Dark Mode” alt=“Dark Mode” src=”https://media.giphy.com/media/MtGAE4ZabBs4g/giphy.gif” >}}

Implementation Overview

Before we begin, I’m just going to lay down the steps we’re going to follow to implement the Dark Mode

  1. Defining CSS variables for each mode
  2. Creating Elements that use colors from CSS Variables
  3. Hooking everything up with Javascript

Step 1: Defining colors for each theme

// For Normal Theme
:root {
	--primarytext: #333333;
  --secondarytext: #797779;
  --bgcolor: white;
  --greylight: #f6f6f6;
}

// For Dark Theme
[data-theme="dark"] {
	--primarytext: #ffffff;
  --secondarytext: #b0b3b8;
  --bgcolor: #18191a;
  --greylight: #232526;
}

Step 2: Creating Elements which use colors from the defined CSS Variables

Now, that we have our themes defined. Let’s go ahead and create a few HTML elements. For the sake of this article, I’m just going to use the CSS variables on the following elements

  1. Header
  2. Paragraph
  3. Card Element (Custom element that we’re going to create)
h1{
color: var(--primarytext)
}

p{
color: var(--primarytext)
}

.card{
padding:20px;
width : 200px;
height: 200px;
backgroundColor: var(--greylight)
}

As you can see from the code above, we’re using the CSS variables to add color to the header, paragraph elements, and the card class. By default, the values for the variables will be picked up from the variables defined under the root styles, but we’ll use javascript in the next step to change it to point to the variables defined under the dark theme. Let’s see how we can achieve that.

Step 3: Hooking everything up with Javascript

In this step, let’s use javascript to dynamically toggle the mode as and when a certain event is detected (example, button click). We’ll also be persisting the users’ selected mode in LocalStorage so that the next time the user opens the page, the theme is the one that the user selected before. Also, for the sake of this article, we’ll just be implementing the dark mode on the client-side. We can take this one step further by persisting the user’s selected mode in the database which is the preferred approach.

Enough talking, lets see the code now

const toggleButton = document.getElementById("toggle");

function changeTheme(mode) {
  document.documentElement.setAttribute("data-theme", mode);
}

function changeMode() {
  let currentMode = localStorage.getItem("mode");
  if (currentMode === "dark") {
    changeTheme("light");
    localStorage.setItem("mode", "light");
  }
  if (currentMode === "light") {
    changeTheme("dark");
    localStorage.setItem("mode", "dark");
  }
}

toggleButton.addEventListener("click", changeMode);

As we can see from the code above, we trigger the change mode method every time the button is clicked on. This checks the localstorage to get the current mode, the mode is then reversed. Also, note that we’re using document.documentElement.setAttribute() which changes the theme to the specified value.

To see a working example of the project you can head over to the following Codesandbox link.

Shivaraj Bakale Image

Shivaraj Bakale

Diving deep from frontend waves to the backend caves 🌊🤠

React
Redux
Redux-saga
HTML