Spaces:
Running
Running
'use client'; | |
import { createContext, useContext, useEffect, useState } from 'react'; | |
type DarkModeContextType = { | |
isDarkMode: boolean; | |
toggleDarkMode: () => void; | |
}; | |
const DarkModeContext = createContext<DarkModeContextType | undefined>(undefined); | |
export function DarkModeProvider({ children }: { children: React.ReactNode }) { | |
const [isDarkMode, setIsDarkMode] = useState(false); | |
useEffect(() => { | |
// Check if user has dark mode preference | |
const isDark = localStorage.getItem('darkMode') === 'true'; | |
setIsDarkMode(isDark); | |
if (isDark) { | |
document.documentElement.classList.add('dark'); | |
} | |
}, []); | |
const toggleDarkMode = () => { | |
setIsDarkMode((prev) => { | |
const newValue = !prev; | |
localStorage.setItem('darkMode', String(newValue)); | |
if (newValue) { | |
document.documentElement.classList.add('dark'); | |
} else { | |
document.documentElement.classList.remove('dark'); | |
} | |
return newValue; | |
}); | |
}; | |
return ( | |
<DarkModeContext.Provider value={{ isDarkMode, toggleDarkMode }}> | |
{children} | |
</DarkModeContext.Provider> | |
); | |
} | |
export function useDarkMode() { | |
const context = useContext(DarkModeContext); | |
if (context === undefined) { | |
throw new Error('useDarkMode must be used within a DarkModeProvider'); | |
} | |
return context; | |
} |