nuxt-color-mode not running. Returning undefined

Viewed 39

Installed nuxt-color-mode following their official Docs. Though, when loading the page I'm getting Cannot read properties of undefined (reading 'value')

npm install --save-dev @nuxtjs/color-mode

Added to nuxt.config.js

buildModules: [
  // https://go.nuxtjs.dev/tailwindcss
  '@nuxtjs/tailwindcss',
  '@nuxtjs/color-mode'
],

Then within index.vue I've added:

<script>
export default {
  name: 'IndexPage',
  colorMode: 'light',
}
</script>

Unsure why the following line is causing an error: <h1>Color mode: {{ $colorMode.value }}</h1> it's also not adding a class to the <html> element.

Have I done something wrong here?

1 Answers

You'll need to downgrade to the latest v2 of the package aka "@nuxtjs/color-mode": "^2.1.1". It looks like the v3 is not properly working with Nuxt2.


The following template works well

<template>
  <div>
    <h1>Color mode: {{ $colorMode.value }}</h1>
    <select v-model="$colorMode.preference">
      <option value="system">System</option>
      <option value="light">Light</option>
      <option value="dark">Dark</option>
      <option value="sepia">Sepia</option>
    </select>
  </div>
</template>

<style>
body {
  background-color: #fff;
  color: rgba(0, 0, 0, 0.8);
}
.dark-mode body {
  background-color: #091a28;
  color: #ebf4f1;
}
.sepia-mode body {
  background-color: #f1e7d0;
  color: #433422;
}
</style>

With

buildModules: ['@nuxtjs/color-mode']

and ssr: true in nuxt.config.js.

Related