forked from kallaway/100-days-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-theme-switcher.js
60 lines (51 loc) · 1.58 KB
/
my-theme-switcher.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// function setTheme(themeName) {
// localStorage.setItem('theme', themeName);
// document.documentElement.className = themeName;
// }
// function toggleTheme() {
// if (localStorage.getItem('theme') === 'theme-dark') {
// setTheme('theme-light');
// console.log('light-theme loaded')
// } else {
// setTheme('theme-dark');
// console.log('dark-theme loaded')
// }
// }
// (function () {
// if (localStorage.getItem('theme') === 'theme-dark') {
// setTheme('theme-dark');
// } else {
// setTheme('theme-light');
// }
// })();
// Define which theme should load next
const themeList = {
dark: 'light',
light: 'blues',
blues: 'dark'
};
// Load the existing theme in local storage
const theme = localStorage.getItem('theme');
const bodyClass = document.body.classList;
theme && bodyClass.add(theme);
// Change the theme on a button click
function toggleTheme() {
const current = localStorage.getItem('theme');
const next = themeList[current];
bodyClass.replace(current, next);
localStorage.setItem('theme', next);
console.log('theme', next);
}
document.getElementById('themeButton').onclick = toggleTheme;
function clearStorage() {
localStorage.clear();
console.log('Local Storage Cleared!')
}
const themeCheck = localStorage.getItem('theme');
if (typeof themeCheck !== 'undefined' || themeCheck !== null) {
console.log(`${theme} theme displayed`);
localStorage.setItem('theme', 'blues');
} else {
console.log('theme is not found');
localStorage.setItem('theme', 'blues');
}