90.4k

Adding dark mode to your next app.

Install next-themes

Start by installing next-themes:

pnpm add next-themes

Create a theme provider

components/theme-provider.tsx
"use client"   import * as React from "react" import { ThemeProvider as NextThemesProvider } from "next-themes"   export function ThemeProvider({  children,  ...props }: React.ComponentProps<typeof NextThemesProvider>) {  return <NextThemesProvider {...props}>{children}</NextThemesProvider> }

Wrap your root layout

Add the ThemeProvider to your root layout and add the suppressHydrationWarning prop to the html tag.

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.