-
Notifications
You must be signed in to change notification settings - Fork 1
/
theme-changer-v2.html
156 lines (131 loc) · 4.12 KB
/
theme-changer-v2.html
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en" data-theme="default">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@500&display=swap"
rel="stylesheet"
/>
<title>Theme Changer With More Advanced Functions</title>
<script>
// Entire Dark-Light Theme Functionality
const storageKey = "theme";
const changeTheme = () => {
theme.name = theme.name === "light" ? "dark" : "light";
setPreference();
};
const getColorPreference = () => {
if (localStorage.getItem(storageKey)) {
return localStorage.getItem(storageKey);
} else {
return window.matchMedia("(prefers-color-scheme: dark)").mathces
? "dark"
: "light";
}
};
const setPreference = () => {
localStorage.setItem(storageKey, theme.name);
displayPreference();
};
const displayPreference = () => {
document.firstElementChild.setAttribute("data-theme", theme.name);
document
.querySelector("#themeChanger")
?.setAttribute("aria-label", theme.name);
};
var theme = {
name: getColorPreference(),
};
displayPreference();
window.onload = () => {
// So that the screen-reader can get the current dynamic value [optional. just to improve the accessibility]
displayPreference();
document
.querySelector("#themeChanger")
.addEventListener("click", changeTheme);
};
// Sync With The System Changes
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", ({ matches: isDark }) => {
theme.name = isDark ? "dark" : "light";
setPreference();
});
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Fira Code", -apple-system, BlinkMacSystemFont, "Segoe UI",
Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue",
sans-serif;
transition: 0.2s ease;
}
html {
font-size: 18px;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
--background: hsl(215, 100%, 98%);
--primary-color: hsl(215, 100%, 40%);
--color-white: #ffffff;
}
html[data-theme="dark"] {
--background: hsl(215, 100%, 3%);
--primary-color: hsl(215, 100%, 30%);
--color-white: hsl(215, 100%, 10%);
}
body {
background-color: var(--background);
}
.theme-changer {
background-color: var(--color-white);
border: 3px solid var(--primary-color);
padding: 2rem;
color: var(--primary-color);
border-radius: 0.3rem;
text-transform: uppercase;
font-size: 2rem;
cursor: pointer;
touch-action: manipulation;
-webkit-tap-highlight-color: transparent;
outline-offset: 5px;
user-select: none;
-ms-user-select: none;
-moz-user-select: none;
--bshadow: hsl(215, 100%, 90%);
box-shadow: 0px 12px 20px 0px var(--bshadow);
font-weight: 500;
}
html[data-theme="dark"] .theme-changer {
box-shadow: 0px 12px 20px 0px var(--background);
}
.theme-changer:hover {
background-color: var(--primary-color);
color: var(--color-white);
box-shadow: none;
}
html[data-theme="dark"] .theme-changer:hover {
box-shadow: 0px 16px 20px 0px hsl(216, 100%, 2%);
}
</style>
</head>
<body>
<button
class="theme-changer"
id="themeChanger"
title="Toggles Light & Dark"
aria-label="auto"
aria-live="polite"
>
Dark / Light
</button>
</body>
</html>