-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththeme-mode.js
More file actions
37 lines (33 loc) · 1.01 KB
/
Copy paththeme-mode.js
File metadata and controls
37 lines (33 loc) · 1.01 KB
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
function switchTheme() {
const currentStyle = currentTheme();
const iconElement = document.getElementById('github-icon');
if (currentStyle === 'light') {
setTheme('dark');
if (iconElement) {
iconElement.setAttribute('class', 'octicon');
iconElement.setAttribute('color', '#f0f6fc');
}
}
else {
setTheme('light');
if (iconElement) {
iconElement.removeAttribute('color');
iconElement.removeAttribute('class');
}
}
}
function setTheme(style) {
document.querySelectorAll('.isInitialToggle').forEach(elem => {
elem.classList.remove('isInitialToggle');
});
document.documentElement.setAttribute('data-color-mode', style);
localStorage.setItem('data-color-mode', style);
}
function currentTheme() {
const localStyle = localStorage.getItem('data-color-mode');
const systemStyle = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
return localStyle || systemStyle;
}
(() => {
setTheme(currentTheme());
})();