1
1
'use strict' ;
2
2
3
- /* global default_theme, hljs, ClipboardJS */
3
+ /* global default_theme, default_dark_theme, default_light_theme, hljs, ClipboardJS */
4
4
5
5
// Fix back button cache problem
6
6
window . onunload = function ( ) { } ;
@@ -329,7 +329,13 @@ aria-label="Show hidden lines"></button>';
329
329
themePopup . querySelectorAll ( '.theme-selected' ) . forEach ( function ( el ) {
330
330
el . classList . remove ( 'theme-selected' ) ;
331
331
} ) ;
332
- themePopup . querySelector ( 'button#' + get_theme ( ) ) . classList . add ( 'theme-selected' ) ;
332
+ const selected = get_saved_theme ( ) ?? 'default_theme' ;
333
+ let element = themePopup . querySelector ( 'button#' + selected ) ;
334
+ if ( element === null ) {
335
+ // Fall back in case there is no "Default" item.
336
+ element = themePopup . querySelector ( 'button#' + get_theme ( ) ) ;
337
+ }
338
+ element . classList . add ( 'theme-selected' ) ;
333
339
}
334
340
335
341
function hideThemes ( ) {
@@ -338,20 +344,37 @@ aria-label="Show hidden lines"></button>';
338
344
themeToggleButton . focus ( ) ;
339
345
}
340
346
341
- function get_theme ( ) {
342
- let theme ;
347
+ function get_saved_theme ( ) {
348
+ let theme = null ;
343
349
try {
344
350
theme = localStorage . getItem ( 'mdbook-theme' ) ;
345
351
} catch ( e ) {
346
352
// ignore error.
347
353
}
354
+ return theme ;
355
+ }
356
+
357
+ function delete_saved_theme ( ) {
358
+ localStorage . removeItem ( 'mdbook-theme' ) ;
359
+ }
360
+
361
+ function get_theme ( ) {
362
+ const theme = get_saved_theme ( ) ;
348
363
if ( theme === null || theme === undefined || ! themeIds . includes ( theme ) ) {
349
- return default_theme ;
364
+ if ( typeof default_dark_theme === 'undefined' ) {
365
+ // A customized index.hbs might not define this, so fall back to
366
+ // old behavior of determining the default on page load.
367
+ return default_theme ;
368
+ }
369
+ return window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches
370
+ ? default_dark_theme
371
+ : default_light_theme ;
350
372
} else {
351
373
return theme ;
352
374
}
353
375
}
354
376
377
+ let previousTheme = default_theme ;
355
378
function set_theme ( theme , store = true ) {
356
379
let ace_theme ;
357
380
@@ -383,8 +406,6 @@ aria-label="Show hidden lines"></button>';
383
406
} ) ;
384
407
}
385
408
386
- const previousTheme = get_theme ( ) ;
387
-
388
409
if ( store ) {
389
410
try {
390
411
localStorage . setItem ( 'mdbook-theme' , theme ) ;
@@ -395,13 +416,17 @@ aria-label="Show hidden lines"></button>';
395
416
396
417
html . classList . remove ( previousTheme ) ;
397
418
html . classList . add ( theme ) ;
419
+ previousTheme = theme ;
398
420
updateThemeSelected ( ) ;
399
421
}
400
422
401
- // Set theme
402
- const theme = get_theme ( ) ;
423
+ const query = window . matchMedia ( '(prefers-color-scheme: dark)' ) ;
424
+ query . onchange = function ( ) {
425
+ set_theme ( get_theme ( ) , false ) ;
426
+ } ;
403
427
404
- set_theme ( theme , false ) ;
428
+ // Set theme.
429
+ set_theme ( get_theme ( ) , false ) ;
405
430
406
431
themeToggleButton . addEventListener ( 'click' , function ( ) {
407
432
if ( themePopup . style . display === 'block' ) {
@@ -420,7 +445,12 @@ aria-label="Show hidden lines"></button>';
420
445
} else {
421
446
return ;
422
447
}
423
- set_theme ( theme ) ;
448
+ if ( theme === 'default_theme' || theme === null ) {
449
+ delete_saved_theme ( ) ;
450
+ set_theme ( get_theme ( ) , false ) ;
451
+ } else {
452
+ set_theme ( theme ) ;
453
+ }
424
454
} ) ;
425
455
426
456
themePopup . addEventListener ( 'focusout' , function ( e ) {
0 commit comments