-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.txt
81 lines (68 loc) · 3.37 KB
/
theme.txt
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var themeToggleDarkIconMobile = document.getElementById('theme-toggle-dark-icon-mobile');
var themeToggleLightIconMobile = document.getElementById('theme-toggle-light-icon-mobile');
var themeToggleDarkIconDesktop = document.getElementById('theme-toggle-dark-icon-desktop');
var themeToggleLightIconDesktop = document.getElementById('theme-toggle-light-icon-desktop');
// On page load or when changing themes, best to add inline in `head` to avoid FOUC
if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark')
}
// Change the icons inside the button based on previous settings
if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
themeToggleLightIconMobile.classList.remove('hidden');
themeToggleLightIconDesktop.classList.remove('hidden');
} else {
themeToggleDarkIconMobile.classList.remove('hidden');
themeToggleDarkIconDesktop.classList.remove('hidden');
}
var themeToggleBtnMobile = document.getElementById('theme-toggle-mobile');
var themeToggleBtnDesktop = document.getElementById('theme-toggle-desktop');
themeToggleBtnMobile.addEventListener('click', function() {
// toggle icons inside button
themeToggleDarkIconMobile.classList.toggle('hidden');
themeToggleLightIconMobile.classList.toggle('hidden');
// if set via local storage previously
if (localStorage.getItem('color-theme')) {
if (localStorage.getItem('color-theme') === 'light') {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
} else {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
}
// if NOT set via local storage previously
} else {
if (document.documentElement.classList.contains('dark')) {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
} else {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
}
}
});
themeToggleBtnDesktop.addEventListener('click', function() {
// toggle icons inside button
themeToggleDarkIconDesktop.classList.toggle('hidden');
themeToggleLightIconDesktop.classList.toggle('hidden');
// if set via local storage previously
if (localStorage.getItem('color-theme')) {
if (localStorage.getItem('color-theme') === 'light') {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
} else {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
}
// if NOT set via local storage previously
} else {
if (document.documentElement.classList.contains('dark')) {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
} else {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
}
}
});