aboutsummaryrefslogtreecommitdiff
path: root/js/toggle.js
blob: 11ba783c659ce1e1a0e0cb13137d8574507e6804 (plain)
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
const moon = document.getElementById('moon');
const sun = document.getElementById('sun');

/**
 * Check if the user has dark mode enabled
 */
if (localStorage.getItem('darkMode') === 'true') {
    turnDark();
} else {
    turnLight();
}

/**
 * Update local storage with the current theme
 */
function updateLocalStorage() {
    if (document.documentElement.classList.contains('dark')) {
        localStorage.setItem('darkMode', 'true');
    } else {
        localStorage.setItem('darkMode', 'false');
    }
}

/**
 * Toggle the theme to dark
 */
function turnLight() {
    document.documentElement.classList.remove('dark');

    sun.style.display = 'none';
    moon.style.display = 'block';

    sun.style.opacity = '0';
    moon.style.opacity = '1';

    updateLocalStorage();
}

/**
 * Toggle the theme to light
 */
function turnDark() {
    document.documentElement.classList.add('dark');

    moon.style.display = 'none';
    sun.style.display = 'block';

    moon.style.opacity = '0';
    sun.style.opacity = '1';

    updateLocalStorage();
}

// Check for icon clicks
moon.addEventListener('click', () => {
    turnDark();
});

sun.addEventListener('click', () => {
    turnLight();
});