diff --git a/src/components/header.jsx b/src/components/header.jsx index 8bf66f5a..601e4235 100644 --- a/src/components/header.jsx +++ b/src/components/header.jsx @@ -10,6 +10,13 @@ import logo from '../images/mdg.png'; const Header = (props) => { const { heading } = props; + // Theme state: 'light' or 'dark'. Persisted to localStorage. + const [theme, setTheme] = useState(() => { + if (typeof window === 'undefined') return 'light'; + const stored = window.localStorage.getItem('theme'); + if (stored) return stored; + return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; + }); const [stats, setstats] = useState({ starsCount: 0, forksCount: 0, @@ -37,7 +44,7 @@ const Header = (props) => { } }; useEffect(() => { - fetchData(); + fetchData(); setInterval(fetchData, 60000); gsap.set('.star, .fork', { @@ -52,15 +59,26 @@ const Header = (props) => { }); }, []); + // Apply theme to document root and persist selection + useEffect(() => { + if (typeof document !== 'undefined') { + if (theme === 'dark') document.documentElement.classList.add('dark'); + else document.documentElement.classList.remove('dark'); + } + if (typeof window !== 'undefined') window.localStorage.setItem('theme', theme); + }, [theme]); + + const toggleTheme = () => setTheme((t) => (t === 'dark' ? 'light' : 'dark')); + return (