I have implemented a dark mode feature on a personal project I have been working on. I used js-cookie to set a cookie called darkModeEnabled to change the styling of the page. However, when I am dark mode and do a page refresh I get a little bit of FOUC. I have tried using the v-cloak directive but that was a no go. Below is the nuxt.congif.ts file I am working with:
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true , extractCSS: true},
css: ["~/assets/css/tailwind.css"],
modules: ['@pinia/nuxt'],
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
},
app: {
head: {
title: 'My awesome websote title',
htmlAttrs: { lang: 'en'},
meta: [
{ name: 'description', content: 'My awesome website'}
],
link: [
{ rel: 'stylesheet', href: 'https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined'},
{ rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Rubik'}
],
script: [{children: `
import Cookies from './node_modules/js-cookie/dist/js.cookie.mjs'
if (Cookies.get('darkModeEnabled') === 'true'){
document.documentElement.classList.add('dark')
} else {
Cookies.remove('darkModeEnabled')
document.documentElement.classList.remove('dark')
}
`, type:'module'},]
}
}
});
below is the tailwind.css file
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
html{
background-color: #D8DEE9;
tab-size: 4;
scroll-behavior: smooth;
font-family: Rubik;
transition: 400ms;
visibility: visible;
}
html.dark {
background-color: #242933;
tab-size: 4;
scroll-behavior: smooth;
font-family: Rubik;
transition: 400ms;
visibility: visible;
}