-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
294 lines (262 loc) · 13 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<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">
<title>Dark Mode Toggler Example</title>
<script>
let getColorPreference = typeof modifyGetColorPreference !== 'undefined' ? modifyGetColorPreference : true;
let defaultTheme = typeof modifyDefaultTheme !== 'undefined' ? modifyDefaultTheme : 'light';
// Gets last saved theme on local storage, if null sets the theme based on system preference
let theme = localStorage.getItem('data-theme') || (getColorPreference ? (window.matchMedia("(prefers-color-scheme: dark)").matches ? 'dark' : 'light') : defaultTheme);
const setTheme = (newTheme) => {
theme = newTheme; // set newTheme to theme
document.documentElement.setAttribute("data-theme", newTheme) // set newTheme to data-theme
localStorage.setItem("data-theme", newTheme) // save newTheme to local storage
}
// Sets the theme
setTheme(theme);
document.onreadystatechange = () => {
// Get the element based on ID, checks it if theme is dark and add onchange event listener
const togglerCustom = document.getElementById("themeTogglerCustom");
togglerCustom.checked = theme == 'dark' ? true : false;
togglerCustom.onchange = (event) => {
changeTheme();
toggler.checked = togglerCustom.checked;
}
// Same as before but for Bootstrap switch
const toggler = document.getElementById("themeToggler");
toggler.checked = theme == 'dark' ? true : false;
toggler.onchange = (event) => {
changeTheme();
togglerCustom.checked = toggler.checked;
}
}
const changeTheme = () => {
document.body.style.transition = "background-color 0.5s";
setTheme(theme != 'light' ? 'light' : 'dark');
}
</script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css">
<link rel="stylesheet" href="theme.toggler.css">
<style>
:root{
--bg-color: #aebbc9;
}
[data-theme="dark"] {
--bg-color: #212529;
}
body {
color: var(--themeToggler-text-color);
background-color: var(--bg-color);
}
</style>
</head>
<body>
<div class="container-fluid d-flex justify-content-center align-items-center" style="min-height: 100vh;">
<div class="row align-self-center w-100">
<div class="col-12 text-center">
<span class="fs-1">DARK THEME TOGGLERS EXAMPLE</span>
<br><span class="fs-4">Common code between both implementations:</span><br><span class="fs-6">Click on buttons to see/expand the code</span><br><br>
<!-- BOOTSTRAP -->
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseBOOTSTRAP" aria-expanded="false" aria-controls="collapseBOOTSTRAP">
Bootstrap CDN
</button><br>
<div class="collapse" id="collapseBOOTSTRAP">
<pre><code class="language-html"><!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
</code></pre>
</div>
<!-- CSS -->
<br><button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseCSS" aria-expanded="false" aria-controls="collapseCSS">
CSS
</button><br>
<div class="collapse" id="collapseCSS">
<div class="row d-flex align-items-center">
<div class="col-12 col-md-6 m-0">
Done with CSS variables:
<pre><code class="language-css">:root {
--bg-color: #6c757d;
--text-color : black;
}
[data-theme="dark"] {
--bg-color: #212529;
--text-color: white;
}
body {
color: var(--text-color);
background-color: var(--bg-color);
}
.pointer * {
cursor: pointer;
user-select: none;
}
</code></pre>
</div>
<div class="col-12 col-md-6">
Or the same can be achieved this way: <br><br>
<pre><code class="language-css">body {
color: white;
background-color: #6c757d;
}
[data-theme="dark"] body {
color: black;
background-color: #212529;
}
.pointer * {
cursor: pointer;
user-select: none;
}
</code></pre>
</div>
</div>
</div>
<!-- JS -->
<br><button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseJS" aria-expanded="false" aria-controls="collapseJS">
JS
</button><br>
<div class="collapse" id="collapseJS">
This script is recommended to be on the HTML head so the theme can be stablish before the page loads, this avoids the page flashing when the theme changes on slow connections or CPUs
<br><br>Include the script via jsDelivr CDN:
<pre><code class="language-html"><script src="https://cdn.jsdelivr.net/gh/Haruki1707/ThemeToggler/theme.toggler.min.js" crossorigin="anonymous"></script></code></pre>
<br>Or instead check the code:
<pre><code class="language-js">// getColorPreference: true (default)
// defaultTheme: 'light' (default)
// Default value of 'getColorPreference' by defining 'modifyGetColorPreference' with another value
// Same applies to 'defaultTheme', it can be modified by defining 'modifyDefaultTheme'
// This 'modify' variables declaration must be before the load of this script
let getColorPreference = typeof modifyGetColorPreference !== 'undefined' ? modifyGetColorPreference : true;
let defaultTheme = typeof modifyDefaultTheme !== 'undefined' ? modifyDefaultTheme : 'light';
// Gets last saved theme on local storage, if null sets the theme based on system preference
let theme = localStorage.getItem('data-theme') || (getColorPreference ? (window.matchMedia("(prefers-color-scheme: dark)").matches ? 'dark' : 'light') : defaultTheme);
const setTheme = (newTheme) => {
theme = newTheme; // sets newTheme to theme
localStorage.setItem("data-theme", newTheme) // saves newTheme to local storage
document.documentElement.setAttribute("data-theme", newTheme) // sets newTheme to data-theme
}
// Sets the theme
setTheme(theme);
document.onreadystatechange = () => {
// Gets the element based on ID, adds 'checked' attribute if theme is dark and adds onchange event listener
const themeToggler = document.getElementById("themeToggler");
themeToggler.checked = theme == 'dark' ? true : false;
themeToggler.onchange = (event) => setTheme(theme != 'light' ? 'light' : 'dark');
};
</code></pre>
</div><br>
</div>
<div class="col-12 col-md-6 d-flex align-items-center flex-column">
<hr class="w-100">
<div class="form-check form-switch pointer">
<input class="form-check-input" type="checkbox" role="switch" id="themeToggler">
<label class="form-check-label" for="themeToggler">Dark theme</label>
</div>
<br><span class="fs-5">Required code for bootstrap switch implementation:</span>
<!-- HTML -->
<br><button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseHTMLB" aria-expanded="false" aria-controls="collapseHTMLB">
HTML
</button><br>
<div class="collapse w-100" id="collapseHTMLB">
<pre><code class="language-html"><div class="form-check form-switch pointer">
<input class="form-check-input" type="checkbox" role="switch" id="themeToggler">
<label class="form-check-label" for="themeToggler">Dark theme</label>
</div>
</code></pre>
</div>
</div>
<div class="col-12 col-md-6 d-flex align-items-center flex-column">
<hr class="w-100">
<label class="pointer d-flex align-items-center justify-content-center">
<div class="themeToggler-switch d-flex align-items-center justify-content-center">
<input type="checkbox" class="d-none" id="themeTogglerCustom">
<i class="bi bi-circle-fill text-light themeToggler-circle"></i>
<i class="bi bi-sun text-dark themeToggler-icon"></i>
<i class="bi bi-moon-fill text-light themeToggler-icon"></i>
</div>
<span class="themeToggler-text">Dark theme</span>
</label>
<br><span class="fs-5">Required code for custom switch implementation:</span>
<!-- BOOTSTRAP ICONS-->
<br><button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseBOOTSTRAPICONS" aria-expanded="false" aria-controls="collapseBOOTSTRAPICONS">
Bootstrap Icons CDN
</button>
<div class="collapse w-100" id="collapseBOOTSTRAPICONS">
<pre><code class="language-html"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</code></pre>
</div>
<!-- HTML -->
<br><button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseHTMLCUSTOM" aria-expanded="false" aria-controls="collapseHTMLCUSTOM">
HTML
</button>
<div class="collapse w-100" id="collapseHTMLCUSTOM">
<pre><code class="language-html"><label class="pointer d-flex align-items-center justify-content-center">
<div class="themeToggler-switch d-flex align-items-center justify-content-center">
<input type="checkbox" class="d-none" id="themeToggler">
<i class="bi bi-circle-fill text-light themeToggler-circle"></i>
<i class="bi bi-brightness-high text-dark themeToggler-icon"></i>
<i class="bi bi-moon-fill text-light themeToggler-icon"></i>
</div>
<span class="themeToggler-text">Dark theme</span>
</label>
</code></pre>
</div>
<!-- CSS -->
<br><button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseCSSCUSTOM" aria-expanded="false" aria-controls="collapseCSSCUSTOM">
CSS
</button>
<div class="collapse w-100" id="collapseCSSCUSTOM">
<br>Include the style via jsDelivr CDN (already includes the 'pointer' class):
<pre><code class="language-html"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Haruki1707/ThemeToggler/theme.toggler.min.css" crossorigin="anonymous"></code></pre>
<br>Or instead check the code:
<pre><code class="language-css">:root{
/*This variables can be modified to customize the themeToggler */
--themeToggler-bg-color: #0c0b13;
--themeToggler-text-color: black;
}
[data-theme="dark"] {
--themeToggler-bg-color: #9c9ca3;
--themeToggler-text-color: white;
}
[class*=" themeToggler-"]::before {
display: block !important;
}
.themeToggler-switch {
width: 64px;
height: 30px;
padding: 6px;
font-size: 20px;
position: relative;
border-radius: 60px;
transition: all 0.5s;
background-color: var(--themeToggler-bg-color);
}
.themeToggler-circle {
font-size: 24px;
position: absolute;
transition: all 0.5s;
transform: translateX(-16px);
}
.themeToggler-icon {
padding: 6px;
font-size: 20px;
}
.themeToggler-text {
padding-left: 5px;
transition: all 0.5s;
color: var(--themeToggler-text-color);
}
[data-theme="dark"] .themeToggler-circle {
transform: translateX(16px);
}
</code></pre>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
</body>
</html>