Alina Lozowski
Migrating to the React project
e7abd9e
raw
history blame
855 Bytes
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 };
};