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-nightfall.html
331 lines (298 loc) · 13.4 KB
/
test-nightfall.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
<!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 id="css" rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link id="css-dark" rel="stylesheet" href="./dist/bootstrap-night.css" media="(prefers-color-scheme: dark)">
<!-- Prism.js for syntax highlighting -->
<link rel="stylesheet/less" type="text/css" href="./css/prism-combo.less">
<title>bootstrap-nightfall</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 a base CSS theme file, and a <i>deltas</i> add-on</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>A fully themed Bootstrap CSS as a base file</li>
<li>And a <i>deltas</i> add-on CSS with only the changes, filtered by stylesheet media filter</li>
<li>Does not require jQuery</li>
<li>Does not require fallback code</li>
</ul>
<p>Replace the bootstrap stylesheet with the following code:</p>
<div class="code"><pre><code class="language-html language-js"><!-- 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">
<!-- Load the primary CSS first ... -->
<link <span class="collapse more-code" id="code-css-light">id="css-light" </span>rel="stylesheet" href="bootstrap.css">
<!-- ... and then the alternate CSS first as a snap-on for dark color scheme preference -->
<link <span class="collapse more-code" id="code-css-dark">id="css-dark" </span>rel="stylesheet" href="bootstrap-nightfall.css" media="(prefers-color-scheme: dark)">
</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-light code-css-dark" >some additional code</a>.</p>
</div>
</div><div class="row mt-5 collapse more-code">
<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), detecting this state and setting a `<code>dark</code>` data variable in the body.
We also update the toggle switch in the UI.
Once this switch has been clicked on, iether enable or disable the snap-on CSS based on the 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() {
if (typeof $("body").attr("data-color-scheme") === 'undefined') {
$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 toggle the css
function toggle_color_scheme_css($id, $mode) {
$dark = ($mode == 'dark') ? true : false;
$("#"+$id+"-dark").attr( "disabled", !$dark );
$("body").attr( "data-color-scheme", ($dark ? "dark" : "light" ) );
}
// function to initialise the css
function init_color_scheme_css($id, $mode) {
$dark = ($mode == 'dark') ? true : false;
toggle_color_scheme_css($id, $mode);
setTimeout(function(){ // let the browser catch up
$("#"+$id+"-dark").removeAttr("media");
}, 100);
}
// toggle button click code
$("#css-toggle-btn").bind("click", function() {
// get current mode
// don't use `.data("color-scheme")`, it doesn't refresh
$mode = $("body").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);
}
// by here we have the current mode, so swap it
$mode = ($mode == 'dark') ? 'light' : 'dark';
toggle_color_scheme_css("css", $mode);
});
});
</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>
<!-- 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({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 updateDarkHtml($dark) {
if ($dark) {
$("html").removeClass('light').addClass("dark");
} else {
$("html").addClass('light').removeClass("dark");
}
}
function updateDarkToggleButton() {
if (typeof $("body").attr("data-color-scheme") === 'undefined') {
$dark = (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches);
$("#css-toggle-btn").prop( "checked", $dark );
updateDarkHtml($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 toggle the css
function toggle_color_scheme_css($id, $mode) {
$dark = ($mode == 'dark') ? true : false;
$("#"+$id+"-dark").attr( "disabled", !$dark );
$("body").attr( "data-color-scheme", ($dark ? "dark" : "light" ) );
updateDarkHtml($dark);
}
// function to initialise the css
function init_color_scheme_css($id, $mode) {
$dark = ($mode == 'dark') ? true : false;
toggle_color_scheme_css($id, $mode);
setTimeout(function(){ // let the browser catch up
$("#"+$id+"-dark").removeAttr("media");
}, 100);
}
// toggle button click code
$("#css-toggle-btn").bind("click", function() {
// get current mode
// don't use `.data("color-scheme")`, it doesn't refresh
$mode = $("body").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);
}
// by here we have the current mode, so swap it
$mode = ($mode == 'dark') ? 'light' : 'dark';
toggle_color_scheme_css("css", $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-nightfall").addClass("active");
}
} );
$.ajax( {
url: './copyright.htm', type: 'get',
success: function(content) {
$("#copyright").html( content );
}
} );
});
</script>
</body>
</html>