Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import { useState, useEffect } from 'react'; | |
export const useThemeMode = () => { | |
// Get system preference | |
const getSystemPreference = () => { | |
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | |
}; | |
// Initialize theme mode from system preference | |
const [mode, setMode] = useState(getSystemPreference); | |
// Listen to system preference changes | |
useEffect(() => { | |
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); | |
const handleChange = (e) => { | |
setMode(e.matches ? 'dark' : 'light'); | |
}; | |
mediaQuery.addEventListener('change', handleChange); | |
return () => mediaQuery.removeEventListener('change', handleChange); | |
}, []); | |
const toggleTheme = () => { | |
setMode((prevMode) => (prevMode === 'light' ? 'dark' : 'light')); | |
}; | |
return { mode, toggleTheme }; | |
}; |