maplibre-gl-layer-control 0.2.0 → 0.4.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.
@@ -1 +1,395 @@
1
+ import { IControl } from 'maplibre-gl';
2
+ import { Map as Map_2 } from 'maplibre-gl';
3
+
4
+ /**
5
+ * Clamp a numeric value between min and max
6
+ * @param value Value to clamp
7
+ * @param min Minimum value
8
+ * @param max Maximum value
9
+ * @returns Clamped value
10
+ */
11
+ export declare function clamp(value: number, min: number, max: number): number;
12
+
13
+ /**
14
+ * Format a numeric value based on the step size
15
+ * @param value Numeric value to format
16
+ * @param step Step size (determines decimal places)
17
+ * @returns Formatted string
18
+ */
19
+ export declare function formatNumericValue(value: number, step: number): string;
20
+
21
+ /**
22
+ * Get the current opacity value for a layer
23
+ * @param map MapLibre map instance
24
+ * @param layerId Layer ID
25
+ * @param layerType Layer type
26
+ * @returns Current opacity value (0-1)
27
+ */
28
+ export declare function getLayerOpacity(map: Map_2, layerId: string, layerType: string): number;
29
+
30
+ /**
31
+ * Get layer type from map
32
+ * @param map MapLibre map instance
33
+ * @param layerId Layer ID
34
+ * @returns Layer type or null if layer not found
35
+ */
36
+ export declare function getLayerType(map: Map_2, layerId: string): string | null;
37
+
38
+ /**
39
+ * Check if a layer type supports style editing
40
+ * @param layerType Layer type
41
+ * @returns True if the layer type supports style editing
42
+ */
43
+ export declare function isStyleableLayerType(layerType: string): layerType is StyleableLayerType;
44
+
45
+ /**
46
+ * LayerControl - A comprehensive layer control for MapLibre GL
47
+ * Provides visibility toggle, opacity control, and advanced style editing
48
+ */
49
+ export declare class LayerControl implements IControl {
50
+ private map;
51
+ private container;
52
+ private button;
53
+ private panel;
54
+ private state;
55
+ private targetLayers;
56
+ private styleEditors;
57
+ private minPanelWidth;
58
+ private maxPanelWidth;
59
+ private showStyleEditor;
60
+ private showOpacitySlider;
61
+ private widthSliderEl;
62
+ private widthThumbEl;
63
+ private widthValueEl;
64
+ private isWidthSliderActive;
65
+ private widthDragRectWidth;
66
+ private widthDragStartX;
67
+ private widthDragStartWidth;
68
+ private widthFrame;
69
+ constructor(options?: LayerControlOptions);
70
+ /**
71
+ * Called when the control is added to the map
72
+ */
73
+ onAdd(map: Map_2): HTMLElement;
74
+ /**
75
+ * Called when the control is removed from the map
76
+ */
77
+ onRemove(): void;
78
+ /**
79
+ * Auto-detect layers from the map and populate layerStates
80
+ */
81
+ private autoDetectLayers;
82
+ /**
83
+ * Get the source IDs that were part of the original style (from the style URL)
84
+ * Sources added via map.addSource() are considered user-added
85
+ */
86
+ private getOriginalStyleSourceIds;
87
+ /**
88
+ * Generate a friendly display name from a layer ID
89
+ */
90
+ private generateFriendlyName;
91
+ /**
92
+ * Create the main container element
93
+ */
94
+ private createContainer;
95
+ /**
96
+ * Create the toggle button
97
+ */
98
+ private createToggleButton;
99
+ /**
100
+ * Create the panel element
101
+ */
102
+ private createPanel;
103
+ /**
104
+ * Create action buttons for Show All / Hide All
105
+ */
106
+ private createActionButtons;
107
+ /**
108
+ * Set visibility of all layers
109
+ */
110
+ private setAllLayersVisibility;
111
+ /**
112
+ * Create the panel header with title and width control
113
+ */
114
+ private createPanelHeader;
115
+ /**
116
+ * Create the width control slider
117
+ */
118
+ private createWidthControl;
119
+ /**
120
+ * Setup event listeners for width slider
121
+ */
122
+ private setupWidthSliderEvents;
123
+ /**
124
+ * Update panel width from pointer event
125
+ */
126
+ private updateWidthFromPointer;
127
+ /**
128
+ * Apply panel width (clamped to min/max)
129
+ */
130
+ private applyPanelWidth;
131
+ /**
132
+ * Update width display (value label and thumb position)
133
+ */
134
+ private updateWidthDisplay;
135
+ /**
136
+ * Setup main event listeners
137
+ */
138
+ private setupEventListeners;
139
+ /**
140
+ * Setup listeners for map layer changes
141
+ */
142
+ private setupLayerChangeListeners;
143
+ /**
144
+ * Toggle panel expanded/collapsed state
145
+ */
146
+ private toggle;
147
+ /**
148
+ * Expand the panel
149
+ */
150
+ private expand;
151
+ /**
152
+ * Collapse the panel
153
+ */
154
+ private collapse;
155
+ /**
156
+ * Build layer items (called initially and when layers change)
157
+ */
158
+ private buildLayerItems;
159
+ /**
160
+ * Add a single layer item to the panel
161
+ */
162
+ private addLayerItem;
163
+ /**
164
+ * Toggle layer visibility
165
+ */
166
+ private toggleLayerVisibility;
167
+ /**
168
+ * Change layer opacity
169
+ */
170
+ private changeLayerOpacity;
171
+ /**
172
+ * Check if a layer is a user-added layer (vs basemap layer)
173
+ */
174
+ private isUserAddedLayer;
175
+ /**
176
+ * Toggle visibility for all background layers (basemap layers)
177
+ */
178
+ private toggleBackgroundVisibility;
179
+ /**
180
+ * Change opacity for all background layers (basemap layers)
181
+ */
182
+ private changeBackgroundOpacity;
183
+ /**
184
+ * Create legend button for Background layer
185
+ */
186
+ private createBackgroundLegendButton;
187
+ /**
188
+ * Toggle background legend panel visibility
189
+ */
190
+ private toggleBackgroundLegend;
191
+ /**
192
+ * Open background legend panel
193
+ */
194
+ private openBackgroundLegend;
195
+ /**
196
+ * Close background legend panel
197
+ */
198
+ private closeBackgroundLegend;
199
+ /**
200
+ * Create the background legend panel with individual layer controls
201
+ */
202
+ private createBackgroundLegendPanel;
203
+ /**
204
+ * Check if a layer is currently rendered in the map viewport
205
+ */
206
+ private isLayerRendered;
207
+ /**
208
+ * Populate the background layer list with individual layers
209
+ */
210
+ private populateBackgroundLayerList;
211
+ /**
212
+ * Toggle visibility of an individual background layer
213
+ */
214
+ private toggleIndividualBackgroundLayer;
215
+ /**
216
+ * Set visibility for all background layers
217
+ */
218
+ private setAllBackgroundLayersVisibility;
219
+ /**
220
+ * Update the main Background checkbox based on individual layer states
221
+ */
222
+ private updateBackgroundCheckboxState;
223
+ /**
224
+ * Create style button for a layer
225
+ */
226
+ private createStyleButton;
227
+ /**
228
+ * Toggle style editor for a layer
229
+ */
230
+ private toggleStyleEditor;
231
+ /**
232
+ * Open style editor for a layer
233
+ */
234
+ private openStyleEditor;
235
+ /**
236
+ * Close style editor for a layer
237
+ */
238
+ private closeStyleEditor;
239
+ /**
240
+ * Create style editor UI
241
+ */
242
+ private createStyleEditor;
243
+ /**
244
+ * Add style controls based on layer type
245
+ */
246
+ private addStyleControlsForLayerType;
247
+ /**
248
+ * Add controls for fill layers
249
+ */
250
+ private addFillControls;
251
+ /**
252
+ * Add controls for line layers
253
+ */
254
+ private addLineControls;
255
+ /**
256
+ * Add controls for circle layers
257
+ */
258
+ private addCircleControls;
259
+ /**
260
+ * Add controls for raster layers
261
+ */
262
+ private addRasterControls;
263
+ /**
264
+ * Add controls for symbol layers
265
+ */
266
+ private addSymbolControls;
267
+ /**
268
+ * Create a color control
269
+ */
270
+ private createColorControl;
271
+ /**
272
+ * Create a slider control
273
+ */
274
+ private createSliderControl;
275
+ /**
276
+ * Reset layer style to original
277
+ */
278
+ private resetLayerStyle;
279
+ /**
280
+ * Update layer states from map (sync UI with map)
281
+ */
282
+ private updateLayerStatesFromMap;
283
+ /**
284
+ * Update UI elements for a specific layer
285
+ */
286
+ private updateUIForLayer;
287
+ /**
288
+ * Check for new layers and add them to the control, remove deleted layers
289
+ */
290
+ private checkForNewLayers;
291
+ }
292
+
293
+ /**
294
+ * Options for LayerControl constructor
295
+ */
296
+ export declare interface LayerControlOptions {
297
+ /** Whether the control starts collapsed (default: true) */
298
+ collapsed?: boolean;
299
+ /** Initial layer states (keyed by layer ID) */
300
+ layerStates?: LayerStates;
301
+ /** Array of layer IDs to control (if not specified, controls all layers) */
302
+ layers?: string[];
303
+ /** Initial panel width in pixels (default: 320) */
304
+ panelWidth?: number;
305
+ /** Minimum panel width in pixels (default: 240) */
306
+ panelMinWidth?: number;
307
+ /** Maximum panel width in pixels (default: 420) */
308
+ panelMaxWidth?: number;
309
+ /** Whether to show the style editor button (gear icon) for layers (default: true) */
310
+ showStyleEditor?: boolean;
311
+ /** Whether to show the opacity slider for layers (default: true) */
312
+ showOpacitySlider?: boolean;
313
+ }
314
+
315
+ /**
316
+ * State for a single layer
317
+ */
318
+ export declare interface LayerState {
319
+ /** Whether the layer is visible */
320
+ visible: boolean;
321
+ /** Layer opacity (0-1) */
322
+ opacity: number;
323
+ /** Display name for the layer */
324
+ name: string;
325
+ }
326
+
327
+ /**
328
+ * Collection of layer states keyed by layer ID
329
+ */
330
+ export declare interface LayerStates {
331
+ [layerId: string]: LayerState;
332
+ }
333
+
334
+ /**
335
+ * Normalize a color value to hex format
336
+ * Handles hex strings, RGB strings, and RGB arrays
337
+ * @param value Color value in various formats
338
+ * @returns Normalized hex color string (always 6 digits)
339
+ */
340
+ export declare function normalizeColor(value: any): string;
341
+
342
+ /**
343
+ * Paint properties for different layer types
344
+ */
345
+ export declare interface PaintProperty {
346
+ /** Property name (e.g., 'fill-color') */
347
+ name: string;
348
+ /** Current value */
349
+ value: any;
350
+ }
351
+
352
+ /**
353
+ * Convert RGB values to hex color string
354
+ * @param r Red component (0-255)
355
+ * @param g Green component (0-255)
356
+ * @param b Blue component (0-255)
357
+ * @returns Hex color string (e.g., '#ff0000')
358
+ */
359
+ export declare function rgbToHex(r: number, g: number, b: number): string;
360
+
361
+ /**
362
+ * Set the opacity for a layer
363
+ * @param map MapLibre map instance
364
+ * @param layerId Layer ID
365
+ * @param layerType Layer type
366
+ * @param opacity Opacity value (0-1)
367
+ */
368
+ export declare function setLayerOpacity(map: Map_2, layerId: string, layerType: string, opacity: number): void;
369
+
370
+ /**
371
+ * MapLibre layer types that support styling
372
+ */
373
+ export declare type StyleableLayerType = 'fill' | 'line' | 'circle' | 'symbol' | 'raster';
374
+
375
+ /**
376
+ * Control for a paint property (color picker or slider)
377
+ */
378
+ export declare interface StyleControlConfig {
379
+ /** Label to display */
380
+ label: string;
381
+ /** Paint property name */
382
+ property: string;
383
+ /** Control type */
384
+ type: 'color' | 'slider';
385
+ /** For sliders: minimum value */
386
+ min?: number;
387
+ /** For sliders: maximum value */
388
+ max?: number;
389
+ /** For sliders: step increment */
390
+ step?: number;
391
+ /** Default value if property is not set */
392
+ defaultValue?: any;
393
+ }
394
+
1
395
  export { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maplibre-gl-layer-control",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "A comprehensive layer control for MapLibre GL with advanced styling capabilities",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -89,5 +89,8 @@
89
89
  "vite": "^6.0.0",
90
90
  "vite-plugin-dts": "^4.3.0",
91
91
  "vitest": "^2.1.0"
92
+ },
93
+ "dependencies": {
94
+ "maplibre-gl-layer-control": "^0.2.0"
92
95
  }
93
96
  }