22import { AUTO_MODE , DARK_MODE , LIGHT_MODE } from " @constants/constants.ts" ;
33import I18nKey from " @i18n/i18nKey" ;
44import { i18n } from " @i18n/translation" ;
5- import Icon from " @iconify/svelte" ;
5+ import Icon , { addIcon } from " @iconify/svelte" ;
6+ import icons from ' @iconify-json/material-symbols/icons.json' ;
7+ // Preload icons to avoid CDN requests
8+ [' wb-sunny-outline-rounded' , ' dark-mode-outline-rounded' , ' radio-button-partial-outline' ].forEach (name => {
9+ if (icons .icons [name ]) {
10+ addIcon (` material-symbols:${name } ` , { body: icons .icons [name ].body , width: icons .width , height: icons .height });
11+ }
12+ });
613import {
714 applyThemeToDocument ,
815 getStoredTheme ,
@@ -11,11 +18,30 @@ import {
1118import { onMount } from " svelte" ;
1219import type { LIGHT_DARK_MODE } from " @/types/config.ts" ;
1320
21+ let {} = $props ();
22+
1423const seq: LIGHT_DARK_MODE [] = [LIGHT_MODE , DARK_MODE , AUTO_MODE ];
15- let mode: LIGHT_DARK_MODE = $state (AUTO_MODE );
24+ let mode: LIGHT_DARK_MODE = $state (
25+ typeof document !== ' undefined'
26+ ? (document .documentElement .getAttribute (' data-theme-mode' ) as LIGHT_DARK_MODE || AUTO_MODE )
27+ : AUTO_MODE
28+ );
1629
1730onMount (() => {
1831 mode = getStoredTheme ();
32+ document .documentElement .setAttribute (' data-theme-mode' , mode );
33+
34+ // Wire up the button that's rendered in Navbar.astro
35+ const button = document .getElementById (' scheme-switch' );
36+ const wrapper = document .getElementById (' theme-switch-wrapper' );
37+ if (button ) {
38+ button .onclick = toggleScheme ;
39+ button .onmouseenter = showPanel ;
40+ }
41+ if (wrapper ) {
42+ wrapper .onmouseleave = hidePanel ;
43+ }
44+
1945 const darkModePreference = window .matchMedia (" (prefers-color-scheme: dark)" );
2046 const changeThemeWhenSchemeChanged: Parameters <
2147 typeof darkModePreference .addEventListener <" change" >
@@ -34,6 +60,7 @@ onMount(() => {
3460function switchScheme(newMode : LIGHT_DARK_MODE ) {
3561 mode = newMode ;
3662 setTheme (newMode );
63+ document .documentElement .setAttribute (' data-theme-mode' , newMode );
3764}
3865
3966function toggleScheme() {
@@ -57,43 +84,29 @@ function hidePanel() {
5784}
5885 </script >
5986
60- <!-- z-50 make the panel higher than other float panels -->
61- <div class ="relative z-50" role ="menu" tabindex ="-1" onmouseleave ={hidePanel }>
62- <button aria-label ="Light/Dark Mode" role ="menuitem" class ="relative btn-plain scale-animation rounded-lg h-11 w-11 active:scale-90" id ="scheme-switch" onclick ={toggleScheme } onmouseenter ={showPanel }>
63- <div class ="absolute" class:opacity- 0={mode !== LIGHT _MODE}>
64- <Icon icon =" material-symbols:wb-sunny-outline-rounded" class =" text-[1.25rem]" ></Icon >
65- </div >
66- <div class ="absolute" class:opacity- 0={mode !== DARK _MODE}>
67- <Icon icon =" material-symbols:dark-mode-outline-rounded" class =" text-[1.25rem]" ></Icon >
68- </div >
69- <div class ="absolute" class:opacity- 0={mode !== AUTO _MODE}>
70- <Icon icon =" material-symbols:radio-button-partial-outline" class =" text-[1.25rem]" ></Icon >
71- </div >
72- </button >
73-
74- <div id =" light-dark-panel" class =" hidden lg:block absolute transition float-panel-closed top-11 -right-2 pt-5" >
75- <div class =" card-base float-panel p-2" >
76- <button class =" flex transition whitespace-nowrap items-center !justify-start w-full btn-plain scale-animation rounded-lg h-9 px-3 font-medium active:scale-95 mb-0.5"
77- class:current-theme-btn ={mode === LIGHT_MODE }
78- onclick ={() => switchScheme (LIGHT_MODE )}
79- >
80- <Icon icon =" material-symbols:wb-sunny-outline-rounded" class =" text-[1.25rem] mr-3" ></Icon >
81- {i18n (I18nKey .lightMode )}
82- </button >
83- <button class =" flex transition whitespace-nowrap items-center !justify-start w-full btn-plain scale-animation rounded-lg h-9 px-3 font-medium active:scale-95 mb-0.5"
84- class:current-theme-btn ={mode === DARK_MODE }
85- onclick ={() => switchScheme (DARK_MODE )}
86- >
87- <Icon icon =" material-symbols:dark-mode-outline-rounded" class =" text-[1.25rem] mr-3" ></Icon >
88- {i18n (I18nKey .darkMode )}
89- </button >
90- <button class =" flex transition whitespace-nowrap items-center !justify-start w-full btn-plain scale-animation rounded-lg h-9 px-3 font-medium active:scale-95"
91- class:current-theme-btn ={mode === AUTO_MODE }
92- onclick ={() => switchScheme (AUTO_MODE )}
93- >
94- <Icon icon =" material-symbols:radio-button-partial-outline" class =" text-[1.25rem] mr-3" ></Icon >
95- {i18n (I18nKey .systemMode )}
96- </button >
97- </div >
87+ <!-- Button is rendered in Navbar.astro for instant display -->
88+ <div id =" light-dark-panel" class =" hidden lg:block absolute transition float-panel-closed top-11 -right-2 pt-5" role =" menu" tabindex =" -1" >
89+ <div class =" card-base float-panel p-2" >
90+ <button class =" flex transition whitespace-nowrap items-center !justify-start w-full btn-plain scale-animation rounded-lg h-9 px-3 font-medium active:scale-95 mb-0.5"
91+ class:current-theme-btn ={mode === LIGHT_MODE }
92+ onclick ={() => switchScheme (LIGHT_MODE )}
93+ >
94+ <Icon icon =" material-symbols:wb-sunny-outline-rounded" class =" text-[1.25rem] mr-3" ></Icon >
95+ {i18n (I18nKey .lightMode )}
96+ </button >
97+ <button class =" flex transition whitespace-nowrap items-center !justify-start w-full btn-plain scale-animation rounded-lg h-9 px-3 font-medium active:scale-95 mb-0.5"
98+ class:current-theme-btn ={mode === DARK_MODE }
99+ onclick ={() => switchScheme (DARK_MODE )}
100+ >
101+ <Icon icon =" material-symbols:dark-mode-outline-rounded" class =" text-[1.25rem] mr-3" ></Icon >
102+ {i18n (I18nKey .darkMode )}
103+ </button >
104+ <button class =" flex transition whitespace-nowrap items-center !justify-start w-full btn-plain scale-animation rounded-lg h-9 px-3 font-medium active:scale-95"
105+ class:current-theme-btn ={mode === AUTO_MODE }
106+ onclick ={() => switchScheme (AUTO_MODE )}
107+ >
108+ <Icon icon =" material-symbols:radio-button-partial-outline" class =" text-[1.25rem] mr-3" ></Icon >
109+ {i18n (I18nKey .systemMode )}
110+ </button >
98111 </div >
99112</div >
0 commit comments