forked from lapo-luchini/asn1js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.js
37 lines (37 loc) · 1.29 KB
/
theme.js
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
27
28
29
30
31
32
33
34
35
36
37
// set dark theme depending on OS settings
function setTheme(theme) {
if (theme == 'os') {
let prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
if (prefersDarkScheme.matches) {
theme = 'dark';
} else {
theme = 'light';
}
}
document.documentElement.style['color-scheme'] = theme;
document.querySelector('html').setAttribute('data-theme', theme);
// set the theme-color for iOS devices
let bgColor = getComputedStyle(document.documentElement).getPropertyValue('--main-bg-color');
let metaThemeColor = document.querySelector('meta[name=theme-color]');
metaThemeColor.setAttribute('content', bgColor);
}
// activate selected theme
let theme = 'os';
const localStorageTheme = localStorage.getItem('theme');
if (localStorageTheme) {
theme = localStorageTheme;
}
setTheme(theme);
// add handler to theme selection element
const selectTheme = document.getElementById('theme-select');
if (selectTheme) {
selectTheme.addEventListener ('change', function () {
localStorage.setItem('theme', selectTheme.value);
setTheme(selectTheme.value);
});
if (theme == 'light') {
selectTheme.selectedIndex = 2;
} else if (theme == 'dark') {
selectTheme.selectedIndex = 1;
}
}