@@ -490,16 +490,86 @@ function searchLoaded(index, docs) {
490490
491491// Switch theme
492492
493+ jtd . themeStorageKey = 'jtd-theme' ;
494+
495+ jtd . syncThemeClass = function ( theme ) {
496+ var isDark = theme == 'dark' ;
497+ document . documentElement . classList . toggle ( 'dark' , isDark ) ;
498+ document . documentElement . classList . toggle ( 'light' , ! isDark ) ;
499+
500+ if ( document . body ) {
501+ document . body . classList . toggle ( 'dark' , isDark ) ;
502+ document . body . classList . toggle ( 'light' , ! isDark ) ;
503+ }
504+
505+ var toggles = document . querySelectorAll ( '.theme-toggle' ) ;
506+ toggles . forEach ( function ( toggle ) {
507+ var label = isDark ? 'Switch to light mode' : 'Switch to dark mode' ;
508+ toggle . setAttribute ( 'aria-label' , label ) ;
509+ toggle . setAttribute ( 'title' , label ) ;
510+ toggle . setAttribute ( 'aria-pressed' , isDark ) ;
511+ } ) ;
512+ }
513+
514+ jtd . storedTheme = function ( ) {
515+ try {
516+ return localStorage . getItem ( jtd . themeStorageKey ) ;
517+ } catch ( e ) {
518+ return null ;
519+ }
520+ }
521+
522+ jtd . preferredTheme = function ( ) {
523+ var storedTheme = jtd . storedTheme ( ) ;
524+ if ( storedTheme == 'dark' || storedTheme == 'light' ) {
525+ return storedTheme == 'light' ? 'default' : storedTheme ;
526+ }
527+
528+ if ( window . matchMedia && window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ) {
529+ return 'dark' ;
530+ }
531+
532+ return 'default' ;
533+ }
534+
493535jtd . getTheme = function ( ) {
494536 var cssFileHref = document . querySelector ( '[rel="stylesheet"]' ) . getAttribute ( 'href' ) ;
495537 return cssFileHref . substring ( cssFileHref . lastIndexOf ( '-' ) + 1 , cssFileHref . length - 4 ) ;
496538}
497539
498540jtd . setTheme = function ( theme ) {
499541 var cssFile = document . querySelector ( '[rel="stylesheet"]' ) ;
500- cssFile . setAttribute ( 'href' , '{{ "assets/css/just-the-docs-" | relative_url }}' + theme + '.css' ) ;
542+ theme = theme == 'datatalks' ? 'default' : theme ;
543+ cssFile . setAttribute ( 'href' , '{{ "/assets/css/just-the-docs-" | relative_url }}' + theme + '.css' ) ;
544+ jtd . syncThemeClass ( theme ) ;
545+ }
546+
547+ jtd . saveTheme = function ( theme ) {
548+ try {
549+ localStorage . setItem ( jtd . themeStorageKey , theme ) ;
550+ } catch ( e ) {
551+ }
552+ }
553+
554+ jtd . toggleTheme = function ( ) {
555+ var theme = jtd . getTheme ( ) == 'dark' ? 'default' : 'dark' ;
556+ jtd . setTheme ( theme ) ;
557+ jtd . saveTheme ( theme == 'default' ? 'light' : theme ) ;
501558}
502559
560+ jtd . setTheme ( jtd . preferredTheme ( ) ) ;
561+
562+ jtd . onReady ( function ( ) {
563+ jtd . syncThemeClass ( jtd . getTheme ( ) ) ;
564+
565+ var toggles = document . querySelectorAll ( '.theme-toggle' ) ;
566+ toggles . forEach ( function ( toggle ) {
567+ jtd . addEvent ( toggle , 'click' , function ( ) {
568+ jtd . toggleTheme ( ) ;
569+ } ) ;
570+ } ) ;
571+ } ) ;
572+
503573// Note: pathname can have a trailing slash on a local jekyll server
504574// and not have the slash on GitHub Pages
505575
0 commit comments