@@ -7,7 +7,21 @@ const HSThemeAppearance = {
77 const defaultTheme = 'default'
88 let theme = localStorage . getItem ( 'hs_theme' ) || defaultTheme
99
10- if ( document . querySelector ( 'html' ) . classList . contains ( 'dark' ) ) return
10+ if ( document . querySelector ( 'html' ) . classList . contains ( 'dark' ) ) {
11+ // Still need to set hs-dark-mode-active and toggle icons
12+ document . querySelector ( 'html' ) . classList . add ( 'hs-dark-mode-active' )
13+ const moonIcon = document . querySelector ( '[data-hs-theme-click-value="dark"]' )
14+ const sunIcon = document . querySelector ( '[data-hs-theme-click-value="light"]' )
15+ if ( moonIcon ) {
16+ moonIcon . classList . add ( 'hidden' )
17+ moonIcon . classList . remove ( 'block' )
18+ }
19+ if ( sunIcon ) {
20+ sunIcon . classList . remove ( 'hidden' )
21+ sunIcon . classList . add ( 'block' )
22+ }
23+ return
24+ }
1125 this . setAppearance ( theme )
1226 } ,
1327 _resetStylesOnLoad ( ) {
@@ -29,10 +43,45 @@ const HSThemeAppearance = {
2943 }
3044
3145 document . querySelector ( 'html' ) . classList . remove ( 'dark' )
46+ document . querySelector ( 'html' ) . classList . remove ( 'light' )
3247 document . querySelector ( 'html' ) . classList . remove ( 'default' )
3348 document . querySelector ( 'html' ) . classList . remove ( 'auto' )
3449
35- document . querySelector ( 'html' ) . classList . add ( this . getOriginalAppearance ( ) )
50+ const themeToApply = this . getOriginalAppearance ( )
51+
52+ // Only add 'dark' class for dark mode. Light mode = no class
53+ if ( themeToApply === 'dark' ) {
54+ document . querySelector ( 'html' ) . classList . add ( 'dark' )
55+ }
56+
57+ // Manually toggle the hs-dark-mode-active class for icon visibility
58+ const htmlEl = document . querySelector ( 'html' )
59+ const moonIcon = document . querySelector ( '[data-hs-theme-click-value="dark"]' )
60+ const sunIcon = document . querySelector ( '[data-hs-theme-click-value="light"]' )
61+
62+ if ( themeToApply === 'dark' ) {
63+ htmlEl . classList . add ( 'hs-dark-mode-active' )
64+ // In dark mode: hide moon, show sun
65+ if ( moonIcon ) {
66+ moonIcon . classList . add ( 'hidden' )
67+ moonIcon . classList . remove ( 'block' )
68+ }
69+ if ( sunIcon ) {
70+ sunIcon . classList . remove ( 'hidden' )
71+ sunIcon . classList . add ( 'block' )
72+ }
73+ } else {
74+ htmlEl . classList . remove ( 'hs-dark-mode-active' )
75+ // In light mode: show moon, hide sun
76+ if ( moonIcon ) {
77+ moonIcon . classList . remove ( 'hidden' )
78+ moonIcon . classList . add ( 'block' )
79+ }
80+ if ( sunIcon ) {
81+ sunIcon . classList . add ( 'hidden' )
82+ sunIcon . classList . remove ( 'block' )
83+ }
84+ }
3685
3786 setTimeout ( ( ) => {
3887 $resetStylesEl . remove ( )
@@ -66,10 +115,6 @@ window.addEventListener('load', () => {
66115 const $clickableThemes = document . querySelectorAll ( '[data-hs-theme-click-value]' )
67116 const $switchableThemes = document . querySelectorAll ( '[data-hs-theme-switch]' )
68117
69- $clickableThemes . forEach ( $item => {
70- $item . addEventListener ( 'click' , ( ) => HSThemeAppearance . setAppearance ( $item . getAttribute ( 'data-hs-theme-click-value' ) , true , $item ) )
71- } )
72-
73118 $switchableThemes . forEach ( $item => {
74119 $item . addEventListener ( 'change' , ( e ) => {
75120 HSThemeAppearance . setAppearance ( e . target . checked ? 'dark' : 'default' )
@@ -110,12 +155,27 @@ window.addEventListener('load', () => {
110155 return
111156 }
112157
158+ // Don't interfere with theme switcher clicks
159+ if ( e . target . closest ( '[data-hs-theme-click-value]' ) ) {
160+ return
161+ }
162+
113163 // Close if clicking outside the dropdown
114164 if ( ! dropdown . contains ( e . target ) ) {
115165 closeDropdown ( )
116166 }
117167 } )
118168
169+ // Use event delegation for theme switchers - MUST be after dropdown handler
170+ document . addEventListener ( 'click' , ( e ) => {
171+ const themeButton = e . target . closest ( '[data-hs-theme-click-value]' )
172+ if ( themeButton ) {
173+ e . preventDefault ( )
174+ const themeValue = themeButton . getAttribute ( 'data-hs-theme-click-value' )
175+ HSThemeAppearance . setAppearance ( themeValue , true , themeButton )
176+ }
177+ } )
178+
119179 // Close dropdown when clicking a menu item
120180 dropdownMenu . querySelectorAll ( 'a' ) . forEach ( function ( link ) {
121181 link . addEventListener ( 'click' , function ( ) {
@@ -142,4 +202,3 @@ window.addEventListener('load', () => {
142202 }
143203 }
144204} )
145-
0 commit comments