"use client"; import { useEffect, useState } from "react"; type Theme = "light" | "dark"; /** Script inline (avant hydratation) : applique le thème mémorisé ou celui du système, sans flash. */ export const THEME_INIT_SCRIPT = `(function(){try{var t=localStorage.getItem('qc26-theme');if(t!=='light'&&t!=='dark'){t=window.matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light'}document.documentElement.setAttribute('data-theme',t);var m=document.querySelector('meta[name="theme-color"]');if(m){m.setAttribute('content',t==='dark'?'#0e1013':'#f6f5f1')}}catch(e){}})();`; function current(): Theme { if (typeof document === "undefined") return "light"; return (document.documentElement.getAttribute("data-theme") as Theme) || "light"; } export function ThemeToggle({ className }: { className?: string }) { const [theme, setTheme] = useState("light"); // eslint-disable-next-line react-hooks/set-state-in-effect -- synchronisation unique avec l'attribut posé avant hydratation useEffect(() => { setTheme(current()); }, []); const toggle = () => { const next: Theme = current() === "dark" ? "light" : "dark"; document.documentElement.setAttribute("data-theme", next); try { localStorage.setItem("qc26-theme", next); } catch { /* privé */ } document.querySelector('meta[name="theme-color"]')?.setAttribute("content", next === "dark" ? "#0e1013" : "#f6f5f1"); setTheme(next); }; return ( ); }