maplibre-gl-layer-control 0.18.2 → 0.18.3

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.
@@ -0,0 +1,519 @@
1
+ import { Map as Map_2 } from 'maplibre-gl';
2
+
3
+ /**
4
+ * A single background-layer visibility preset: a map of style-layer ID to
5
+ * whether that layer should be visible.
6
+ */
7
+ declare type BackgroundLayerVisibility = Record<string, boolean>;
8
+
9
+ /**
10
+ * Collection of named background-layer visibility presets, keyed by preset name.
11
+ */
12
+ declare type BackgroundPresets = Record<string, BackgroundLayerVisibility>;
13
+
14
+ /**
15
+ * Context menu state
16
+ */
17
+ declare interface ContextMenuState {
18
+ /** Whether the context menu is visible */
19
+ visible: boolean;
20
+ /** The layer ID that the context menu is targeting */
21
+ targetLayerId: string | null;
22
+ /** X position of the context menu */
23
+ x: number;
24
+ /** Y position of the context menu */
25
+ y: number;
26
+ }
27
+
28
+ /**
29
+ * Adapter interface for custom (non-MapLibre) layers.
30
+ * Implement this interface to integrate custom layer types (e.g., deck.gl layers)
31
+ * with the layer control.
32
+ */
33
+ declare interface CustomLayerAdapter {
34
+ /** Unique type identifier for this adapter (e.g., 'cog', 'zarr', 'deck') */
35
+ type: string;
36
+ /** Get all layer IDs managed by this adapter */
37
+ getLayerIds(): string[];
38
+ /**
39
+ * Get the current state of a layer. For a native map layer the adapter lists
40
+ * in `getLayerIds`, report the layer's own visibility and opacity, without
41
+ * folding in any group's: the control shows these values in the layer's row
42
+ * and style editor and never reads them back from the map.
43
+ */
44
+ getLayerState(layerId: string): LayerState | null;
45
+ /** Set layer visibility */
46
+ setVisibility(layerId: string, visible: boolean): void;
47
+ /** Set layer opacity (0-1) */
48
+ setOpacity(layerId: string, opacity: number): void;
49
+ /** Get display name for a layer */
50
+ getName(layerId: string): string;
51
+ /** Get layer symbol type for UI display (optional) */
52
+ getSymbolType?(layerId: string): string;
53
+ /**
54
+ * Subscribe to layer changes (add/remove).
55
+ * Returns an unsubscribe function.
56
+ */
57
+ onLayerChange?(callback: (event: 'add' | 'remove', layerId: string) => void): () => void;
58
+ /**
59
+ * Get the bounds of a layer (optional).
60
+ * Returns [west, south, east, north] or null if not available.
61
+ */
62
+ getBounds?(layerId: string): [number, number, number, number] | null;
63
+ /**
64
+ * Remove a layer (optional).
65
+ * Called when user removes a layer via context menu.
66
+ */
67
+ removeLayer?(layerId: string): void;
68
+ /**
69
+ * Get native MapLibre layer IDs for a custom layer (optional).
70
+ * When provided, the style editor will show paint property controls
71
+ * for these native layers instead of the generic "custom layer" message.
72
+ */
73
+ getNativeLayerIds?(layerId: string): string[];
74
+ /**
75
+ * Get the layer groups this adapter defines (optional). When any adapter
76
+ * returns groups, the panel nests each layer under its group (see
77
+ * {@link getLayerGroupId}) as collapsible folders, and drag-and-drop and the
78
+ * context-menu moves keep a layer among the other layers of its own group.
79
+ * Groups that contain no layer, directly or through a nested group, are not
80
+ * shown.
81
+ */
82
+ getGroups?(): LayerGroupState[];
83
+ /**
84
+ * Get the ID of the group a layer belongs to, or undefined when the layer
85
+ * sits at the top level (optional; required for groups to have members).
86
+ */
87
+ getLayerGroupId?(layerId: string): string | undefined;
88
+ /** Set group visibility (optional; called from the group's checkbox). */
89
+ setGroupVisibility?(groupId: string, visible: boolean): void;
90
+ /** Set group opacity (0-1) (optional; called from the group's slider). */
91
+ setGroupOpacity?(groupId: string, opacity: number): void;
92
+ /**
93
+ * Record that a group was collapsed or expanded in the panel (optional).
94
+ * Without it the control still folds the group, but only until the panel is
95
+ * rebuilt from the adapter's `getGroups`.
96
+ */
97
+ setGroupCollapsed?(groupId: string, collapsed: boolean): void;
98
+ }
99
+
100
+ /**
101
+ * Drag state for layer reordering
102
+ */
103
+ declare interface DragState {
104
+ /** Whether drag is active */
105
+ active: boolean;
106
+ /** The layer ID being dragged */
107
+ layerId: string | null;
108
+ /** Starting Y position of the drag */
109
+ startY: number;
110
+ /** Current Y position of the drag */
111
+ currentY: number;
112
+ /** Placeholder element showing drop location */
113
+ placeholder: HTMLElement | null;
114
+ /** The element being dragged (clone) */
115
+ draggedElement: HTMLElement | null;
116
+ }
117
+
118
+ /**
119
+ * Internal control state (not exported)
120
+ */
121
+ export declare interface InternalControlState {
122
+ collapsed: boolean;
123
+ panelWidth: number;
124
+ activeStyleEditor: string | null;
125
+ layerStates: LayerStates;
126
+ originalStyles: Map<string, OriginalStyle>;
127
+ userInteractingWithSlider: boolean;
128
+ /** Whether the background legend panel is open */
129
+ backgroundLegendOpen: boolean;
130
+ /** Layer IDs whose category sub-legend is expanded */
131
+ expandedCategoryLayers: Set<string>;
132
+ /** Individual background layer visibility states */
133
+ backgroundLayerVisibility: Map<string, boolean>;
134
+ /** Whether to show only rendered layers in background legend */
135
+ onlyRenderedFilter: boolean;
136
+ /** Context menu state */
137
+ contextMenu: ContextMenuState;
138
+ /** Layer ID currently being renamed */
139
+ renamingLayerId: string | null;
140
+ /** Custom layer names set by user (layerId -> customName) */
141
+ customLayerNames: Map<string, string>;
142
+ /** Drag state for layer reordering */
143
+ drag: DragState;
144
+ /** Whether a style operation is in progress (prevents checkForNewLayers from running) */
145
+ isStyleOperationInProgress: boolean;
146
+ }
147
+
148
+ /**
149
+ * Options for LayerControl constructor
150
+ */
151
+ export declare interface LayerControlOptions {
152
+ /** Whether the control starts collapsed (default: true) */
153
+ collapsed?: boolean;
154
+ /** Initial layer states (keyed by layer ID). Partial values are accepted; unspecified fields will be auto-detected from the map. */
155
+ layerStates?: PartialLayerStates;
156
+ /** Array of layer IDs to control (if not specified, controls all layers) */
157
+ layers?: string[];
158
+ /** Initial panel width in pixels (default: 350) */
159
+ panelWidth?: number;
160
+ /** Minimum panel width in pixels (default: 240) */
161
+ panelMinWidth?: number;
162
+ /** Maximum panel width in pixels (default: 960) */
163
+ panelMaxWidth?: number;
164
+ /** Whether to show the style editor button (gear icon) for layers (default: true) */
165
+ showStyleEditor?: boolean;
166
+ /**
167
+ * Whether the style editor shows every property control it supports for a
168
+ * layer's type, even when the property is absent from the layer style
169
+ * (default: false). When false, controls for optional properties (e.g.
170
+ * `fill-opacity`, `fill-outline-color`) only appear once the layer sets them.
171
+ *
172
+ * Unset properties start at their effective value: the style-spec default,
173
+ * or the inherited value (`fill-outline-color` follows `fill-color`).
174
+ * Opening or closing the editor never writes to the map style; a property is
175
+ * set only when the user edits its control, and Reset Style unsets it again.
176
+ * A control that cannot take effect (e.g. outline color with
177
+ * `fill-antialias: false` or a fill pattern, text properties without a
178
+ * `text-field`, icon opacity without an `icon-image`) or whose value is a
179
+ * data-driven expression stays visible but disabled with an explanation;
180
+ * expressions are never replaced. Applies to ordinary layers and to native
181
+ * sublayers exposed by custom-layer adapters.
182
+ */
183
+ showAllStyleProperties?: boolean;
184
+ /** Whether to show the opacity slider for layers (default: true) */
185
+ showOpacitySlider?: boolean;
186
+ /** Whether to show layer type symbols/icons next to layer names (default: true) */
187
+ showLayerSymbol?: boolean;
188
+ /**
189
+ * Maximum panel height in pixels. When omitted, the panel grows to fill the
190
+ * available vertical space in the map container and only becomes scrollable
191
+ * once the layer list is taller than that space. Set an explicit value to
192
+ * cap the height instead.
193
+ */
194
+ panelMaxHeight?: number;
195
+ /** Whether to exclude drawn layers from drawing libraries like Geoman, Mapbox GL Draw, etc. (default: true) */
196
+ excludeDrawnLayers?: boolean;
197
+ /** Array of wildcard patterns to exclude layers by name (e.g., ['*-temp-*', 'debug-*']) */
198
+ excludeLayers?: string[];
199
+ /** Custom layer adapters for non-MapLibre layers (e.g., deck.gl COG layers, Zarr layers) */
200
+ customLayerAdapters?: CustomLayerAdapter[];
201
+ /**
202
+ * URL of the basemap style JSON. If provided, all layers defined in this style
203
+ * will be grouped under "Background", and all other layers will be shown individually
204
+ * in the layer control. This provides reliable distinction between basemap layers
205
+ * and user-added layers.
206
+ */
207
+ basemapStyleUrl?: string;
208
+ /** Whether to enable context menu (right-click) on layers (default: true) */
209
+ enableContextMenu?: boolean;
210
+ /** Whether to enable drag-and-drop reordering of layers (default: true) */
211
+ enableDragAndDrop?: boolean;
212
+ /** Callback when a layer is renamed via context menu */
213
+ onLayerRename?: (layerId: string, oldName: string, newName: string) => void;
214
+ /** Callback when layers are reordered via drag-and-drop */
215
+ onLayerReorder?: (layerOrder: string[]) => void;
216
+ /** Callback when a layer is removed via context menu */
217
+ onLayerRemove?: (layerId: string) => void;
218
+ /**
219
+ * Callback fired whenever a paint property changes through the per-layer
220
+ * style editor (a slider/color input/pattern picker, or Reset Style).
221
+ * Reports the layer the open style editor belongs to (`layerId`), the
222
+ * MapLibre paint property name (e.g. `"raster-brightness-max"`,
223
+ * `"fill-color"`, or `"fill-pattern"`), and the new value. Pattern selection
224
+ * reports its image ID; selecting None reports `undefined`. Reset reports
225
+ * the restored raw value, including a string, expression, or `undefined`
226
+ * for `fill-pattern`, and `undefined` for a property that
227
+ * `showAllStyleProperties` exposed and the user set on a layer that did not
228
+ * have it. Lets consumers mirror changes into their own store so
229
+ * external style UI stays in sync. The control still applies changes to the
230
+ * map itself; this callback is purely a notification.
231
+ */
232
+ onLayerStyleChange?: (layerId: string, property: string, value: unknown) => void;
233
+ /**
234
+ * Callback fired when the Background (basemap) group visibility is toggled
235
+ * via the control's checkbox. Lets consumers mirror the new state into their
236
+ * own store so external basemap UI (e.g. a separate layer panel) stays in sync.
237
+ */
238
+ onBackgroundVisibilityChange?: (visible: boolean) => void;
239
+ /**
240
+ * Callback fired when the Background (basemap) group opacity is changed via
241
+ * the control's slider. Lets consumers mirror the new opacity into their own
242
+ * store so external basemap UI stays in sync.
243
+ */
244
+ onBackgroundOpacityChange?: (opacity: number) => void;
245
+ /**
246
+ * Whether to show the "Saved configurations" controls in the Background Layers
247
+ * panel, letting users save the current basemap element visibility as a named
248
+ * preset and re-apply it later with one click (default: true).
249
+ */
250
+ enableBackgroundPresets?: boolean;
251
+ /**
252
+ * localStorage key under which background-layer visibility presets are stored.
253
+ * Presets persist across sessions and projects (default:
254
+ * 'maplibre-layer-control:background-presets').
255
+ */
256
+ backgroundPresetStorageKey?: string;
257
+ /**
258
+ * Callback fired whenever the set of saved background presets changes
259
+ * (created or deleted). Receives the full preset map. Applying a preset does
260
+ * not change the stored set, so it does not trigger this callback.
261
+ */
262
+ onBackgroundPresetsChange?: (presets: BackgroundPresets) => void;
263
+ }
264
+
265
+ /**
266
+ * React wrapper for LayerControl
267
+ *
268
+ * @example
269
+ * ```tsx
270
+ * import { LayerControlReact } from 'maplibre-gl-layer-control/react';
271
+ *
272
+ * function MapComponent() {
273
+ * const [map, setMap] = useState<MapLibreMap | null>(null);
274
+ *
275
+ * return (
276
+ * <>
277
+ * <div ref={(el) => {
278
+ * if (el && !map) {
279
+ * const newMap = new maplibregl.Map({ container: el, ... });
280
+ * setMap(newMap);
281
+ * }
282
+ * }} />
283
+ * {map && (
284
+ * <LayerControlReact
285
+ * map={map}
286
+ * position="top-right"
287
+ * layerStates={{
288
+ * 'my-layer': { visible: true, opacity: 1, name: 'My Layer' }
289
+ * }}
290
+ * />
291
+ * )}
292
+ * </>
293
+ * );
294
+ * }
295
+ * ```
296
+ */
297
+ export declare function LayerControlReact({ map, position, ...options }: LayerControlReactProps): null;
298
+
299
+ export declare interface LayerControlReactProps extends LayerControlOptions {
300
+ /** MapLibre map instance */
301
+ map: Map_2;
302
+ /** Position on the map */
303
+ position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
304
+ }
305
+
306
+ /**
307
+ * A collapsible folder of layers in the panel. Supplied by a
308
+ * {@link CustomLayerAdapter} through `getGroups`; layers join a group through
309
+ * the adapter's `getLayerGroupId`.
310
+ *
311
+ * A group's `visible` and `opacity` are its own settings, separate from those
312
+ * of its layers. The control shows and edits them but never folds them into
313
+ * the layer rows: applying a hidden or translucent group to its layers on the
314
+ * map is the adapter's job.
315
+ */
316
+ declare interface LayerGroupState {
317
+ /** Unique group ID */
318
+ id: string;
319
+ /** Display name for the group */
320
+ name: string;
321
+ /** ID of the enclosing group, or undefined for a top-level group */
322
+ parentId?: string;
323
+ /** Whether the group is visible */
324
+ visible: boolean;
325
+ /** Group opacity (0-1) */
326
+ opacity: number;
327
+ /** Whether the group's rows are folded away in the panel */
328
+ collapsed: boolean;
329
+ }
330
+
331
+ /**
332
+ * State for a single layer
333
+ */
334
+ export declare interface LayerState {
335
+ /** Whether the layer is visible */
336
+ visible: boolean;
337
+ /** Layer opacity (0-1) */
338
+ opacity: number;
339
+ /** Display name for the layer */
340
+ name: string;
341
+ /** Whether this is a custom (non-MapLibre) layer */
342
+ isCustomLayer?: boolean;
343
+ /** Custom layer type identifier (e.g., 'cog', 'zarr') */
344
+ customLayerType?: string;
345
+ }
346
+
347
+ /**
348
+ * Collection of layer states keyed by layer ID
349
+ */
350
+ declare interface LayerStates {
351
+ [layerId: string]: LayerState;
352
+ }
353
+
354
+ /**
355
+ * Original style cache for a layer (used for reset functionality)
356
+ */
357
+ export declare interface OriginalStyle {
358
+ /** Original paint properties */
359
+ paint: Record<string, any>;
360
+ }
361
+
362
+ /**
363
+ * Partial layer state for user-provided initial configuration.
364
+ * Users can specify only the properties they want to override (e.g., just `name`).
365
+ */
366
+ declare type PartialLayerState = Partial<LayerState>;
367
+
368
+ /**
369
+ * Collection of partial layer states keyed by layer ID (for options input)
370
+ */
371
+ declare interface PartialLayerStates {
372
+ [layerId: string]: PartialLayerState;
373
+ }
374
+
375
+ /**
376
+ * Hook for managing layer visibility and opacity state
377
+ *
378
+ * @example
379
+ * ```tsx
380
+ * const { layerStates, toggleVisibility, setOpacity } = useLayerState({
381
+ * map,
382
+ * initialStates: {
383
+ * 'my-layer': { visible: true, opacity: 1, name: 'My Layer' }
384
+ * }
385
+ * });
386
+ * ```
387
+ */
388
+ export declare function useLayerState({ map, initialStates, }: UseLayerStateOptions): UseLayerStateReturn;
389
+
390
+ export declare interface UseLayerStateOptions {
391
+ /** MapLibre map instance */
392
+ map: Map_2;
393
+ /** Initial layer states */
394
+ initialStates: Record<string, LayerState>;
395
+ }
396
+
397
+ export declare interface UseLayerStateReturn {
398
+ /** Current layer states */
399
+ layerStates: Record<string, LayerState>;
400
+ /** Toggle layer visibility */
401
+ toggleVisibility: (layerId: string) => void;
402
+ /** Set layer opacity */
403
+ setOpacity: (layerId: string, opacity: number) => void;
404
+ /** Update a layer state */
405
+ updateLayerState: (layerId: string, updates: Partial<LayerState>) => void;
406
+ }
407
+
408
+ /**
409
+ * Hook to sync React state with MapLibre map layer changes
410
+ *
411
+ * @example
412
+ * ```tsx
413
+ * useMapLayerSync({
414
+ * map,
415
+ * onLayerStatesChange: (states) => {
416
+ * console.log('Layer states updated:', states);
417
+ * },
418
+ * onNewLayers: (layerIds) => {
419
+ * console.log('New layers detected:', layerIds);
420
+ * }
421
+ * });
422
+ * ```
423
+ */
424
+ export declare function useMapLayerSync({ map, onLayerStatesChange, onNewLayers, trackedLayers, }: UseMapLayerSyncOptions): void;
425
+
426
+ export declare interface UseMapLayerSyncOptions {
427
+ /** MapLibre map instance */
428
+ map: Map_2;
429
+ /** Callback when layer states change on the map */
430
+ onLayerStatesChange?: (layerStates: Record<string, LayerState>) => void;
431
+ /** Callback when new layers are detected */
432
+ onNewLayers?: (layerIds: string[]) => void;
433
+ /** Layers to track (empty = track all) */
434
+ trackedLayers?: string[];
435
+ }
436
+
437
+ /**
438
+ * Hook for managing panel width state
439
+ *
440
+ * @example
441
+ * ```tsx
442
+ * const { width, setWidth, increaseWidth, decreaseWidth } = usePanelWidth({
443
+ * initialWidth: 320,
444
+ * minWidth: 240,
445
+ * maxWidth: 420
446
+ * });
447
+ *
448
+ * return (
449
+ * <div style={{ width: `${width}px` }}>
450
+ * <button onClick={() => decreaseWidth(10)}>-</button>
451
+ * <span>{width}px</span>
452
+ * <button onClick={() => increaseWidth(10)}>+</button>
453
+ * </div>
454
+ * );
455
+ * ```
456
+ */
457
+ export declare function usePanelWidth({ initialWidth, minWidth, maxWidth, }?: UsePanelWidthOptions): UsePanelWidthReturn;
458
+
459
+ export declare interface UsePanelWidthOptions {
460
+ /** Initial width */
461
+ initialWidth?: number;
462
+ /** Minimum width */
463
+ minWidth?: number;
464
+ /** Maximum width */
465
+ maxWidth?: number;
466
+ }
467
+
468
+ export declare interface UsePanelWidthReturn {
469
+ /** Current panel width */
470
+ width: number;
471
+ /** Set panel width (clamped to min/max) */
472
+ setWidth: (width: number) => void;
473
+ /** Increase width by step */
474
+ increaseWidth: (step?: number) => void;
475
+ /** Decrease width by step */
476
+ decreaseWidth: (step?: number) => void;
477
+ /** Reset to initial width */
478
+ resetWidth: () => void;
479
+ }
480
+
481
+ /**
482
+ * Hook for managing style editor state
483
+ *
484
+ * @example
485
+ * ```tsx
486
+ * const { activeEditor, openEditor, closeEditor, resetStyle } = useStyleEditor({ map });
487
+ *
488
+ * return (
489
+ * <button onClick={() => openEditor('my-layer')}>
490
+ * Edit Style
491
+ * </button>
492
+ * );
493
+ * ```
494
+ */
495
+ export declare function useStyleEditor({ map }: UseStyleEditorOptions): UseStyleEditorReturn;
496
+
497
+ export declare interface UseStyleEditorOptions {
498
+ /** MapLibre map instance */
499
+ map: Map_2;
500
+ }
501
+
502
+ export declare interface UseStyleEditorReturn {
503
+ /** Currently active editor (layer ID) */
504
+ activeEditor: string | null;
505
+ /** Original styles cache */
506
+ originalStyles: Map<string, OriginalStyle>;
507
+ /** Open style editor for a layer */
508
+ openEditor: (layerId: string) => void;
509
+ /** Close the active editor */
510
+ closeEditor: () => void;
511
+ /** Toggle editor for a layer */
512
+ toggleEditor: (layerId: string) => void;
513
+ /** Reset layer to original style */
514
+ resetStyle: (layerId: string) => void;
515
+ /** Apply style changes */
516
+ applyStyle: (layerId: string, property: string, value: any) => void;
517
+ }
518
+
519
+ export { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maplibre-gl-layer-control",
3
- "version": "0.18.2",
3
+ "version": "0.18.3",
4
4
  "description": "A comprehensive layer control for MapLibre GL with advanced styling capabilities",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -17,6 +17,16 @@
17
17
  "default": "./dist/index.cjs"
18
18
  }
19
19
  },
20
+ "./react": {
21
+ "import": {
22
+ "types": "./dist/types/react.d.ts",
23
+ "default": "./dist/react.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/types/react.d.ts",
27
+ "default": "./dist/react.cjs"
28
+ }
29
+ },
20
30
  "./style.css": "./dist/maplibre-gl-layer-control.css"
21
31
  },
22
32
  "files": [
@@ -81,7 +91,7 @@
81
91
  "@vitest/ui": "^4.0.17",
82
92
  "eslint": "^9.17.0",
83
93
  "jsdom": "^25.0.0",
84
- "maplibre-gl": "^5.14.0",
94
+ "maplibre-gl": "^6.13.0",
85
95
  "prettier": "^3.4.0",
86
96
  "react": "^18.3.0",
87
97
  "react-dom": "^18.3.0",