leaflet-theme-control 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leaflet-theme-control",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "description": "A Leaflet control for switching between visual themes (light, dark, grayscale, custom, etc.) using CSS filters",
5
5
  "main": "src/leaflet-theme-control.js",
6
6
  "module": "src/leaflet-theme-control.js",
@@ -57,6 +57,9 @@ export class ThemeControl extends Control {
57
57
  // AbortController for automatic event cleanup
58
58
  this._abortController = new AbortController()
59
59
 
60
+ // Setup MutationObserver to watch for language changes on html[lang]
61
+ this._setupLanguageObserver()
62
+
60
63
  // Setup media query listener for system theme changes
61
64
  if (this.options.detectSystemTheme) {
62
65
  const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
@@ -143,6 +146,12 @@ export class ThemeControl extends Control {
143
146
  // Abort all event listeners
144
147
  this._abortController.abort()
145
148
 
149
+ // Disconnect language observer
150
+ if (this._langObserver) {
151
+ this._langObserver.disconnect()
152
+ this._langObserver = null
153
+ }
154
+
146
155
  // Cleanup editor
147
156
  if (this.editor) {
148
157
  this.editor.cleanup()
@@ -152,17 +161,54 @@ export class ThemeControl extends Control {
152
161
  this.map = null
153
162
  }
154
163
 
164
+ _setupLanguageObserver() {
165
+ // Watch for changes to html[lang] attribute
166
+ this._langObserver = new MutationObserver((mutations) => {
167
+ for (const mutation of mutations) {
168
+ if (mutation.type === 'attributes' && mutation.attributeName === 'lang') {
169
+ // Language changed, update button label
170
+ this.updateButtonLabel()
171
+ break
172
+ }
173
+ }
174
+ })
175
+
176
+ // Observe the html element for lang attribute changes
177
+ this._langObserver.observe(document.documentElement, {
178
+ attributes: true,
179
+ attributeFilter: ['lang'],
180
+ })
181
+ }
182
+
155
183
  _updateButton(button, themeKey) {
156
184
  if (!button) return // No button if addButton: false
157
185
 
158
186
  const theme = this.options.themes[themeKey]
159
187
  const label = this._getThemeLabel(themeKey)
160
188
 
161
- button.setAttribute('aria-label', 'Theme: ' + label)
162
- button.title = 'Theme: ' + label
189
+ // Get translated button prefix or use default
190
+ let buttonPrefix = 'Theme'
191
+ if (this.options.getEditorLabels) {
192
+ const translated = this.options.getEditorLabels('themeButton')
193
+ if (translated && translated !== 'themeButton') {
194
+ buttonPrefix = translated
195
+ }
196
+ }
197
+
198
+ const fullLabel = `${buttonPrefix}: ${label}`
199
+ button.setAttribute('aria-label', fullLabel)
200
+ button.title = fullLabel
163
201
  button.textContent = theme.icon || '🎨'
164
202
  }
165
203
 
204
+ /**
205
+ * Update button label (useful for language changes)
206
+ * Call this method after changing language to update the button's aria-label and title
207
+ */
208
+ updateButtonLabel() {
209
+ this._updateButton(this.button, this.currentTheme)
210
+ }
211
+
166
212
  _getThemeLabel(themeKey) {
167
213
  // Use custom label function if provided
168
214
  if (this.options.getLabel) {
@@ -152,6 +152,7 @@ export class ThemeEditor {
152
152
  contrast: 'Contrast',
153
153
  sepia: 'Sepia',
154
154
  grayscaleFilter: 'Grayscale',
155
+ themeButton: 'Theme',
155
156
  }
156
157
 
157
158
  return labels[key] || key