Theming
Dark Mode Configuration in Next.js (App Router)
In this example, the dark mode support is implemented using the `next-themes` library. The `ThemeProvider` component is used to define the default theme and allow users to toggle between themes based on their preference.
First, install the next-themes
library to manage dark mode:
The following example shows how to integrate dark mode in a Next.js App Router project. The ThemeProvider
component is wrapped around the app’s layout, and themes are managed via the class
attribute.
To ensure that dark mode works smoothly with Tailwind CSS, set the darkMode
option to class
in your tailwind.config.js
:
You can add a button in your app to switch themes using the next-themes
hook:
You can add the theme toggle button to your Navbar
component:
With this setup, your application now fully supports dark mode. Users can toggle between themes, and their preferences will be saved in their browser.
This guide is tailored for the App Router in Next.js. For other frameworks or the Pages Router, you can adapt similar steps.