next-themes library, which provides excellent support for server-side rendering and prevents hydration mismatches.
Prerequisites
Before starting, ensure you have:- A Next.js 13+ application with App Router
- EoN UI components installed
- Basic understanding of React Context and client components
The
next-themes library is specifically designed for Next.js and handles the complexities of SSR, preventing flash of unstyled content and hydration issues.Implementation Steps
Install next-themes
First, install the This package handles theme persistence, system preference detection, and SSR compatibility automatically.
next-themes package which provides a robust theme management solution for Next.js applications.Create Theme Provider Component
Create a client component that wraps the This wrapper component allows you to:
next-themes provider. This component will manage the theme state across your application.components/theme-provider.tsx
The
"use client" directive is required because theme management involves browser APIs like localStorage and window.matchMedia.- Keep the provider logic isolated
- Add custom configuration if needed
- Maintain type safety with TypeScript
Configure Root Layout
Wrap your application with the theme provider in the root layout. This ensures all components have access to the theme context.
app/layout.tsx
Configuration Options
attribute="class": Applies theme as a CSS class on the HTML element (required for Tailwind’s dark mode)defaultTheme="system": Uses the system preference as the default themeenableSystem: Enables automatic system preference detectiondisableTransitionOnChange: Prevents CSS transitions during theme changes for instant switchingsuppressHydrationWarning: Prevents React hydration warnings caused by the theme script
Add Mode Toggle Component
Create a user interface component that allows users to switch between light, dark, and system themes. You can use any EoN UI component pattern for this.Here’s an example using a dropdown menu:
components/mode-toggle.tsx
Using the Mode Toggle
Place the mode toggle component anywhere in your application, typically in the header or navigation:components/header.tsx
Using the Theme in Your Components
Once configured, you can access the current theme in any client component:Styling Components for Dark Mode
EoN UI components automatically support dark mode when using Tailwind CSS. Use thedark: variant to apply styles in dark mode:
Troubleshooting
Flash of unstyled content on page load
Flash of unstyled content on page load
Ensure you’ve added
suppressHydrationWarning to the <html> tag in your root layout. This is required because next-themes injects a script that runs before React hydration.Theme not persisting across page refreshes
Theme not persisting across page refreshes
Check that:
- The
ThemeProvideris wrapping your entire application in the root layout - localStorage is accessible in your environment (not blocked by privacy settings)
- You’re not clearing localStorage elsewhere in your application
Hydration mismatch errors
Hydration mismatch errors
This usually happens when trying to render theme-dependent content during SSR. Use the
mounted state to prevent rendering until after hydration:Advanced Configuration
Custom Storage Key
Change where the theme preference is stored:Multiple Themes
Support more than just light and dark:Disable Transitions
Prevent CSS transitions during theme changes:Next Steps
- Explore EoN UI components to see dark mode styling in action
- Learn about customizing themes for your brand
- Check out accessibility best practices for dark mode
