📌 Next.js + Tailwind CSS ile Dinamik Tema (Light/Dark Mode)

1️⃣ next-themes Kütüphanesi

npm install next-themes

2️⃣ ThemeProvider ile Uygulamayı Sarmalama

// app/layout.jsx
import { ThemeProvider } from 'next-themes'

export default function Layout({ children }) {
  return (
    <html suppressHydrationWarning>
      <head />
      <body>
        <ThemeProvider>{children}</ThemeProvider>
      </body>
    </html>
  )
}

✅ Böylece tüm sayfalar ve bileşenler, mevcut tema bilgisini useTheme hook’u ile alabilir.


3️⃣ useTheme Hook'u ile Tema Değiştirme

import { useTheme } from 'next-themes'

const ThemeChanger = () => {
  const { theme, setTheme } = useTheme()

  return (
    <div>
      Current theme: {theme}
      <button onClick={() => setTheme('light')}>Light Mode</button>
      <button onClick={() => setTheme('dark')}>Dark Mode</button>
    </div>
  )
}