@@ -10,37 +10,31 @@ const themeWatcher = watchColorSchemeChange((colorScheme) => {
1010 const systemTheme = localStorage . getItem ( 'system-theme' )
1111 // setting stored in local storage
1212 const localTheme = localStorage . getItem ( 'local-theme' )
13- // // if no local theme set - system is default
13+ // if no local theme set - system is default
1414 if ( localTheme === null ) {
1515 setTheme ( colorScheme )
16- localStorage . setItem ( 'system-theme' , colorScheme || 'light' )
16+ localStorage . setItem ( 'system-theme' , colorScheme )
1717 // page load - load any stored themes or set theme
1818 } else {
1919 // listen for system changes, update if any
2020 if ( colorScheme != systemTheme ) {
2121 setTheme ( colorScheme )
22- localStorage . setItem ( 'system-theme' , colorScheme || 'light' )
23- // override local theme
24- localStorage . removeItem ( 'local-theme' )
22+ localStorage . setItem ( 'system-theme' , colorScheme )
2523 } else {
2624 // else load local theme
27- if ( localTheme === 'light' ) {
28- lightModeOn ( )
29- } else if ( localTheme === 'dark' ) {
30- darkModeOn ( )
31- }
25+ setTheme ( localTheme ) ;
3226 }
3327 }
34- // wait for load then and add listner on button
28+ // wait for load then and add listener on button
3529 document . addEventListener ( 'DOMContentLoaded' , ( ) => {
36-
37- document
38- . querySelector ( '#theme-toggle' )
39- . addEventListener ( 'click ', toggleLocalStorageTheme )
30+ const themeToggle = document ?. querySelector ( '#theme-toggle' ) ;
31+ themeToggle . addEventListener ( 'click' , toggleLocalStorageTheme ) ;
32+ // set area-label for screen readers
33+ themeToggle . setAttribute ( 'aria-label ', colorScheme ? 'Switch to light mode' : 'Switch to dark mode' ) ;
4034 } )
4135 }
4236} )
43- // set the theme to given value
37+ // apply current theme
4438function setTheme ( theme ) {
4539 // only support dark else any other defaults to light
4640 if ( theme === 'dark' ) {
@@ -49,39 +43,26 @@ function setTheme(theme) {
4943 lightModeOn ( )
5044 }
5145}
52- // toggle btwn themes or set a theme if none set
53- function toggleLocalStorageTheme ( e ) {
54- const localTheme = localStorage . getItem ( 'local-theme' )
46+ // toggle btn themes
47+ function toggleLocalStorageTheme ( ) {
48+ const localTheme = localStorage . getItem ( 'local-theme' ) ;
5549 if ( localTheme === 'light' ) {
56- localStorage . setItem ( 'local-theme' , 'dark' )
57- darkModeOn ( )
58- } else if ( localTheme === 'dark' ) {
59- localStorage . setItem ( 'local-theme' , 'light' )
60- lightModeOn ( )
61- // localTheme still null
50+ setTheme ( 'dark' ) ;
6251 } else {
63- // need to check page state then set
64- if ( darkModeState ( ) ) {
65- localStorage . setItem ( 'local-theme' , 'light' )
66- lightModeOn ( )
67- } else {
68- localStorage . setItem ( 'local-theme' , 'dark' )
69- darkModeOn ( )
70- }
52+ setTheme ( 'light' ) ;
7153 }
7254}
7355function darkModeOn ( ) {
7456 document ?. documentElement ?. classList ?. remove ( 'light-mode' )
7557 document ?. documentElement ?. classList ?. add ( 'dark-mode' )
7658 updateThemeIcon ( 'dark' ) ;
59+ localStorage . setItem ( 'local-theme' , 'dark' ) ;
7760}
7861function lightModeOn ( ) {
7962 document ?. documentElement ?. classList . remove ( 'dark-mode' )
8063 document ?. documentElement ?. classList ?. add ( 'light-mode' )
8164 updateThemeIcon ( 'light' ) ;
82- }
83- function darkModeState ( ) {
84- return document ?. documentElement ?. classList . contains ( 'dark-mode' )
65+ localStorage . setItem ( 'local-theme' , 'light' ) ;
8566}
8667function hasLocalStorage ( ) {
8768 return typeof Storage !== 'undefined'
@@ -102,16 +83,15 @@ function watchColorSchemeChange(callback) {
10283}
10384
10485function updateThemeIcon ( theme ) {
105- const sun = document . getElementById ( 'icon-sun' ) ;
106- const moon = document . getElementById ( 'icon-moon' ) ;
107- if ( ! sun || ! moon ) return ;
108-
86+ const sun = document ? .getElementById ( 'icon-sun' ) ;
87+ const moon = document ? .getElementById ( 'icon-moon' ) ;
88+ const themeToggle = document ?. getElementById ( 'theme-toggle' ) ;
89+ if ( ! sun || ! moon || ! themeToggle ) return ;
10990 const isDark = theme === 'dark' ;
110-
111- // Show the icon representing the *next* theme
112- sun . style . display = isDark ? 'block' : 'none' ; // Show sun in dark mode
113- moon . style . display = isDark ? 'none' : 'block' ; // Show moon in light mode
11491 // improve accessibility for screen readers
92+ sun . hidden = ! isDark ;
93+ moon . hidden = isDark ;
11594 sun . setAttribute ( 'aria-hidden' , isDark ? 'false' : 'true' ) ;
11695 moon . setAttribute ( 'aria-hidden' , isDark ? 'true' : 'false' ) ;
96+ themeToggle . setAttribute ( 'aria-label' , isDark ? 'Switch to light mode' : 'Switch to dark mode' ) ;
11797} ;
0 commit comments