Skip to content

Commit 2e11db5

Browse files
committed
Switch theme when preferred color scheme changes
1 parent b0cf568 commit 2e11db5

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/theme/book.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,20 @@ function playground_text(playground, hidden = true) {
322322
var theme;
323323
try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
324324
if (theme === null || theme === undefined || !themeIds.includes(theme)) {
325-
return default_theme;
325+
if (typeof default_dark_theme === 'undefined') {
326+
// A customized index.hbs might not define this, so fall back to
327+
// old behavior of determining the default on page load.
328+
return default_theme;
329+
}
330+
return window.matchMedia("(prefers-color-scheme: dark)").matches
331+
? default_dark_theme
332+
: default_light_theme;
326333
} else {
327334
return theme;
328335
}
329336
}
330337

338+
var previousTheme = default_theme;
331339
function set_theme(theme, store = true) {
332340
let ace_theme;
333341

@@ -359,21 +367,23 @@ function playground_text(playground, hidden = true) {
359367
});
360368
}
361369

362-
var previousTheme = get_theme();
363-
364370
if (store) {
365371
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
366372
}
367373

368374
html.classList.remove(previousTheme);
369375
html.classList.add(theme);
376+
previousTheme = theme;
370377
updateThemeSelected();
371378
}
372379

373-
// Set theme
374-
var theme = get_theme();
380+
const query = window.matchMedia("(prefers-color-scheme: dark)");
381+
query.onchange = function(event) {
382+
set_theme(get_theme(), false);
383+
};
375384

376-
set_theme(theme, false);
385+
// Set theme.
386+
set_theme(get_theme(), false);
377387

378388
themeToggleButton.addEventListener('click', function () {
379389
if (themePopup.style.display === 'block') {

src/theme/index.hbs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@
5353
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
5454
{{/if}}
5555

56-
<!-- Provide site root to javascript -->
56+
<!-- Provide site root and default themes to javascript -->
5757
<script>
58-
var path_to_root = "{{ path_to_root }}";
59-
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "{{ preferred_dark_theme }}" : "{{ default_theme }}";
58+
const path_to_root = "{{ path_to_root }}";
59+
const default_light_theme = "{{ default_theme }}";
60+
const default_dark_theme = "{{ preferred_dark_theme }}";
6061
</script>
6162
<!-- Start loading toc.js asap -->
6263
<script src="{{ resource "toc.js" }}"></script>
@@ -81,6 +82,7 @@
8182

8283
<!-- Set the theme before any content is loaded, prevents flash -->
8384
<script>
85+
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
8486
var theme;
8587
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
8688
if (theme === null || theme === undefined) { theme = default_theme; }

0 commit comments

Comments
 (0)