DEV Community

Cover image for How to implement Dark Mode on a Nuxt App using Vuetify and SCSS
Ayoazeez26
Ayoazeez26

Posted on

How to implement Dark Mode on a Nuxt App using Vuetify and SCSS

Do you ever find yourself in a situation where you want to implement dark mode on your Vue app but you don't know how to go about it or you've hit a bug somewhere and you just can't make it work? I hope by the end of this article you get enough information to figure it out 🤗

Now let's dive into it 🏊🏾‍♂️🏊🏾‍♂️

Vuetify comes with two themes pre-installed, light and dark. To set the default theme of your application, use the defaultTheme option.

export default {
  // Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify
  vuetify: {
    theme: {
      dark: false,
      themes: {
        light: {
          primary: '#1F1F1F',
          secondary: '#05BAD5',
        },
        dark: {
          primary: '#F2F2F2',
          'grey-3': '#AFAFAF',
        }
      }
    }
  },
}
Enter fullscreen mode Exit fullscreen mode

Creating our toggle button

First things first, we need to create our button to toggle between light and dark mode. To do this, we're going to use a checkbox input this way:

<script setup lang="ts">
const checkbox = ref(false);
</script>
<template>
  <div class="mr-4">
    <input id="checkbox" v-model="checkbox" type="checkbox" class="checkbox">
    <label for="checkbox" class="checkbox-label">
      <img width="20" src="/svg/sun.svg" alt="sun icon">
      <img width="20" src="/svg/moon.svg" alt="moon icon">
      <span class="ball"></span>
    </label>
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

Next, we have to style the checkbox to make the user feel like they are toggling a switch, to achieve this, we add this styles to to our css


.checkbox {
  opacity: 0;
  position: absolute;
}

.checkbox-label {
  background-color: #fff;
  width: 96px;
  height: 40px;
  border-radius: 50px;
  position: relative;
  padding: 4px 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.checkbox-label .ball {
  background-color: #1f1f1f;
  width: 30px;
  height: 30px;
  position: absolute;
  left: 8px;
  top: 5px;
  border-radius: 50%;
  transition: transform 0.2s linear;
}

.checkbox:checked + .checkbox-label {
  background-color: #121212;
}

.checkbox:checked + .checkbox-label .ball {
  transform: translateX(48px);
  background-color: #f2f2f2;
}


Enter fullscreen mode Exit fullscreen mode

The resulting checkbox should look like this

Checkbox Light Mode

And like this when toggled

Checkbox Dark Mode

To set the checkbox's v-model value to toggle between dark and light mode, we will set a watcher on the checkbox value and use it to toggle vuetify's dark mode state.
co

<script setup lang="ts">
import { useTheme } from 'vuetify';

const theme = useTheme();
const checkbox = ref(false);

watch(checkbox, (value) => {
  theme.global.name.value = value ? 'dark' : 'light';
});
</script>

Enter fullscreen mode Exit fullscreen mode

By default, the checkbox's value, and vuetify's dark theme value, is false. With what we've done so far, clicking on the toggle button would change the checkbox's value to true, at the same time change the vuetify dark theme value to true, and it changes back to false if we click the button one more time.

Top comments (0)