@@ -7,9 +7,9 @@ const THEMES = ['system', 'dark', 'light']
77/**
88 * Sets initial night mode state and registers to settings updates.
99 */
10- export function initialize ( ) {
10+ export function initialize ( theme ) {
1111 settingsStore . getAndSubscribe ( settings => {
12- document . body . classList . toggle ( DARK_MODE_CLASS , shouldUseDarkMode ( settings ) )
12+ document . body . classList . toggle ( DARK_MODE_CLASS , shouldUseDarkMode ( theme || settings . theme ) )
1313 } )
1414 listenToDarkMode ( )
1515}
@@ -18,18 +18,20 @@ export function initialize () {
1818 * Cycles through themes and saves the preference.
1919 */
2020export function cycleTheme ( ) {
21- const settings = settingsStore . get ( )
22- const currentTheme = settings . theme || 'system'
23- const nextTheme = THEMES [ THEMES . indexOf ( currentTheme ) + 1 ] || THEMES [ 0 ]
21+ const nextTheme = THEMES [ THEMES . indexOf ( currentTheme ( ) ) + 1 ] || THEMES [ 0 ]
2422 settingsStore . update ( { theme : nextTheme } )
2523 showToast ( `Set theme to "${ nextTheme } "` )
2624}
2725
28- function shouldUseDarkMode ( settings ) {
26+ export function currentTheme ( ) {
27+ return settingsStore . get ( ) . theme || 'system'
28+ }
29+
30+ function shouldUseDarkMode ( theme ) {
2931 // nightMode used to be true|false|null
3032 // Now it's 'dark'|'light'|'system'|null with null treated as 'system'
31- return ( settings . theme === 'dark' ) ||
32- ( prefersDarkColorScheme ( ) && ( settings . theme == null || settings . theme === 'system' ) )
33+ return ( theme === 'dark' ) ||
34+ ( prefersDarkColorScheme ( ) && ( theme == null || theme === 'system' ) )
3335}
3436
3537function prefersDarkColorScheme ( ) {
@@ -38,9 +40,9 @@ function prefersDarkColorScheme () {
3840
3941function listenToDarkMode ( ) {
4042 window . matchMedia ( '(prefers-color-scheme: dark)' ) . addListener ( _e => {
41- const settings = settingsStore . get ( )
42- const isNight = shouldUseDarkMode ( settings )
43- if ( settings . theme == null || settings . theme === 'system' ) {
43+ const theme = settingsStore . get ( ) . theme
44+ const isNight = shouldUseDarkMode ( theme )
45+ if ( theme == null || theme === 'system' ) {
4446 document . body . classList . toggle ( DARK_MODE_CLASS , isNight )
4547 showToast ( `Browser changed theme to "${ isNight ? 'dark' : 'light' } "` )
4648 }
0 commit comments