@@ -309,7 +309,13 @@ function playground_text(playground, hidden = true) {
309309 themePopup . querySelectorAll ( '.theme-selected' ) . forEach ( function ( el ) {
310310 el . classList . remove ( 'theme-selected' ) ;
311311 } ) ;
312- themePopup . querySelector ( "button#" + get_theme ( ) ) . classList . add ( 'theme-selected' ) ;
312+ const selected = get_saved_theme ( ) ?? "default_theme" ;
313+ var element = themePopup . querySelector ( "button#" + selected ) ;
314+ if ( element === null ) {
315+ // Fall back in case there is no "Default" item.
316+ element = themePopup . querySelector ( "button#" + get_theme ( ) ) ;
317+ }
318+ element . classList . add ( 'theme-selected' ) ;
313319 }
314320
315321 function hideThemes ( ) {
@@ -318,9 +324,18 @@ function playground_text(playground, hidden = true) {
318324 themeToggleButton . focus ( ) ;
319325 }
320326
321- function get_theme ( ) {
322- var theme ;
327+ function get_saved_theme ( ) {
328+ var theme = null ;
323329 try { theme = localStorage . getItem ( 'mdbook-theme' ) ; } catch ( e ) { }
330+ return theme ;
331+ }
332+
333+ function delete_saved_theme ( ) {
334+ localStorage . removeItem ( 'mdbook-theme' ) ;
335+ }
336+
337+ function get_theme ( ) {
338+ var theme = get_saved_theme ( ) ;
324339 if ( theme === null || theme === undefined || ! themeIds . includes ( theme ) ) {
325340 if ( typeof default_dark_theme === 'undefined' ) {
326341 // A customized index.hbs might not define this, so fall back to
@@ -402,7 +417,12 @@ function playground_text(playground, hidden = true) {
402417 } else {
403418 return ;
404419 }
405- set_theme ( theme ) ;
420+ if ( theme === "default_theme" || theme == null ) {
421+ delete_saved_theme ( ) ;
422+ set_theme ( get_theme ( ) , false ) ;
423+ } else {
424+ set_theme ( theme ) ;
425+ }
406426 } ) ;
407427
408428 themePopup . addEventListener ( 'focusout' , function ( e ) {
0 commit comments