2

I wanted to implement dark mode on my Nuxt app using tailwind and the recommended @nuxtjs/color-mdoe module. Testing tailwind's dark: classes went fine and worked as expected, however I can't make a button switcher work to set the color mode programmatically.

I installed in devDeps the module in version 3.2.0, which should be compatible with Nuxt 3, according to the docs

"@nuxtjs/tailwindcss": "^6.1.3",
"@nuxtjs/color-mode": "^3.2.0"

And applied the proper configuration in nuxt.config.ts

modules: [ '@nuxtjs/color-mode' ],
colorMode: {
    classSuffix: '',
    preference: 'system',
    fallback: 'dark'
  }

I used tailwind nuxt module In tailwind.config.js

module.exports= {
  theme: {
    colors: {
      transparent: 'transparent',
      current: 'currentColor',
      dark: '#212129',
      darkPrimary: '#00E1FF',
      darkSecondary: '#00D6D6',
      light: '#E9DAC1',
      lightPrimary: '#1d68f3',
      lightSecondary: '#00b5f0',
      main: '#0073FF',
      white: '#FFFFFF',
    },
    spacing: {
      'header': '120px',
    },
    darkMode: 'class'
  }
}

While in ./assets/css/main.css I have no meaningful config about dark mode, just some classes I defined globally

html {
  @apply transition-colors ease-in duration-1000;
  @apply bg-gradient-to-b bg-no-repeat w-screen ;
  @apply dark:from-dark/95 dark:via-dark/95 dark:to-dark dark:text-white from-white via-light/50 to-light;
}

.contain {
  @apply px-[5%] md:px-[25%];       
}

Since I wanted to place the switch in the header here's what I did in the component:

<template>
  <header class="contain py-[15px] flex items-center justify-between backdrop-blur-3xl">
    <button @click="toggleDarkMode($colorMode.preference === 'dark' ? 'light' : 'dark')">
      <nuxt-icon v-if="$colorMode.preference === 'dark'" name="sun"/>
      <nuxt-icon v-else name="moon"/>
    </button>
  </header>
</template>

<script setup>
function toggleDarkMode(theme) {
  useColorMode().preference = theme
}
</script>

The classes are actually toggling when I manually change the color mode from my os (win11) settings, but clicking the button won't replicate the same behavior. The mode seems to be switching since the icon does change accordingly.

Looking at the docs and tutorials I found elsewhere it should just work like that.
Do I need to set the mode as a global state inside the store? Should I call the hook in a root-level component?

  • hi, what do you mean when you say, classes toggling and where is your CSS or SCSS file I have exactly the same code and it's working when I press the button the root class changes to dark – sadeq shahmoradi Dec 10 '22 at 01:54
  • pls check this link https://stackblitz.com/edit/github-ac9rz7?file=README.md – sadeq shahmoradi Dec 10 '22 at 02:19
  • The demo is working perfectly fine, are you sure you don't have an extension running? https://share.cleanshot.com/ejDEUZ – kissu Dec 10 '22 at 02:27
  • Also, if that Nuxt module doesn't work well, maybe give a try to that VueUse composable used here: https://github.com/antfu/vitesse/blob/b0323be19cbd921ec5e5de35c2d8476a7e28a6ef/src/components/TheFooter.vue#L17 (`useDark`) – kissu Dec 10 '22 at 02:30
  • @sadeqshahmoradi I mean I wanted to switch mode with a button. I edited my question to post the tailwind and css files too – StefanGarofalo Dec 10 '22 at 12:02
  • @kissu I have the dark reader extension but I did disabled it on localhost. I tried useDark hook too from vueuse but to no avail – StefanGarofalo Dec 10 '22 at 12:03

1 Answers1

1

ok, there are several mistakes:

package.json

my package.json file content is this:

i think your tailwind version is 3.1.6

"devDependencies": {
    "@nuxtjs/color-mode": "^3.2.0",
    "autoprefixer": "^10.4.13",
    "nuxt": "3.0.0",
    "postcss": "^8.4.19",
    "tailwindcss": "^3.2.4"
  }

tailwind.config.js

there are mistakes in your tailwind.config.js file:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./app.vue'], // you forget content
  darkMode: 'class', //you should define darkMode here
  theme: {
    extend: {},
   //darkMode: 'class' >> this is mistake
  },
  plugins: [],
};

first, you forget the content property you should get this warn:

warn - The content option in your Tailwind CSS configuration is missing or empty.

what is content:

The content section of your tailwind.config.js file is where you configure the paths to all of your HTML templates, JavaScript components, and any other source files that contain Tailwind class names.

second, your darkMode shouldn't be the property of theme you should define it in the root

main.css

now about your main.css file you didn't add tailwind directives( but this is not necessary) :

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  body{
    @apply bg-lightPrimary dark:bg-darkPrimary;
  }
}

nuxt.config.ts

Finally, your nuxt.config.ts should be this:

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
  modules: ['@nuxtjs/color-mode'],
  colorMode: {
    classSuffix: '',
    preference: 'system',
    fallback: 'dark',
  },
  css: ['/assets/css/main.css'],
  postcss: {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    },
  },
});

I copied your codes and tested them I think your main mistake is that you define darkMode property in the wrong place:

PLS CHECK THIS LINK

summary

  1. check tailwind installation here
  2. add darkMode: 'class' correctly
  3. check content property (read more here)
  4. make sure you added main.css file to nuxt.config.ts globally

these two questions can help you too:

Install tailwind in nuxt3

better dark mode configuration

sadeq shahmoradi
  • 1,395
  • 1
  • 6
  • 22
  • 1
    I was indeed the 'darkMode' property of tailwind's config in the wrong place (theme). Such a silly mistake, thank you so much ahahah. Anyway all the missing configs you saw in my snippets were due to me using the tailwind's nuxt official module, which should have made it easier actually. Therefore no content property, in fact my custom colors and other tailwind's classes were all working correctly. Thanks a lot <3 – StefanGarofalo Dec 11 '22 at 18:18
  • 1
    Happy to help my friend – sadeq shahmoradi Dec 11 '22 at 18:20