aboutsummaryrefslogtreecommitdiff
path: root/res/js/themeswap.js
blob: 12969a01c57ede91caa6ce95d7d79d73ce3dbb39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function addStyleSheet(name, id) {
	var path = '/res/css/' + name + '.min.css';
	var old = document.getElementById(id);
	if (old && (old.href != path)) {
		old.href = path;
	}
}

var otherTheme = {
	'dark': 'light',
	'light': 'dark',
};

var currentTheme = localStorage.getItem('theme');

if (!otherTheme.hasOwnProperty(currentTheme)) {
	currentTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}

addStyleSheet(currentTheme, 'theme');

function toggleTheme() {
	currentTheme = otherTheme[currentTheme] || 'light';
	localStorage.setItem('theme', currentTheme);
	addStyleSheet(currentTheme, 'theme');
}