forked from akshitagupta15june/PetMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
light-dark-theme.js
120 lines (111 loc) · 3.94 KB
/
light-dark-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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Element {
constructor(cssNames, lightCss, darkCss) {
this.cssNames = cssNames;
this.eles = [];
cssNames.forEach((element) => {
document.querySelectorAll("." + element).forEach((ele) => {
this.eles.push(ele);
});
});
this.lightCss = lightCss;
this.darkCss = darkCss;
this.applyTheme();
}
applyTheme() {
const isDarkMode = document.body.classList.contains("dark-theme");
this.eles.forEach((element) => {
if (isDarkMode) {
element.classList.add(this.darkCss);
element.classList.remove(this.lightCss);
} else {
element.classList.add(this.lightCss);
element.classList.remove(this.darkCss);
}
});
}
toggle() {
this.eles.forEach((element) => {
if (document.body.classList.contains("light-mode")) {
element.classList.toggle(this.lightCss);
element.style.color = ""; // Change text color to blue in light mode
} else {
element.classList.toggle(this.darkCss);
element.style.color = ""; // Reset text color in dark mode
}
});
}
}
const icon = document.getElementById("switch");
icon.onclick = function () {
document.body.classList.toggle("dark-theme");
if (document.body.classList.contains("dark-theme")) {
icon.className = "ri-moon-line turn-red-hover dark-theme";
localStorage.setItem("theme", "dark");
} else {
icon.className = "ri-sun-line turn-yellow-hover light-theme";
localStorage.setItem("theme", "light");
}
toggleThemeForAllElements();
};
let elements = [
new Element(["text_1"], "text-slate-50", "text-gray-900"),
new Element(["text_2"], "text-blue-600", "text-blue-200"),
new Element(
[
"text_3",
"meet-heading",
"main-body-section-div",
"turn-white-hover",
"text-custom-heading",
"text-center"
],
"text-gray-900",
"text-white"
),
new Element(["text_4"], "text-choco", "text-white"),
new Element(
["bg-header-offwhite", "main-card", "b_1"],
"bg-slate-100",
"bg-amber-900"
),
new Element(
["bg_2", "meet-heading", "pet-card-flex"],
"bg-blue-300",
"bg-yellow-900"
),
new Element(["bg_3"], "bg-blue-600", "bg-blue-200"),
new Element(["bg-blog", "bg-donate"], "bg-white-200", "bg-amber-700"),
new Element(["mission-1"], "mission-dark", "mission-light"),
new Element(["mission-2"], "mission-dark", "mission-light"),
new Element(["navbar-item"], "turn-red-hover", "turn-yellow-hover"),
new Element(
["contact-form-text"],
"contactusform-color-lightmode",
"contactusform-color-darkmode"
),
new Element(["donate-h1", "donate-h2", "leading-7"], ["text-amber", "text-gray-900"], "text-white"),
// Add new card elements
new Element(["pet-card-flex"], "bg-light-mode", "bg-dark-mode"),
new Element(["main-card"], "main-card-light", "main-card-dark"),
new Element(["hover-button"], "hover-button-light", "hover-button-dark"),
new Element(["pet-name"], "text-light-mode", "text-dark-mode"), // New class for pet name
new Element(["text_4"], "text-light-mode", "text-dark-mode") // Apply changes to list items and icons
];
function toggleThemeForAllElements() {
elements.forEach((element) => element.toggle());
}
// Function to load theme preference on page load
function loadThemePreference() {
const theme = localStorage.getItem("theme");
if (theme === "dark") {
document.body.classList.add("dark-theme");
icon.className = "ri-moon-line turn-red-hover dark-theme";
} else {
document.body.classList.remove("dark-theme");
icon.className = "ri-sun-line turn-yellow-hover light-theme";
}
// Apply the correct theme to all elements
elements.forEach((element) => element.applyTheme());
}
// Load theme preference on page load
document.addEventListener("DOMContentLoaded", loadThemePreference);