Skip to main content
Version: 1.0.0

ThemeProvider Component

The ThemeProvider component is a crucial part of theme management in your Next.js application. It wraps the NextThemesProvider from the 'next-themes' library, allowing you to easily implement theme switching functionality throughout your app.

Importing the Component

To use the ThemeProvider in your Next.js application, import it as follows:

import { ThemeProvider } from '@/components/ThemeProvider';

Basic Usage

Wrap your application or a part of it with the ThemeProvider:

import { ThemeProvider } from '@/components/ThemeProvider';

export default function App({ Component, pageProps }) {
return (
<ThemeProvider attribute='class' defaultTheme='system' enableSystem>
<Component {...pageProps} />
</ThemeProvider>
);
}

Key Points:

  1. The "use client" directive indicates that this is a client-side component.
  2. It imports React and the NextThemesProvider from 'next-themes'.
  3. The component is a simple wrapper around NextThemesProvider, passing through all props and children.

Props

The ThemeProvider accepts all props that the NextThemesProvider from 'next-themes' accepts. Some commonly used props include:

  • attribute (string): The attribute to apply to the HTML element. Default is data-theme.
  • defaultTheme (string): The default theme to use if no theme is set. Default is 'light'.
  • enableSystem (boolean): Whether to switch between dark and light based on system preferences. Default is false.
  • storageKey (string): The key to use for storing theme preference in localStorage. Default is 'theme'.

For a full list of props, refer to the next-themes documentation.

Functionality

The ThemeProvider component enables theme management across your application. It:

  1. Provides a context for theme-related information.
  2. Handles theme switching and persistence.
  3. Allows for system preference detection when enabled.

Best Practices

  1. Place the ThemeProvider at the root of your application to ensure theme consistency across all components.
  2. Use the attribute="class" prop to apply themes using Tailwind CSS classes.
  3. Enable system preference detection with enableSystem for better user experience.

Example: Advanced Usage

Here's an example of how you might use ThemeProvider with custom options:

import { ThemeProvider } from '@/components/ThemeProvider';

export default function App({ Component, pageProps }) {
return (
<ThemeProvider
attribute='class'
defaultTheme='system'
enableSystem
disableTransitionOnChange
themes={['light', 'dark', 'custom']}
>
<Component {...pageProps} />
</ThemeProvider>
);
}

In this example, we're:

  • Using class attribute for theme application (works well with Tailwind CSS)
  • Setting the default theme to follow the system preference
  • Enabling system theme detection
  • Disabling transitions when the theme changes to prevent flickering
  • Defining custom theme options

Accessibility

The ThemeProvider enhances accessibility by:

  • Allowing users to choose their preferred color scheme
  • Supporting system-level preferences when enableSystem is true

Troubleshooting

  • If themes are not applying correctly, ensure the attribute prop matches your CSS implementation (e.g., class for Tailwind CSS).
  • For SSR applications, you might see a flash of incorrect theme. Use the forcedTheme prop on specific pages to mitigate this.

By using the ThemeProvider component, you can easily implement robust theme management in your Next.js application, providing a better user experience with theme customization options.