33 * The module is loaded on every page via BaseLayout/HomePage.
44 */
55
6- type Theme = 'dark' | 'light' ;
7-
8- declare global {
9- interface Window {
10- toggleTheme : ( ) => void ;
11- }
12- }
13-
146const THEME_STORAGE_KEY = 'theme' ;
157
16- const getStoredTheme = ( ) : Theme => {
8+ const getStoredTheme = ( ) => {
179 const stored = typeof localStorage !== 'undefined'
18- ? ( localStorage . getItem ( THEME_STORAGE_KEY ) as Theme | null )
10+ ? localStorage . getItem ( THEME_STORAGE_KEY )
1911 : null ;
2012 return stored === 'light' ? 'light' : 'dark' ;
2113} ;
2214
23- const setThemeAttribute = ( theme : Theme ) => {
15+ const setThemeAttribute = ( theme ) => {
2416 document . documentElement . setAttribute ( 'data-theme' , theme ) ;
2517} ;
2618
27- const syncThemeIcons = ( theme : Theme ) => {
19+ const syncThemeIcons = ( theme ) => {
2820 const icon = document . getElementById ( 'theme-icon' ) ;
2921 const mobileIcon = document . getElementById ( 'mobile-theme-icon' ) ;
3022 const iconText = theme === 'dark' ? '○' : '●' ;
@@ -33,7 +25,7 @@ const syncThemeIcons = (theme: Theme) => {
3325 if ( mobileIcon ) mobileIcon . textContent = iconText ;
3426} ;
3527
36- const applyTheme = ( theme : Theme , persist = false ) => {
28+ const applyTheme = ( theme , persist = false ) => {
3729 setThemeAttribute ( theme ) ;
3830 syncThemeIcons ( theme ) ;
3931 if ( persist ) {
@@ -44,18 +36,16 @@ const applyTheme = (theme: Theme, persist = false) => {
4436setThemeAttribute ( getStoredTheme ( ) ) ;
4537
4638const handleThemeToggle = ( ) => {
47- const currentAttr = document . documentElement . getAttribute ( 'data-theme' ) as Theme | null ;
39+ const currentAttr = document . documentElement . getAttribute ( 'data-theme' ) ;
4840 const current = currentAttr === 'light' ? 'light' : 'dark' ;
49- const next : Theme = current === 'dark' ? 'light' : 'dark' ;
41+ const next = current === 'dark' ? 'light' : 'dark' ;
5042 applyTheme ( next , true ) ;
5143} ;
5244
5345window . toggleTheme = handleThemeToggle ;
5446
55- type Cleanup = ( ) => void ;
56-
57- const setupThemeToggles = ( ) : Cleanup => {
58- const buttons = Array . from ( document . querySelectorAll < HTMLButtonElement > ( '.theme-toggle' ) ) ;
47+ const setupThemeToggles = ( ) => {
48+ const buttons = Array . from ( document . querySelectorAll ( '.theme-toggle' ) ) ;
5949
6050 if ( buttons . length === 0 ) {
6151 return ( ) => { } ;
@@ -72,15 +62,15 @@ const setupThemeToggles = (): Cleanup => {
7262 } ;
7363} ;
7464
75- const setupMobileNavigation = ( ) : Cleanup => {
76- const navToggle = document . getElementById ( 'mobile-nav-toggle' ) as HTMLButtonElement | null ;
65+ const setupMobileNavigation = ( ) => {
66+ const navToggle = document . getElementById ( 'mobile-nav-toggle' ) ;
7767 const navMenu = document . getElementById ( 'mobile-nav-menu' ) ;
7868
7969 if ( ! navToggle || ! navMenu ) {
8070 return ( ) => { } ;
8171 }
8272
83- const links = Array . from ( navMenu . querySelectorAll < HTMLAnchorElement > ( 'a' ) ) ;
73+ const links = Array . from ( navMenu . querySelectorAll ( 'a' ) ) ;
8474
8575 const openMenu = ( ) => {
8676 navMenu . classList . add ( 'is-open' ) ;
@@ -102,14 +92,14 @@ const setupMobileNavigation = (): Cleanup => {
10292 }
10393 } ;
10494
105- const handleDocumentClick = ( event : MouseEvent ) => {
106- const target = event . target as Node | null ;
95+ const handleDocumentClick = ( event ) => {
96+ const target = event . target ;
10797 if ( ! navMenu . classList . contains ( 'is-open' ) ) return ;
10898 if ( target && ( navMenu . contains ( target ) || navToggle . contains ( target ) ) ) return ;
10999 closeMenu ( ) ;
110100 } ;
111101
112- const handleDocumentKeydown = ( event : KeyboardEvent ) => {
102+ const handleDocumentKeydown = ( event ) => {
113103 if ( event . key === 'Escape' && navMenu . classList . contains ( 'is-open' ) ) {
114104 closeMenu ( ) ;
115105 }
@@ -128,11 +118,11 @@ const setupMobileNavigation = (): Cleanup => {
128118 } ;
129119} ;
130120
131- let teardownNav : Cleanup | undefined ;
132- let teardownTheme : Cleanup | undefined ;
121+ let teardownNav ;
122+ let teardownTheme ;
133123
134124const enhance = ( ) => {
135- const currentAttr = document . documentElement . getAttribute ( 'data-theme' ) as Theme | null ;
125+ const currentAttr = document . documentElement . getAttribute ( 'data-theme' ) ;
136126 syncThemeIcons ( currentAttr === 'light' ? 'light' : 'dark' ) ;
137127 if ( teardownTheme ) {
138128 teardownTheme ( ) ;
0 commit comments