MediaWiki:Common.js: Difference between revisions

From Mistbound
Jump to navigation Jump to search
Add clickable light/dark theme toggle
 
Move theme toggle to reliable inline script; blank Common.js
 
Line 1: Line 1:
(function () {
function currentTheme() {
var h = document.documentElement;
if (h.classList.contains('theme-dark')) { return 'dark'; }
if (h.classList.contains('theme-light')) { return 'light'; }
return ( window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ) ? 'dark' : 'light';
}


function applyTheme(t) {
var h = document.documentElement;
h.classList.remove('theme-dark', 'theme-light');
h.classList.add('theme-' + t);
try { localStorage.setItem('mb-theme', t); } catch (e) {}
}
mw.loader.using('mediawiki.util').then(function () {
var link = mw.util.addPortletLink(
'p-personal', '#', 'Theme', 'pt-theme', 'Switch between light and dark theme'
);
if (!link) { return; }
link.addEventListener('click', function (e) {
e.preventDefault();
applyTheme(currentTheme() === 'dark' ? 'light' : 'dark');
});
});
}());

Latest revision as of 11:16, 28 June 2026