-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy paththemetoggle.js
More file actions
39 lines (34 loc) · 1.07 KB
/
themetoggle.js
File metadata and controls
39 lines (34 loc) · 1.07 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
38
39
let currentTheme = localStorage.getItem('currentTheme') || 'auto'
const pygmentsDark = document.getElementById('pygments_dark_css')
const pydocthemeDark = document.getElementById('pydoctheme_dark_css')
const themeOrder = {
light: 'dark',
dark: 'auto',
auto: 'light',
}
updateTheme()
function toggleTheme() {
currentTheme = themeOrder[currentTheme]
localStorage.setItem('currentTheme', currentTheme)
updateTheme()
}
function updateTheme() {
const buttons = Array.from(document.getElementsByClassName('theme-toggle'))
switch (currentTheme) {
case 'light':
pydocthemeDark.media = 'not all'
pygmentsDark.media = 'not all'
buttons.forEach(e => e.value = "Toggle theme (light)")
break;
case 'dark':
pydocthemeDark.media = 'all'
pygmentsDark.media = 'all'
buttons.forEach(e => e.value = "Toggle theme (dark)")
break;
default:
// auto
pydocthemeDark.media = '(prefers-color-scheme: dark)'
pygmentsDark.media = '(prefers-color-scheme: dark)'
buttons.forEach(e => e.value = "Toggle theme (auto)")
}
}