@@ -121,34 +121,33 @@ class ThemeSwitchElement extends HTMLElement {
121
121
122
122
constructor ( ) {
123
123
super ( ) ;
124
-
125
124
// See https://stackoverflow.com/q/2305654/8583692
126
125
this . shadowRoot = this . attachShadow ( { mode : "open" } ) ;
127
126
this . shadowRoot . innerHTML = generateIcon ( ...getInitialStateForIcon ( ) ) ;
128
-
129
127
// Add the click listener to the top-most parent (the custom element itself)
130
128
// so the padding etc. on the element be also clickable
131
- this . shadowRoot . host . addEventListener ( "click" , ( ) => {
132
- const oldTheme = getUserThemeSelection ( ) ;
133
- this . toggleTheme ( oldTheme ) ;
134
- const newTheme = getUserThemeSelection ( ) ;
135
- this . dispatchEvent ( this . createEvent ( oldTheme , newTheme ) ) ;
136
- } ) ;
137
-
129
+ this . shadowRoot . host . addEventListener ( "click" , this . onClick ) ;
138
130
// If another theme switch in page toggled, update my icon too
139
131
document . addEventListener ( CUSTOM_EVENT_NAME , event => {
140
132
if ( event . detail . originId !== this . identifier ) {
141
133
this . adaptToTheme ( ) ;
142
134
}
143
135
} ) ;
144
-
145
136
// Create some CSS to apply to the shadow DOM
146
137
// See https://css-tricks.com/styling-a-web-component/
147
138
const style = document . createElement ( "style" ) ;
148
139
style . textContent = generateStyle ( ) ;
149
140
this . shadowRoot . append ( style ) ;
150
141
}
151
142
143
+ onClick ( ) {
144
+ const oldTheme = getUserThemeSelection ( ) ;
145
+ this . toggleTheme ( oldTheme ) ;
146
+ const newTheme = getUserThemeSelection ( ) ;
147
+ const event = this . createEvent ( oldTheme , newTheme ) ;
148
+ this . dispatchEvent ( event ) ;
149
+ }
150
+
152
151
// See https://stackoverflow.com/a/53804106/8583692
153
152
createEvent ( oldTheme , newTheme ) {
154
153
return new CustomEvent ( CUSTOM_EVENT_NAME , {
0 commit comments