@@ -73,14 +73,15 @@ export default class Darkmode {
7373 const span = document . createElement ( 'span' )
7474 span . innerHTML = this . options . label
7575 div . className = 'drkmd-toggle-button'
76+
7677 div . setAttribute ( 'title' , 'Toggle dark mode' )
7778 div . setAttribute ( 'aria-label' , 'Toggle dark mode' )
7879 div . setAttribute ( 'aria-checked' , 'false' )
7980 div . setAttribute ( 'role' , 'checkbox' )
8081 div . appendChild ( span )
8182
8283 div . addEventListener ( 'click' , ( ) => {
83- this . dark === true ? this . toLight ( ) : this . toDark ( )
84+ this . toggle ( )
8485 } )
8586
8687 document . body . insertBefore ( div , document . body . firstChild )
@@ -89,19 +90,23 @@ export default class Darkmode {
8990
9091 toLight ( ) {
9192 if ( this . options . events ) window . dispatchEvent ( new CustomEvent ( 'theme-change' , { detail : { to : 'light' } } ) )
93+
9294 document . documentElement . setAttribute ( 'data-theme' , 'light' )
93- this . _setStorageValue ( false )
9495 document . body . classList . remove ( 'theme-dark' )
9596 document . body . classList . add ( 'theme-light' )
97+
98+ this . _setStorageValue ( false )
9699 this . dark = false
97100 }
98101
99102 toDark ( ) {
100103 if ( this . options . events ) window . dispatchEvent ( new CustomEvent ( 'theme-change' , { detail : { to : 'dark' } } ) )
104+
101105 document . documentElement . setAttribute ( 'data-theme' , 'dark' )
102- this . _setStorageValue ( true )
103106 document . body . classList . add ( 'theme-dark' )
104107 document . body . classList . remove ( 'theme-light' )
108+
109+ this . _setStorageValue ( true )
105110 this . dark = true
106111 }
107112
@@ -119,7 +124,7 @@ export default class Darkmode {
119124 }
120125
121126 _switchThemePrefers ( ) {
122- this . _preferedTheme ( ) === true ? this . swichToDark ( ) : this . swichToLight ( )
127+ this . _preferedTheme ( ) === true ? this . toDark ( ) : this . toLight ( )
123128 }
124129
125130 _getStorageValue ( ) {
@@ -150,6 +155,7 @@ export default class Darkmode {
150155 'href' ,
151156 'data:text/css;charset=UTF-8,' + encodeURIComponent ( css )
152157 )
158+
153159 document . head . appendChild ( linkElement )
154160 }
155161}
0 commit comments