'use client'; import { useState, useEffect } from 'react'; import { Moon, Sun } from 'lucide-react'; import { Button } from '@/components/ui/button'; export function ThemeToggle() { const [theme, setTheme] = useState(undefined); useEffect(() => { // Set initial theme based on localStorage or system preference const storedTheme = localStorage.getItem('theme'); const systemPrefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; if (storedTheme) { setTheme(storedTheme); } else { setTheme(systemPrefersDark ? 'dark' : 'light'); } }, []); useEffect(() => { // Apply theme to document and save to localStorage if (theme === 'dark') { document.documentElement.classList.add('dark'); localStorage.setItem('theme', 'dark'); } else if (theme === 'light') { document.documentElement.classList.remove('dark'); localStorage.setItem('theme', 'light'); } }, [theme]); const toggleTheme = () => { setTheme(prevTheme => (prevTheme === 'light' ? 'dark' : 'light')); }; if (theme === undefined) { // Return a placeholder to avoid rendering mismatch during initial load // and to reserve space, matching the button's size. return ); }