import { useCallback, useEffect, useState } from "react"; import "./ThemeToggle.css"; import { saveSession } from "~/cookie"; import { ThemeTypes } from "~/types/theme"; import { Moon, Sun } from "lucide-react"; export const ThemeToggle = ({ theme: initialTheme }: { theme: ThemeTypes }) => { const [theme, setTheme] = useState(initialTheme); useEffect(() => { // Apply theme class to document body to match root.tsx const body = document.body; if (theme === "dark") { body.classList.add("dark"); } else { body.classList.remove("dark"); } }, [theme]); const changeTheme = useCallback(async () => { const newTheme = theme === "light" ? "dark" : "light"; setTheme(newTheme); // Update document body class immediately to match root.tsx const body = document.body; if (newTheme === "dark") { body.classList.add("dark"); } else { body.classList.remove("dark"); } // Save to cookie for persistence await saveSession("theme", newTheme); }, [theme]); return ( ); };