@@ -346,12 +346,20 @@ aria-label="Show hidden lines"></button>';
346
346
// ignore error.
347
347
}
348
348
if ( theme === null || theme === undefined || ! themeIds . includes ( theme ) ) {
349
- return default_theme ;
349
+ if ( typeof default_dark_theme === 'undefined' ) {
350
+ // A customized index.hbs might not define this, so fall back to
351
+ // old behavior of determining the default on page load.
352
+ return default_theme ;
353
+ }
354
+ return window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches
355
+ ? default_dark_theme
356
+ : default_light_theme ;
350
357
} else {
351
358
return theme ;
352
359
}
353
360
}
354
361
362
+ var previousTheme = default_theme ;
355
363
function set_theme ( theme , store = true ) {
356
364
let ace_theme ;
357
365
@@ -383,8 +391,6 @@ aria-label="Show hidden lines"></button>';
383
391
} ) ;
384
392
}
385
393
386
- const previousTheme = get_theme ( ) ;
387
-
388
394
if ( store ) {
389
395
try {
390
396
localStorage . setItem ( 'mdbook-theme' , theme ) ;
@@ -395,13 +401,17 @@ aria-label="Show hidden lines"></button>';
395
401
396
402
html . classList . remove ( previousTheme ) ;
397
403
html . classList . add ( theme ) ;
404
+ previousTheme = theme ;
398
405
updateThemeSelected ( ) ;
399
406
}
400
407
401
- // Set theme
402
- const theme = get_theme ( ) ;
408
+ const query = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
409
+ query . onchange = function ( event ) {
410
+ set_theme ( get_theme ( ) , false ) ;
411
+ } ;
403
412
404
- set_theme ( theme , false ) ;
413
+ // Set theme.
414
+ set_theme ( get_theme ( ) , false ) ;
405
415
406
416
themeToggleButton . addEventListener ( 'click' , function ( ) {
407
417
if ( themePopup . style . display === 'block' ) {
0 commit comments