@@ -73,14 +73,15 @@ export default class Darkmode {
73
73
const span = document . createElement ( 'span' )
74
74
span . innerHTML = this . options . label
75
75
div . className = 'drkmd-toggle-button'
76
+
76
77
div . setAttribute ( 'title' , 'Toggle dark mode' )
77
78
div . setAttribute ( 'aria-label' , 'Toggle dark mode' )
78
79
div . setAttribute ( 'aria-checked' , 'false' )
79
80
div . setAttribute ( 'role' , 'checkbox' )
80
81
div . appendChild ( span )
81
82
82
83
div . addEventListener ( 'click' , ( ) => {
83
- this . dark === true ? this . toLight ( ) : this . toDark ( )
84
+ this . toggle ( )
84
85
} )
85
86
86
87
document . body . insertBefore ( div , document . body . firstChild )
@@ -89,19 +90,23 @@ export default class Darkmode {
89
90
90
91
toLight ( ) {
91
92
if ( this . options . events ) window . dispatchEvent ( new CustomEvent ( 'theme-change' , { detail : { to : 'light' } } ) )
93
+
92
94
document . documentElement . setAttribute ( 'data-theme' , 'light' )
93
- this . _setStorageValue ( false )
94
95
document . body . classList . remove ( 'theme-dark' )
95
96
document . body . classList . add ( 'theme-light' )
97
+
98
+ this . _setStorageValue ( false )
96
99
this . dark = false
97
100
}
98
101
99
102
toDark ( ) {
100
103
if ( this . options . events ) window . dispatchEvent ( new CustomEvent ( 'theme-change' , { detail : { to : 'dark' } } ) )
104
+
101
105
document . documentElement . setAttribute ( 'data-theme' , 'dark' )
102
- this . _setStorageValue ( true )
103
106
document . body . classList . add ( 'theme-dark' )
104
107
document . body . classList . remove ( 'theme-light' )
108
+
109
+ this . _setStorageValue ( true )
105
110
this . dark = true
106
111
}
107
112
@@ -119,7 +124,7 @@ export default class Darkmode {
119
124
}
120
125
121
126
_switchThemePrefers ( ) {
122
- this . _preferedTheme ( ) === true ? this . swichToDark ( ) : this . swichToLight ( )
127
+ this . _preferedTheme ( ) === true ? this . toDark ( ) : this . toLight ( )
123
128
}
124
129
125
130
_getStorageValue ( ) {
@@ -150,6 +155,7 @@ export default class Darkmode {
150
155
'href' ,
151
156
'data:text/css;charset=UTF-8,' + encodeURIComponent ( css )
152
157
)
158
+
153
159
document . head . appendChild ( linkElement )
154
160
}
155
161
}
0 commit comments