leaflet-theme-control 0.0.1

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2025 Kristjan ESPERANTO
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,277 @@
1
+ # Leaflet Theme Control
2
+
3
+ A Leaflet control for switching between visual themes using CSS filters. Perfect for adding dark mode, grayscale, and custom visual modes to your maps without requiring multiple tile layers.
4
+
5
+ ![Leaflet Theme Control Screenshot](screenshot.png)
6
+
7
+ ## Features
8
+
9
+ - 🌓 **Multiple themes**: Light, Dark, Grayscale, Custom
10
+ - â™ŋ **Accessibility**: Adaptable themes for better visibility
11
+ - 🎨 **CSS Filters**: No need for multiple tile sources
12
+ - 💾 **Persistent**: Saves user preference in localStorage
13
+ - 🔍 **System Detection**: Automatically detects OS dark mode preference
14
+ - 🌍 **i18n Ready**: Customizable labels via callback function
15
+ - đŸĒļ **Lightweight**: Zero dependencies (except Leaflet)
16
+ - ⚡ **Performance**: Instant theme switching without reloading tiles
17
+
18
+ ## Installation
19
+
20
+ ### As npm package (coming soon)
21
+
22
+ ```bash
23
+ npm install leaflet-theme-control
24
+ ```
25
+
26
+ **With bundler (Webpack, Vite, Rollup):**
27
+
28
+ ```javascript
29
+ import { ThemeControl } from "leaflet-theme-control";
30
+ import "leaflet-theme-control/src/leaflet-theme-control.css";
31
+ ```
32
+
33
+ **Without bundler (plain HTML):**
34
+
35
+ ```html
36
+ <link rel="stylesheet" href="node_modules/leaflet-theme-control/src/leaflet-theme-control.css" />
37
+
38
+ <script type="importmap">
39
+ {
40
+ "imports": {
41
+ "leaflet-theme-control": "./node_modules/leaflet-theme-control/src/leaflet-theme-control.js"
42
+ }
43
+ }
44
+ </script>
45
+
46
+ <script type="module">
47
+ import { ThemeControl } from "leaflet-theme-control";
48
+ // Your code here
49
+ </script>
50
+ ```
51
+
52
+ **Or via CDN (coming soon):**
53
+
54
+ ```html
55
+ <link rel="stylesheet" href="https://unpkg.com/leaflet-theme-control/src/leaflet-theme-control.css" />
56
+
57
+ <script type="importmap">
58
+ {
59
+ "imports": {
60
+ "leaflet-theme-control": "https://unpkg.com/leaflet-theme-control/src/leaflet-theme-control.js"
61
+ }
62
+ }
63
+ </script>
64
+
65
+ <script type="module">
66
+ import { ThemeControl } from "leaflet-theme-control";
67
+ </script>
68
+ ```
69
+
70
+ ### As local module (development)
71
+
72
+ **HTML:**
73
+
74
+ ```html
75
+ <link rel="stylesheet" href="./third-party/leaflet-theme-control/src/leaflet-theme-control.css" />
76
+ ```
77
+
78
+ **JavaScript:**
79
+
80
+ ```javascript
81
+ import { ThemeControl } from "./third-party/leaflet-theme-control/src/leaflet-theme-control.js";
82
+ ```
83
+
84
+ ## Usage
85
+
86
+ ### Basic Example
87
+
88
+ ```javascript
89
+ import L from "leaflet";
90
+ import { ThemeControl } from "leaflet-theme-control";
91
+
92
+ const map = L.map("map").setView([51.505, -0.09], 13);
93
+
94
+ L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
95
+ attribution: "Š OpenStreetMap contributors"
96
+ }).addTo(map);
97
+
98
+ // Add theme control
99
+ new ThemeControl().addTo(map);
100
+ ```
101
+
102
+ ### With Custom Options
103
+
104
+ ```javascript
105
+ new ThemeControl({
106
+ position: "topright",
107
+ defaultTheme: "light",
108
+ detectSystemTheme: true,
109
+ storageKey: "my-map-theme",
110
+
111
+ // Custom label function for i18n
112
+ getLabel: (themeKey) => {
113
+ return i18n.t(`themes.${themeKey}`);
114
+ },
115
+
116
+ // Callback when theme changes
117
+ onChange: (themeKey, theme) => {
118
+ console.log(`Theme changed to: ${themeKey}`);
119
+ }
120
+ }).addTo(map);
121
+ ```
122
+
123
+ ### Custom Themes
124
+
125
+ ```javascript
126
+ import { ThemeControl } from "leaflet-theme-control";
127
+
128
+ new ThemeControl({
129
+ themes: {
130
+ light: {
131
+ label: "Light Mode",
132
+ filter: "",
133
+ icon: "â˜€ī¸",
134
+ controlStyle: "light",
135
+ className: "theme-light"
136
+ },
137
+ dark: {
138
+ label: "Dark Mode",
139
+ filter: "invert(1) hue-rotate(180deg) saturate(0.6) brightness(0.5)",
140
+ icon: "🌙",
141
+ controlStyle: "dark",
142
+ className: "theme-dark",
143
+ applyToSelectors: [".my-sidebar", ".my-header"] // Apply filter to these elements too
144
+ },
145
+ monochrome: {
146
+ label: "Black & White",
147
+ filter: "grayscale(1) contrast(1.2)",
148
+ icon: "âšĢ",
149
+ controlStyle: "light",
150
+ className: "theme-mono",
151
+ applyToSelectors: ".my-sidebar" // Single selector also works
152
+ },
153
+ custom: {
154
+ label: "My Theme",
155
+ filter: "invert(1) hue-rotate(180deg) saturate(1) brightness(1) contrast(1) sepia(0.5) grayscale(0.5)",
156
+ icon: "🎨",
157
+ controlStyle: "dark",
158
+ className: "theme-custom",
159
+ applyToSelectors: [".my-sidebar", ".my-footer"]
160
+ }
161
+ }
162
+ }).addTo(map);
163
+ ```
164
+
165
+ **Theme Properties:**
166
+
167
+ - `filter`: CSS filter string (applied to map and `applyToSelectors`)
168
+ - `controlStyle`: `"light"` or `"dark"` for Leaflet controls styling
169
+ - `className`: CSS class added to `<html>` element (for custom styling)
170
+ - `applyToSelectors`: String or Array of CSS selectors to apply the same filter to
171
+
172
+ **Use Cases:**
173
+
174
+ - `applyToSelectors`: Apply the same dark mode filter to sidebar, header, footer etc.
175
+ - `className`: Style elements differently per theme with CSS
176
+
177
+ ```css
178
+ /* Using className for custom styling */
179
+ .theme-dark .my-button {
180
+ background: #2d2d2d;
181
+ color: #e0e0e0;
182
+ }
183
+
184
+ /* Elements in applyToSelectors get the filter automatically */
185
+ .my-sidebar {
186
+ background: white; /* Will be inverted in dark mode */
187
+ }
188
+ ```
189
+
190
+ ### Programmatic Control (No UI Button)
191
+
192
+ For advanced use cases where you want to control themes from your own UI:
193
+
194
+ ```javascript
195
+ // Create control without visible button
196
+ const themeControl = new ThemeControl({
197
+ addButton: false, // No UI button
198
+ enableEditor: true, // Editor still available programmatically
199
+ onChange: (theme) => {
200
+ console.log("Theme changed:", theme);
201
+ }
202
+ });
203
+
204
+ map.addControl(themeControl);
205
+
206
+ // Control themes programmatically
207
+ themeControl.setTheme("dark");
208
+ console.log(themeControl.getCurrentTheme()); // "dark"
209
+
210
+ // Open editor from custom button
211
+ myCustomButton.onclick = () => {
212
+ themeControl.editor.open();
213
+ };
214
+ ```
215
+
216
+ See [examples/api.html](examples/api.html) for a complete example.
217
+
218
+ ## API
219
+
220
+ ### Options
221
+
222
+ | Option | Type | Default | Description |
223
+ | ------------------- | -------- | ---------------------- | ------------------------------------------------------------------- |
224
+ | `position` | String | `"topright"` | Position of the control |
225
+ | `themes` | Object | `DEFAULT_THEMES` | Theme definitions |
226
+ | `defaultTheme` | String | `"light"` | Initial theme |
227
+ | `storageKey` | String | `"leaflet-theme"` | localStorage key |
228
+ | `detectSystemTheme` | Boolean | `true` | Detect OS dark mode |
229
+ | `cssSelector` | String | `".leaflet-tile-pane"` | Elements to apply filter to |
230
+ | `addButton` | Boolean | `true` | Add UI button to map (set to `false` for programmatic control only) |
231
+ | `enableEditor` | Boolean | `false` | Enable theme editor UI with customization sliders |
232
+ | `onChange` | Function | `null` | Callback on theme change: `(themeKey, theme) => {}` |
233
+ | `getLabel` | Function | `null` | Function to get translated theme labels: `(themeKey) => string` |
234
+ | `getEditorLabels` | Function | `null` | Function to get translated editor UI labels: `(key) => string` |
235
+
236
+ ### Methods
237
+
238
+ | Method | Returns | Description |
239
+ | -------------------- | -------- | ------------------------ |
240
+ | `setTheme(themeKey)` | `void` | Switch to specific theme |
241
+ | `getCurrentTheme()` | `String` | Get current theme key |
242
+ | `getThemes()` | `Object` | Get all available themes |
243
+
244
+ ### Editor API (when `enableEditor: true`)
245
+
246
+ | Method | Returns | Description |
247
+ | ---------------------------------- | ------- | ------------------------------ |
248
+ | `editor.open()` | `void` | Open theme selector panel |
249
+ | `editor.openThemeEditor(themeKey)` | `void` | Open editor for specific theme |
250
+ | `editor.close()` | `void` | Close editor panel |
251
+
252
+ ## Built-in Themes
253
+
254
+ - **Light**: Default, no filter
255
+ - **Dark**: Inverted colors with adjusted hue, saturation, and brightness
256
+ - **Grayscale**: Black and white for printing or reduced distraction
257
+ - **Custom**: Fully customizable theme with combined filters (editable via theme editor)
258
+
259
+ ## Browser Support
260
+
261
+ Works in all modern browsers that support CSS filters:
262
+
263
+ - Chrome/Edge 18+
264
+ - Firefox 35+
265
+ - Safari 9.1+
266
+
267
+ ## License
268
+
269
+ MIT License. See [LICENSE](LICENSE.md) for details.
270
+
271
+ ## Contributing
272
+
273
+ Contributions are welcome! Please feel free to submit a Pull Request.
274
+
275
+ ## Credits
276
+
277
+ Originally developed for the [Veggiekarte](https://veggiekarte.de) project. But hopefully useful for others too!
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "leaflet-theme-control",
3
+ "version": "0.0.1",
4
+ "description": "A Leaflet control for switching between visual themes (light, dark, grayscale, custom, etc.) using CSS filters",
5
+ "main": "src/leaflet-theme-control.js",
6
+ "module": "src/leaflet-theme-control.js",
7
+ "types": "types/index.d.ts",
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "exports": {
11
+ ".": {
12
+ "types": "./types/index.d.ts",
13
+ "import": "./src/leaflet-theme-control.js",
14
+ "default": "./src/leaflet-theme-control.js"
15
+ },
16
+ "./src/leaflet-theme-control.css": "./src/leaflet-theme-control.css"
17
+ },
18
+ "files": [
19
+ "src/**/*",
20
+ "types/**/*",
21
+ "README.md",
22
+ "LICENSE.md"
23
+ ],
24
+ "keywords": [
25
+ "leaflet",
26
+ "dark-mode",
27
+ "theme",
28
+ "accessibility",
29
+ "css-filter",
30
+ "map",
31
+ "grayscale",
32
+ "high-contrast"
33
+ ],
34
+ "author": "Kristjan ESPERANTO",
35
+ "license": "MIT",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/kristjanesperanto/leaflet-theme-control.git"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/kristjanesperanto/leaflet-theme-control/issues"
42
+ },
43
+ "homepage": "https://github.com/kristjanesperanto/leaflet-theme-control#readme",
44
+ "peerDependencies": {
45
+ "leaflet": ">=2.0.0-alpha.1"
46
+ },
47
+ "devDependencies": {
48
+ "@eslint/css": "^0.14.1",
49
+ "@eslint/js": "^9.39.2",
50
+ "@eslint/markdown": "^7.5.1",
51
+ "@stylistic/eslint-plugin": "^5.6.1",
52
+ "commit-and-tag-version": "^12.6.1",
53
+ "eslint": "^9.39.2",
54
+ "eslint-plugin-import-x": "^4.16.1",
55
+ "eslint-plugin-jsdoc": "^61.5.0",
56
+ "globals": "^16.5.0",
57
+ "happy-dom": "^20.0.11",
58
+ "leaflet": "^2.0.0-alpha.1",
59
+ "lint-staged": "^16.2.7",
60
+ "prettier": "^3.7.4",
61
+ "simple-git-hooks": "^2.13.1",
62
+ "vitest": "^4.0.16"
63
+ },
64
+ "scripts": {
65
+ "test": "npm run lint && vitest run",
66
+ "test:watch": "vitest",
67
+ "test:coverage": "vitest run --coverage",
68
+ "lint": "eslint && prettier --check .",
69
+ "lint:fix": "eslint --fix && prettier --write .",
70
+ "prepare": "simple-git-hooks",
71
+ "release": "commit-and-tag-version"
72
+ },
73
+ "simple-git-hooks": {
74
+ "pre-commit": "npx lint-staged"
75
+ },
76
+ "lint-staged": {
77
+ "*": [
78
+ "eslint --fix",
79
+ "prettier --write --ignore-unknown"
80
+ ]
81
+ }
82
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Built-in theme presets with CSS filters
3
+ */
4
+ export const DEFAULT_THEMES = {
5
+ light: {
6
+ label: 'Light',
7
+ filter: '',
8
+ icon: 'â˜€ī¸',
9
+ controlStyle: 'light',
10
+ className: 'theme-light',
11
+ },
12
+ dark: {
13
+ label: 'Dark',
14
+ filter: 'invert(1) hue-rotate(180deg) saturate(0.6) brightness(0.5)',
15
+ icon: '🌙',
16
+ controlStyle: 'dark',
17
+ className: 'theme-dark',
18
+ },
19
+ grayscale: {
20
+ label: 'Grayscale',
21
+ filter: 'grayscale(1)',
22
+ icon: 'âšĢ',
23
+ controlStyle: 'light',
24
+ className: 'theme-grayscale',
25
+ },
26
+ custom: {
27
+ label: 'Custom',
28
+ filter: 'invert(1) hue-rotate(180deg) saturate(1) brightness(1) contrast(1) sepia(0.5) grayscale(0.5)',
29
+ icon: '🎨',
30
+ controlStyle: 'dark',
31
+ className: 'theme-custom',
32
+ },
33
+ }