-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.js
47 lines (32 loc) · 1.44 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
38
39
40
41
42
43
// this one is jut to wait for the page to load
document.addEventListener('DOMContentLoaded', () => {
const themeStylesheet = document.getElementById('theme');
const storedTheme = localStorage.getItem('theme');
if(storedTheme){
themeStylesheet.href = storedTheme;
}
const themeToggle = document.getElementById('theme-toggle');
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
themeStylesheet.href = '/assets/css/dark.css';
themeToggle.innerText = 'Switch to light mode';
localStorage.setItem('theme',themeStylesheet.href)
}
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {
themeStylesheet.href = '/assets/css/light.css';
themeToggle.innerText = 'Switch to dark mode';
localStorage.setItem('theme',themeStylesheet.href)
}
themeToggle.addEventListener('click', () => {
// if it's light -> go dark
if(themeStylesheet.href.includes('light')){
themeStylesheet.href = '/assets/css/dark.css';
themeToggle.innerText = 'Switch to light mode';
} else {
// if it's dark -> go light
themeStylesheet.href = '/assets/css/light.css';
themeToggle.innerText = 'Switch to dark mode';
}
// save the preference to localStorage
localStorage.setItem('theme',themeStylesheet.href)
})
})