mediawiki-skins-Citizen/resources/skins.citizen.scripts/theme.js

43 lines
991 B
JavaScript
Raw Normal View History

/**
* @param {Window} window
* @return {void}
*/
function initThemeSettings( window ) {
const setLocalStorage = ( themeName ) => {
try {
// eslint-disable-next-line indent
2021-12-16 03:19:39 +00:00
localStorage.setItem( 'skin-citizen-theme', themeName );
} catch ( e ) {}
};
2021-06-09 18:40:44 +00:00
const prefersDark = window.matchMedia( '(prefers-color-scheme: dark)' ),
applyTheme = () => {
window.applyPref();
// So that theme is applied but localStorage keeps the auto config
setLocalStorage( 'auto' );
};
// Monitor prefers-color-scheme changes
prefersDark.addEventListener( 'change', ( event ) => {
setLocalStorage( event.matches ? 'dark' : 'light' );
2021-06-09 18:40:44 +00:00
applyTheme();
} );
}
/**
* @param {Window} window
* @return {void}
*/
function initTheme( window ) {
if ( typeof window.mw !== 'undefined' ) {
2021-06-09 18:40:44 +00:00
const theme = window.localStorage.getItem( 'skin-citizen-theme' );
if ( theme === null || theme === 'auto' ) {
initThemeSettings( window );
}
}
}
module.exports = {
init: initTheme
};