Dark Mode Switch With Tailwind CSS & Nuxt 3
I am currently rewriting my portfolio website with Nuxt 3 which is still in beta. In this article, I want to show you how I implemented a dark mode switch in Nuxt 3 using Tailwind CSS that I will use in my new portfolio website.
Create Nuxt 3 project
To create a new Nuxt 3 project, we need to run this command in our terminal:
Add Tailwind CSS 3
Next, we add the nuxt/tailwind module, which provides a prerelease version that supports Nuxt 3 and Tailwind CSS v3:
Then we need to add this module to the buildModules
section in nuxt.config.js
:
Now, we can create the Tailwind configuration file tailwind.config.ts
by running the following command:
Let's add a basic CSS file at ./assets/css/tailwind.css
(see official docs for further configuration options):
We define two CSS classes for the dark and light theme. CSS variables (indicated by --
) are used to change CSS values based on the selected theme dynamically.
Therefore, we need to define these colors in our tailwind.conf.js
:
Implement Theme Switch
Let's start to implement a theme switch by adding this simple template to our app.vue
component:
On the div
container element, we dynamically set theme-light
or theme-dark
CSS class based on the reactive darkMode
variable value, which we will implement later in the script
part of the component.
The h1
and container div
elements use our Tailwind CSS classes bg-themeBackground
and text-themeText
to use theme-specific colors for the background and text color.
Additionally, we use the Vue 3 Toggle library to switch between our themes.
Let's take a look at the script
part of app.vue
:
We store the selected theme value in Local Storage and use useState to define a reactive variable called darkMode
:
If the component is mounted, we first detect if the user has requested light or dark color theme by using the CSS media feature "prefers-color-scheme":
Then we set the theme value based on the local storage value:
This the complete app.vue
component code:
Now we can use run the following command to start our Nuxt app in development mode:
Finally, we can test our dark mode theme switch at http://localhost:3000
:
StackBlitz Demo
My simple demo is available as interactive StackBlitz demo:
Alternative
Alternatively, you could also use the color-mode module that supports Nuxt Bridge and Nuxt 3 or useDark from VueUse.
Conclusion
This article showed you how to create a simple dark mode switch in Nuxt 3 with Tailwind CSS v3. You can expect more Nuxt 3 posts in the following months as I plan to blog about interesting topics that I discover while I rewrite my portfolio website.
If you liked this article, follow me on Twitter to get notified about new blog posts and more content from me.
Alternatively (or additionally), you can also subscribe to my weekly Vue.js newsletter.
Building a Vue 3 Desktop App With Pinia, Electron and Quasar
Recently, I planned to rewrite my "Scrum Daily Standup Picker" Electron application in Vue 3. I wrote the initial release in Angular, but I wanted to refactor the code base and rewrite it in Vue 3.
Chrome Recorder: Record, Replay and Measure User Flows
Typically, a user needs to process multiple pages or steps to finish his journey, such as submitting an order or completing a registration. If we as developers need to develop one of the last pages of this user flow, we need to manually process all the previous pages/steps every time we refresh the page or need to restart the flow.