@@ -39,6 +39,42 @@ function intensityToColor(intensity) {
3939 return rootStyle . getPropertyValue ( `--heat-${ level } ` ) . trim ( ) ;
4040}
4141
42+ // ============================================================================
43+ // Theme Support
44+ // ============================================================================
45+
46+ // Get the preferred theme from localStorage or browser preference
47+ function getPreferredTheme ( ) {
48+ const saved = localStorage . getItem ( 'heatmap-theme' ) ;
49+ if ( saved ) return saved ;
50+ return window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ? 'dark' : 'light' ;
51+ }
52+
53+ // Apply theme and update UI. Returns the applied theme.
54+ function applyTheme ( theme ) {
55+ document . documentElement . setAttribute ( 'data-theme' , theme ) ;
56+ const btn = document . getElementById ( 'theme-btn' ) ;
57+ if ( btn ) {
58+ btn . querySelector ( '.icon-moon' ) . style . display = theme === 'dark' ? 'none' : '' ;
59+ btn . querySelector ( '.icon-sun' ) . style . display = theme === 'dark' ? '' : 'none' ;
60+ }
61+ return theme ;
62+ }
63+
64+ // Toggle theme and save preference. Returns the new theme.
65+ function toggleAndSaveTheme ( ) {
66+ const current = document . documentElement . getAttribute ( 'data-theme' ) || 'light' ;
67+ const next = current === 'light' ? 'dark' : 'light' ;
68+ applyTheme ( next ) ;
69+ localStorage . setItem ( 'heatmap-theme' , next ) ;
70+ return next ;
71+ }
72+
73+ // Restore theme from localStorage, or use browser preference
74+ function restoreUIState ( ) {
75+ applyTheme ( getPreferredTheme ( ) ) ;
76+ }
77+
4278// ============================================================================
4379// Favicon (Reuse logo image as favicon)
4480// ============================================================================
0 commit comments