Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Press0 1328 default to light mode #565

Merged
merged 11 commits into from
May 27, 2024
30 changes: 28 additions & 2 deletions src/OnboardingSPA/components/ThemeContextProvider/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { useState, createContext } from '@wordpress/element';
import { useState, useEffect, createContext } from '@wordpress/element';
import { THEME_DARK, THEME_LIGHT } from '../../../constants';

const ThemeContext = createContext();

function getPreferredColorScheme() {
if (
window.matchMedia &&
window.matchMedia( '(prefers-color-scheme: dark)' ).matches
) {
return THEME_DARK;
}
return THEME_LIGHT;
}

const ThemeProvider = ( { children } ) => {
const [ theme, setTheme ] = useState( 'dark' );
const [ theme, setTheme ] = useState( getPreferredColorScheme );

useEffect( () => {
const colorSchemeMediaQuery = window.matchMedia(
'(prefers-color-scheme: dark)'
);

const handleChange = ( event ) => {
setTheme( event.matches ? THEME_DARK : THEME_LIGHT );
};

colorSchemeMediaQuery.addEventListener( 'change', handleChange );

return () => {
colorSchemeMediaQuery.removeEventListener( 'change', handleChange );
};
}, [] );

const toggleTheme = () => {
setTheme( ( prevTheme ) =>
Expand Down
Loading