This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test-nightshade.html
336 lines (298 loc) · 13.4 KB
/
test-nightshade.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="apple-touch-icon" sizes="180x180" href="img/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="img/favicon-32x32.png">
<link rel="shortcut icon" href="img/favicon.ico">
<!-- The page supports both dark and light color schemes, and the page author prefers dark. -->
<meta name="color-scheme" content="light dark">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="./dist/bootstrap-nightshade.css">
<!-- Prism.js for syntax highlighting -->
<link rel="stylesheet/less" type="text/css" href="./css/prism-combo.less">
<title>bootstrap-nightshade</title>
<style>
.d-clipboard {
position: relative;
float: right;
}
.btn-clipboard {
position: absolute;
top: .25rem;
right: .25rem;
opacity: .25;
}
.btn-clipboard:hover,
.btn-clipboard:active {
opacity: 1;
}
.token.comment {
font-style: italic;
}
</style>
</head>
<body>
<header>
<nav class="navbar navbar-dark navbar-expand-lg navbar-light bg-primary">
<div class="container">
<a class="navbar-brand">bootstrap-dark</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto" id="nav-links"></ul>
<form class="form-inline">
<div class="custom-control custom-switch" title="Toggle Dark Mode" data-toggle="tooltip" data-placement="left">
<input type="checkbox" class="custom-control-input" id="css-toggle-btn">
<label class="custom-control-label" for="css-toggle-btn"></label>
</div>
<div class="custom-control custom-switch" title="Remember my preference" data-toggle="tooltip" data-placement="left">
<input type="checkbox" class="custom-control-input" id="css-save-btn">
<label class="custom-control-label" for="css-save-btn"></label>
</div>
</form>
</div>
</div>
</nav>
</header>
<main class="mt-5 mb-5">
<div class="container">
<div class="row">
<div class="col">
<h1>Dark Mode in a single CSS theme file, and <code>html.dark</code> selector</h1>
</div>
</div>
<div class="row mt-5">
<div class="col-sm">
<h2>Setup</h2>
<ul>
<li>A single, fully themed, 2 color mode, Bootstrap CSS</li>
<li>Driven by <code>.dark</code> & <code>.light</code> <code><html></code> tag classes</li>
<li>Requires JavaScript & jQuery</li>
<li>Not dependant on <code>prefers-color-scheme</code>, but honours it initially</li>
</ul>
<p>Replace the bootstrap stylesheet with the following code:</p>
<div class="code"><pre><code class="language-html language-js"><!-- Bootstrap CSS -->
<link rel="stylesheet" href="bootstrap-nightshade.css">
</code></pre></div>
<p>You will also need the following code to toggle a `<code>dark</code>` class in the <code><html></code> tag with this code:</p>
<div class="code"><pre><code class="language-html language-js"><script>
$(document).ready(function(){
// This code uses `localStorage` to save "user prefers color" persistently
// This key used is `user-prefers-color`, and should be one of:
// 0 = only used at first trigger, to indicate firts time selection
// 1 = user wants light mode
// 2 = user wants dark mode
// the key can also be deleted to indicate user has no preference.
// function to toggle the css
function toggle_color_scheme_css($mode) {
// amend the body classes
if ($mode == 'dark') {
$("html").removeClass('light').addClass("dark");
} else {
$("html").removeClass("dark").addClass('light');
}
// if on user prefers color then update stored value
$upc = window.localStorage.getItem('user-prefers-color');
if ($upc !== null) {
if ($upc == 0) $("#css-save-btn").prop( "checked", true ); // first time click
window.localStorage.setItem('user-prefers-color', ($mode == 'dark') ? 2 : 1);
}
}
// function / listener action to toggle the button
function update_color_scheme_css() {
$upc = window.localStorage.getItem('user-prefers-color');
if (($upc === null) || ($upc == 0)) {
$mode = (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) ? 'dark' : 'light';
} else {
$mode = ($upc == 2) ? 'dark' : 'light';
}
$("#css-toggle-btn").prop( "checked", ('dark' == $mode) );
toggle_color_scheme_css($mode);
}
// initial mode discovery & update button
update_color_scheme_css();
if (window.localStorage.getItem('user-prefers-color') !== null)
$("#css-save-btn").prop( "checked", true );
// update every time it changes
if (window.matchMedia) window.matchMedia("(prefers-color-scheme: dark)").addListener( update_color_scheme_css );
// toggle button click code
$("#css-toggle-btn").bind("click", function() {
// disable further automatic toggles
if (window.localStorage.getItem('user-prefers-color') === null)
window.localStorage.setItem('user-prefers-color', 0);
// get current mode, i.e. does body have the `.dark`` classname
$mode = $("html").hasClass("dark") ? 'light' : 'dark';
toggle_color_scheme_css($mode);
});
// toggle button click code
$("#css-save-btn").bind("click", function() {
$chk = $("#css-save-btn").prop("checked");
if ($chk) {
// user wants persistance, save current state
$upc = $("html").hasClass("dark") ? 2 : 1;
window.localStorage.setItem("user-prefers-color", $upc);
} else {
// user doesn't want pesistace, delete saved key
window.localStorage.removeItem("user-prefers-color");
}
});
});
</script>
</code></pre></div>
<p>This code features a persistance engine that uses JS' `<code>window.localStorage</code>` to "remember" the users option.</p>
<p>The issue with the above approach is the “content flash” that happens when the user has persisted a “dark” mode.
On a reload, code will transition to dark as designed but there will be a sub-second flash as the light page loads first and then gets transitioned out.
This is similar to <a href="https://en.wikipedia.org/wiki/Flash_of_unstyled_content" title="Flash Of Unstyled Content" data-toggle="tooltip">FOUC</a> and is well know to developers - with few elegant fixes.</p>
<p>Read the <a href="readme.html">README.md</a> for more on this proof of concept.</p>
</div>
</div>
</div>
</main>
<footer id="copyright" class="text-center">
<small class="text-muted">© 2020</small>
</footer>
<!-- Optional JavaScript --> <!-- ** needed for toggle -->
<!-- jQuery first, then Popper.js + Bootstrap JS bundle -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js"></script>
<!-- Prism.js for syntax highlighting -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/plugins/autoloader/prism-autoloader.min.js"></script>
<!-- Clipboard.js for copy code -->
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<!-- Less.js -->
<script>
less = {
env: "production",
logLevel: 0,
};
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/3.9.0/less.min.js"></script>
<!-- Hotkeys.js for [D] hotkey -->
<script src="https://cdn.jsdelivr.net/gh/jeresig/jquery.hotkeys@0/jquery.hotkeys.js"></script>
<script>
$(document).ready(function(){
// only when jQuery is ready
// init the tooltip
// ----------------------------------------
$('[data-toggle="tooltip"]').tooltip()
.on('mouseleave', function () {
$(this).tooltip('hide')
});;
// Insert copy to clipboard button before .highlight
// borrowed from Bootstrap docs, uses Clipboard.js
// ----------------------------------------
$('div.code').each(function () {
var btnHtml = '<div class="d-clipboard"><button type="button" class="btn btn-outline-info btn-sm btn-clipboard" title="Copy to clipboard">Copy</button></div>'
$(this).before(btnHtml)
$('.btn-clipboard')
.tooltip()
.on('mouseleave', function () {
$(this).tooltip('hide')
});
});
var clipboard = new ClipboardJS('.btn-clipboard', {
target: function (trigger) {
return trigger.parentNode.nextElementSibling
}
});
clipboard.on('success', function (e) {
$(e.trigger)
.attr('title', 'Copied!')
.tooltip('_fixTitle')
.tooltip('show')
.attr('title', 'Copy to clipboard')
.tooltip('_fixTitle')
e.clearSelection()
});
clipboard.on('error', function (e) {
var modifierKey = /mac/i.test(navigator.userAgent) ? '\u2318' : 'Ctrl-'
var fallbackMsg = 'Press ' + modifierKey + 'C to copy'
$(e.trigger)
.attr('title', fallbackMsg)
.tooltip('_fixTitle')
.tooltip('show')
.attr('title', 'Copy to clipboard')
.tooltip('_fixTitle')
});
// ========================================
// This code uses `localStorage` to save "user prefers color" persistently
// This key used is `user-prefers-color`, and should be one of:
// 0 = only used at first trigger, to indicate firts time selection
// 1 = user wants light mode
// 2 = user wants dark mode
// the key can also be deleted to indicate user has no preference.
// function to toggle the css
function toggle_color_scheme_css($mode) {
// amend the body classes
if ($mode == 'dark') {
$("html").removeClass('light').addClass("dark");
} else {
$("html").removeClass("dark").addClass('light');
}
// if on user prefers color then update stored value
$upc = window.localStorage.getItem('user-prefers-color');
if ($upc !== null) {
if ($upc == 0) $("#css-save-btn").prop( "checked", true ); // first time click
window.localStorage.setItem('user-prefers-color', ($mode == 'dark') ? 2 : 1);
}
}
// function / listener action to toggle the button
function update_color_scheme_css() {
$upc = window.localStorage.getItem('user-prefers-color');
if (($upc === null) || ($upc == 0)) {
$mode = (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) ? 'dark' : 'light';
} else {
$mode = ($upc == 2) ? 'dark' : 'light';
}
$("#css-toggle-btn").prop( "checked", ('dark' == $mode) );
toggle_color_scheme_css($mode);
}
// initial mode discovery & update button
update_color_scheme_css();
if (window.localStorage.getItem('user-prefers-color') !== null)
$("#css-save-btn").prop( "checked", true );
// update every time it changes
if (window.matchMedia) window.matchMedia("(prefers-color-scheme: dark)").addListener( update_color_scheme_css );
// toggle button click code
$("#css-toggle-btn").bind("click", function() {
// disable further automatic toggles
if (window.localStorage.getItem('user-prefers-color') === null)
window.localStorage.setItem('user-prefers-color', 0);
// get current mode, i.e. does body have the `.dark`` classname
$mode = $("html").hasClass("dark") ? 'light' : 'dark';
toggle_color_scheme_css($mode);
});
// toggle button click code
$("#css-save-btn").bind("click", function() {
$chk = $("#css-save-btn").prop("checked");
if ($chk) {
// user wants persistance, save current state
$upc = $("html").hasClass("dark") ? 2 : 1;
window.localStorage.setItem("user-prefers-color", $upc);
} else {
// user doesn't want pesistace, delete saved key
window.localStorage.removeItem("user-prefers-color");
}
});
// ----- Nav & Copyright ------------------
$.ajax( {
url: './navbar.htm', type: 'get',
success: function(content) {
$("#nav-links").html( content );
$("#nav-tests,#nav-nightshade").addClass("active");
}
} );
$.ajax( {
url: './copyright.htm', type: 'get',
success: function(content) {
$("#copyright").html( content );
}
} );
});
</script>
</body>
</html>