@@ -201,52 +201,47 @@ if (search_form) {
201201/**
202202 * Light / Dark mode
203203 */
204- window . matchMedia ( '(prefers-color-scheme: dark)' ) . addEventListener ( 'change' , event => {
205- if ( localStorage . theme === 'system' && event . matches ) {
206- document . documentElement . classList . add ( 'dark' ) ;
204+ const update_theme = ( ) => {
205+ const theme = localStorage . getItem ( 'theme' ) || 'system' ;
206+ let current_theme = theme ;
207+
208+ if ( theme === 'system' ) {
209+ current_theme = window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ? 'dark' : 'light' ;
210+ document . documentElement . setAttribute ( 'color-theme' , 'system' ) ;
207211 } else {
208- document . documentElement . classList . remove ( 'dark' ) ;
212+ document . documentElement . setAttribute ( 'color-theme' , theme ) ;
209213 }
210- } ) ;
211214
212- const update_theme = ( ) => {
213- if ( ! ( 'theme' in localStorage ) ) {
214- localStorage . theme = 'system' ;
215- }
215+ document . documentElement . classList . toggle ( 'dark' , current_theme === 'dark' ) ;
216216
217- switch ( localStorage . theme ) {
218- case 'system' :
219- if ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ) {
220- document . documentElement . classList . add ( 'dark' ) ;
221- } else {
222- document . documentElement . classList . remove ( 'dark' ) ;
223- }
224- break ;
225- case 'dark' :
226- document . documentElement . classList . add ( 'dark' ) ;
227- break ;
228- case 'light' :
229- document . documentElement . classList . remove ( 'dark' ) ;
230- break ;
231- }
232- }
217+ const theme_colors = { light : '#fff' , dark : '#1f2937' } ;
218+ document . querySelector ( "meta[name='theme-color']" ) . content = theme_colors [ current_theme ]
219+ } ;
233220
234- update_theme ( ) ;
221+ const init_theme_switcher = ( ) => {
222+ const theme_switchers = document . querySelectorAll ( "[data-theme]" ) ;
235223
236- const theme_sw = document . querySelectorAll ( "[data-theme]" ) ;
224+ theme_switchers . forEach ( button => {
225+ const theme = button . getAttribute ( 'data-theme' ) ;
237226
238- theme_sw . forEach ( button => {
239- let theme = button . getAttribute ( 'data-theme' ) ;
227+ button . addEventListener ( 'click' , ( ) => {
228+ localStorage . setItem ( 'theme' , theme ) ;
229+ update_theme ( ) ;
230+ theme_switchers . forEach ( btn => btn . classList . remove ( 'active' ) ) ;
231+ button . classList . add ( 'active' ) ;
232+ } ) ;
240233
241- button . addEventListener ( 'click' , ( ) => {
242- localStorage . theme = theme ;
243- update_theme ( ) ;
244- theme_sw . forEach ( btn => btn . classList . remove ( 'active' ) ) ;
234+ if ( theme === localStorage . getItem ( 'theme' ) ) {
235+ button . classList . add ( 'active' ) ;
236+ }
237+ } ) ;
245238
246- button . classList . add ( 'active' ) ;
239+ window . matchMedia ( '(prefers-color-scheme: dark)' ) . addEventListener ( 'change' , ( ) => {
240+ if ( localStorage . getItem ( 'theme' ) === 'system' ) {
241+ update_theme ( ) ;
242+ }
247243 } ) ;
244+ } ;
248245
249- if ( theme === localStorage . theme ) {
250- button . classList . add ( 'active' ) ;
251- }
252- } ) ;
246+ update_theme ( ) ;
247+ init_theme_switcher ( ) ;
0 commit comments