@@ -111,25 +111,59 @@ const ICON_INITIAL_STATE_FOR_AUTO = [10, 0, 33, 0];
111
111
const ICON_INITIAL_STATE_FOR_DARK = [ 10 , 0 , 20 , 1 ] ;
112
112
const ICON_INITIAL_STATE_FOR_LIGHT = [ 5 , 1 , 33 , 1 ] ;
113
113
114
- let shadowRoot ;
115
-
116
114
class ThemeSwitchElement extends HTMLElement {
115
+ shadowRoot ;
116
+
117
117
constructor ( ) {
118
118
super ( ) ;
119
119
120
120
// See https://stackoverflow.com/q/2305654/8583692
121
- shadowRoot = this . attachShadow ( { mode : "open" } ) ;
122
- shadowRoot . innerHTML = generateIcon ( ...getInitialStateForIcon ( ) ) ;
121
+ this . shadowRoot = this . attachShadow ( { mode : "open" } ) ;
122
+ this . shadowRoot . innerHTML = generateIcon ( ...getInitialStateForIcon ( ) ) ;
123
123
124
124
// Add the click listener to the top-most parent (the custom element itself)
125
125
// so the padding etc. on the element be also clickable
126
- shadowRoot . host . addEventListener ( "click" , toggleTheme ) ;
126
+ this . shadowRoot . host . addEventListener ( "click" , this . toggleTheme ) ;
127
127
128
128
// Create some CSS to apply to the shadow DOM
129
129
// See https://css-tricks.com/styling-a-web-component/
130
130
const style = document . createElement ( "style" ) ;
131
131
style . textContent = generateStyle ( ) ;
132
- shadowRoot . append ( style ) ;
132
+ this . shadowRoot . append ( style ) ;
133
+ }
134
+
135
+ // See https://stackoverflow.com/q/48316611
136
+ toggleTheme ( ) {
137
+ let theme = getUserThemeSelection ( ) ;
138
+ if ( theme === THEME_AUTO ) {
139
+ localStorage . setItem ( THEME_KEY , THEME_LIGHT ) ;
140
+ this . animateThemeButtonIconToLight ( ) ;
141
+ } else if ( theme === THEME_DARK ) {
142
+ localStorage . setItem ( THEME_KEY , THEME_AUTO ) ;
143
+ this . animateThemeButtonIconToAuto ( ) ;
144
+ } else /* if (theme === THEME_LIGHT) */ {
145
+ localStorage . setItem ( THEME_KEY , THEME_DARK ) ;
146
+ this . animateThemeButtonIconToDark ( ) ;
147
+ }
148
+ updateTheme ( ) ;
149
+ }
150
+
151
+ animateThemeButtonIconToLight ( ) {
152
+ this . shadowRoot . getElementById ( "letter-anim-hide" ) . beginElement ( ) ;
153
+ this . shadowRoot . getElementById ( "core-anim-shrink" ) . beginElement ( ) ;
154
+ this . shadowRoot . getElementById ( "rays-anim-rotate" ) . beginElement ( ) ;
155
+ this . shadowRoot . getElementById ( "rays-anim-show" ) . beginElement ( ) ;
156
+ }
157
+
158
+ animateThemeButtonIconToAuto ( ) {
159
+ this . shadowRoot . getElementById ( "eclipse-anim-go" ) . beginElement ( ) ;
160
+ this . shadowRoot . getElementById ( "letter-anim-show" ) . beginElement ( ) ;
161
+ }
162
+
163
+ animateThemeButtonIconToDark ( ) {
164
+ this . shadowRoot . getElementById ( "rays-anim-hide" ) . beginElement ( ) ;
165
+ this . shadowRoot . getElementById ( "core-anim-enlarge" ) . beginElement ( ) ;
166
+ this . shadowRoot . getElementById ( "eclipse-anim-come" ) . beginElement ( ) ;
133
167
}
134
168
}
135
169
@@ -242,40 +276,6 @@ function getInitialStateForIcon() {
242
276
}
243
277
}
244
278
245
- // See https://stackoverflow.com/q/48316611
246
- function toggleTheme ( ) {
247
- let theme = getUserThemeSelection ( ) ;
248
- if ( theme === THEME_AUTO ) {
249
- localStorage . setItem ( THEME_KEY , THEME_LIGHT ) ;
250
- animateThemeButtonIconToLight ( ) ;
251
- } else if ( theme === THEME_DARK ) {
252
- localStorage . setItem ( THEME_KEY , THEME_AUTO ) ;
253
- animateThemeButtonIconToAuto ( ) ;
254
- } else /* if (theme === THEME_LIGHT) */ {
255
- localStorage . setItem ( THEME_KEY , THEME_DARK ) ;
256
- animateThemeButtonIconToDark ( ) ;
257
- }
258
- updateTheme ( ) ;
259
- }
260
-
261
- function animateThemeButtonIconToLight ( ) {
262
- shadowRoot . getElementById ( "letter-anim-hide" ) . beginElement ( ) ;
263
- shadowRoot . getElementById ( "core-anim-shrink" ) . beginElement ( ) ;
264
- shadowRoot . getElementById ( "rays-anim-rotate" ) . beginElement ( ) ;
265
- shadowRoot . getElementById ( "rays-anim-show" ) . beginElement ( ) ;
266
- }
267
-
268
- function animateThemeButtonIconToAuto ( ) {
269
- shadowRoot . getElementById ( "eclipse-anim-go" ) . beginElement ( ) ;
270
- shadowRoot . getElementById ( "letter-anim-show" ) . beginElement ( ) ;
271
- }
272
-
273
- function animateThemeButtonIconToDark ( ) {
274
- shadowRoot . getElementById ( "rays-anim-hide" ) . beginElement ( ) ;
275
- shadowRoot . getElementById ( "core-anim-enlarge" ) . beginElement ( ) ;
276
- shadowRoot . getElementById ( "eclipse-anim-come" ) . beginElement ( ) ;
277
- }
278
-
279
279
// Export for tests run by npm (no longer needed; kept for future reference)
280
280
// See https://stackoverflow.com/q/63752210/8583692
281
281
// and https://stackoverflow.com/a/54680602/8583692
0 commit comments