Docs
Next.js

Next.js

Adding dark mode to your next app.

Dark mode

Install next-themes

Start by installing next-themes:

npm install next-themes

Create a theme provider

components/theme-provider.tsx
'use client'
 
import { type ThemeProviderProps } from 'next-themes'
import dynamic from 'next/dynamic'
import * as React from 'react'
 
const NextThemesProvider = dynamic(
    () => import('next-themes').then((e) => e.ThemeProvider),
    {
        ssr: false,
    }
)
 
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
    return <NextThemesProvider {...props}>{children}</NextThemesProvider>
}

Wrap your root layout

Add the ThemeProvider to your root layout.

app/layout.tsx
import { ThemeProvider } from "@/components/theme-provider"
 
export default function RootLayout({ children }: RootLayoutProps) {
  return (
    <>
      <html lang="en" suppressHydrationWarning>
        <head />
        <body>
          <ThemeProvider
            attribute="class"
            defaultTheme="system"
            enableSystem
            disableTransitionOnChange
          >
            {children}
          </ThemeProvider>
        </body>
      </html>
    </>
  )
}

Add a mode toggle

Place a mode toggle on your site to toggle between light and dark mode.