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-night.html
358 lines (328 loc) · 15.7 KB
/
test-night.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!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 -->
<script>
if (window.matchMedia("(prefers-color-scheme: dark)").media === "not all") {
document.documentElement.style.display = "none";
document.head.insertAdjacentHTML(
"beforeend",
"<link id=\"css\" rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css\" onload=\"document.documentElement.style.display = ''\">"
);
}
</script>
<link id="css-dark" rel="stylesheet" href="./dist/bootstrap-night.css" media="(prefers-color-scheme: dark)">
<link id="css-light" rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" media="(prefers-color-scheme: no-preference), (prefers-color-scheme: light)">
<!-- Prism.js for syntax highlighting -->
<script>
if (window.matchMedia("(prefers-color-scheme: dark)").media === "not all") {
document.head.insertAdjacentHTML(
"beforeend",
"<link id=\"prism\" rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/themes/prism.min.css\">"
);
}
</script>
<link id="prism-dark" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/themes/prism-tomorrow.min.css" media="(prefers-color-scheme: dark)">
<link id="prism-light" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/themes/prism.min.css" media="(prefers-color-scheme: no-preference), (prefers-color-scheme: light)">
<title>bootstrap-night</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>
</form>
</div>
</div>
</nav>
</header>
<main class="mt-5 mb-5">
<div class="container">
<div class="row">
<div class="col">
<h1>Dark Mode with two CSS theme files</h1>
</div>
</div>
<div class="row mt-5">
<div class="col-sm">
<h2>Basic Setup
<small class="text-muted">With no user toggle support</small>
</h2>
<ul>
<li>Two fully themed Bootstrap CSS are required</li>
<li>Filtered by stylesheet media filters</li>
<li>Based on advice by <a href="https://web.dev/authors/thomassteiner">Thomas Steiner</a> in <a href="https://web.dev/prefers-color-scheme/">Hello darkness, my old friend.</a></li>
<li>JavaScript fallback if the browser does not support dark-mode</li>
<li>Does not require jQuery</li>
</ul>
<p>Replace the bootstrap stylesheet with the following code:</p>
<div class="code"><pre><code class="language-html language-js" data-keep-tags="span"><!-- Bootstrap CSS -->
<!-- Inform modern browsers that this page supports both dark and light color schemes,
and the page author prefers light. -->
<meta name="color-scheme" content="light dark">
<script>
// If `prefers-color-scheme` is not supported, fall back to light mode.
// i.e. In this case, inject the `light` CSS before the others, with
// no media filter so that it will be downloaded with highest priority.
if (window.matchMedia("(prefers-color-scheme: dark)").media === "not all") {
document.documentElement.style.display = "none";
document.head.insertAdjacentHTML(
"beforeend",
"<link <span class="collapse more-code" id="code-css">id=\"css\" </span>rel=\"stylesheet\" href=\"bootstrap.css\" onload=\"document.documentElement.style.display = ''\">"
);
}
</script>
<!-- Load the alternate CSS first ... -->
<link <span class="collapse more-code" id="code-css-dark">id="css-dark" </span>rel="stylesheet" href="bootstrap-night.css" media="(prefers-color-scheme: dark)">
<!-- ... and then the primary CSS last for a fallback on very old browsers that don't support media filtering -->
<link <span class="collapse more-code" id="code-css-light">id="css-light" </span>rel="stylesheet" href="bootstrap.css" media="(prefers-color-scheme: no-preference), (prefers-color-scheme: light)">
</code></pre></div>
<p>This is all you need to enable dark mode with Bootstrap.</p>
<p>You also have the flexibility to give the user control with a toggle switch,
but that requires <a class="link collapsed" data-toggle="collapse" data-target=".more-code" href="#more" aria-expanded="false"
aria-controls="more code-css code-css-light code-css-dark" >some additional code</a>.</p>
</div>
</div><div class="row mt-5 collapse more-code" id="more">
<div class="col-sm">
<h2>Additional Setup
<small class="text-muted">Giving user control</small>
</h2>
<p>The basic principle is honouring the browser preference (which we assume the user set with intent), but also provide a switch in the UI.
Once this switch has been clicked on, swap the color mode and remove browser based color scheme selection.</p>
<p>The following code will require jQuery:</p>
<div class="code"><pre><code class="language-html language-js"><script>
$(document).ready(function(){
// Update the toggle button based on current color scheme
function updateDarkToggleButton() {
$dark = (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches);
$("#css-toggle-btn").prop( "checked", $dark );
}
// Update on first load.
updateDarkToggleButton();
// and every time it changes
if (window.matchMedia) window.matchMedia("(prefers-color-scheme: dark)").addListener( updateDarkToggleButton );
// Color Scheme toggle botton
// function to initialise the css
function init_color_scheme_css($id, $mode) {
if ($("#"+$id)) $("#"+$id).remove(); // remove exitsing id
$("#"+$id+"-"+$mode).attr( {
"data-href-light": $("#"+$id+"-light").attr("href"), // store the light CSS url
"data-href-dark": $("#"+$id+"-dark").attr("href"), // store the dark CSS url
"data-color-scheme": $mode, // store the mode, so that we don't re-initalise
"media": "all", // drop the media filter
"id": $id // rename the id (drop the `-{mode}` bit)
} );
$other = ($mode == 'dark') ? 'light' : 'dark';
$("#"+$id+"-"+$other).remove();
}
// function to toggle the CSS
function toggle_color_scheme_css($id, $mode) {
// grab the new mode css href
$href = $("#"+$id).data("href-"+$mode); // use `.data()` here, leverage the cache
// set the CSS to the mode preference.
$("#"+$id).attr( {
"href": $href,
"data-color-scheme": $mode,
});
}
// toggle button click code
$("#css-toggle-btn").bind("click", function() {
// get current mode
// don't use `.data("color-scheme")`, it doesn't refresh
$mode = $("#css").attr("data-color-scheme");
// test if this is a first time click event, if so initialise the code
if (typeof $mode === 'undefined') {
// not defined yet - set pref. & ask the browser if alt. is active
$mode = 'light';
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) $mode = 'dark';
init_color_scheme_css("css", $mode);
// `init_color_scheme_css()` any other CSS
}
// by here we have the current mode, so swap it
$new_mode = ($mode == 'dark') ? 'light' : 'dark';
toggle_color_scheme_css("css", $new_mode);
// `toggle_color_scheme_css()` any other CSS
});
});
</script>
</code></pre></div>
<p><b>Remember</b> that the above code does not include a persistence layer to remember a user toggled preference.</p>
</div>
</div><div class="row">
<div class="col-sm">
<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/keep-markup/prism-keep-markup.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>
<!-- 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({html: true})
.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')
});
// Update the toggle button based on current color scheme
// ----------------------------------------
function updateDarkToggleButton() {
$dark = (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches);
$("#css-toggle-btn").prop( "checked", $dark );
}
// Update on first load.
updateDarkToggleButton();
// and every time it changes
if (window.matchMedia) window.matchMedia("(prefers-color-scheme: dark)").addListener( updateDarkToggleButton );
// Color Scheme toggle botton
// ----------------------------------------
// function to initialise the css
function init_color_scheme_css($id, $mode) {
if ($("#"+$id)) $("#"+$id).remove(); // remove exitsing id
$("#"+$id+"-"+$mode).attr( {
"data-href-light": $("#"+$id+"-light").attr("href"), // store the light CSS url
"data-href-dark": $("#"+$id+"-dark").attr("href"), // store the dark CSS url
"data-color-scheme": $mode, // store the mode, so that we don't re-initalise
"media": "all", // drop the media filter
"id": $id // rename the id (drop the `-{mode}` bit)
} );
$other = ($mode == 'dark') ? 'light' : 'dark';
$("#"+$id+"-"+$other).remove();
}
function toggle_color_scheme_css($id, $mode) {
// grab the new mode css href
$href = $("#"+$id).data("href-"+$mode);
// set the CSS to the mode preference.
$("#"+$id).attr( {
"href": $href,
"data-color-scheme": $mode,
});
}
// toggle button click code
$("#css-toggle-btn").bind("click", function() {
// get current mode
// don't use `.data("color-scheme")`, it doesn't refresh
$mode = $("#css").attr("data-color-scheme");
// test if this is a first time click event, if so initialise the code
if (typeof $mode === 'undefined') {
// not defined yet - set pref. & ask the browser if alt. is active
$mode = 'light';
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) $mode = 'dark';
init_color_scheme_css("css", $mode);
init_color_scheme_css("prism", $mode);
}
// by here we have the current mode, so swap it
$mode = ($mode == 'dark') ? 'light' : 'dark';
toggle_color_scheme_css("css", $mode);
toggle_color_scheme_css("prism", $mode);
});
// Press [D] hot-key to toggle
$(document).bind('keypress', 'd', function(){
$('#css-toggle-btn').click();
});
$("#css-toggle-btn").parent().attr({"data-original-title": $("#css-toggle-btn").parent().attr("data-original-title") + "<small><br>or press [D] hot-key</small>"});
// ----- Nav & Copyright ------------------
$.ajax( {
url: './navbar.htm', type: 'get',
success: function(content) {
$("#nav-links").html( content );
$("#nav-tests,#nav-night").addClass("active");
}
} );
$.ajax( {
url: './copyright.htm', type: 'get',
success: function(content) {
$("#copyright").html( content );
}
} );
});
</script>
</body>
</html>