-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
70 lines (58 loc) · 2.81 KB
/
theme.js
File metadata and controls
70 lines (58 loc) · 2.81 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
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
/**
Title: theme.js
Author: Megan Walker
Date: 01/01/2023
Description: JavaScript to capture, store, and display the selected theme
References: WEB 330 GitHub, WEB 330 Assign_1
*/
/**
* Set the default theme to light if one has not been set in the browsers localStorage
*/
function setDefaultTheme() {
const theme = localStorage.getItem("mode") || "light-theme";
const iconMode = localStorage.getItem("iconMode") || "fa-toggle-off";
const iconText = localStorage.getItem("iconText") || "Light Mode";
document.body.classList.value = theme;
document.getElementById("icon-mode").classList.add(iconMode);
document.getElementById("icon-text").innerHTML = iconText;
}
/**
* Set the HTML body to the user's selected theme. If one has not been selected, set the theme to light-theme
*/
function setSelectedTheme() {
document.body.classList.value = localStorage.getItem("mode") || "light-theme";
}
/** Copied the following script from WEB 300 GitHub as per instructions in the Assign_1 Document. */
/** Set the default theme to light if one has not been set in the browsers localStorage */
setDefaultTheme();
/** Function to switch the users selected website theme @param x (this instance) */
function toggleMode(x) {
let colorTheme = document.body.classList; // get the body's CSS class
let iconMode = x.classList; // get the current classes assigned to the triggered button
/** If the current body class is set to the light-theme, update the user's preference to the dark-theme in the browsers local storage. */
if (colorTheme.value === "light-theme") {
localStorage.clear();
localStorage.setItem("mode", "dark-theme");
localStorage.setItem("iconMode", "fa-toggle-on");
localStorage.setItem("iconText", "Dark Mode");
} else {
/** If the current body class is set to the dark-theme, update the user's preference to the light-theme in the browsers local storage */
localStorage.clear();
localStorage.setItem("mode", "light-theme");
localStorage.setItem("iconMode", "fa-toggle-off");
localStorage.setItem("iconText", "Light Mode");
}
/** Apply the updated selection to the HTML page elements */
colorTheme.value = localStorage.getItem("mode");
iconMode.value = `fa ${localStorage.getItem("iconMode")} pull-right`;
document.getElementById("icon-text").innerHTML =
localStorage.getItem("iconText");
}
/** Function to clear the browsers localStorage and set the default theme to light-theme. This function is for demonstration purposes only and does not need to be included in any of your solutions. */
function clearLocalStorage() {
localStorage.clear();
document.body.classList.value = "light-theme";
document.getElementById("icon-text").innerHTML = "Light Mode";
document.getElementById("icon-mode").classList.value =
"fa fa-toggle-off pull-right";
}