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.
package/README.md CHANGED
@@ -6,6 +6,8 @@
6
6
  [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/p/github/opengeos/maplibre-gl-layer-control)
7
7
  [![Open in StackBlitz](https://img.shields.io/badge/Open%20in-StackBlitz-blue?logo=stackblitz)](https://stackblitz.com/github/opengeos/maplibre-gl-layer-control)
8
8
 
9
+ **[Live demo / Showcase](https://opengeos.org/maplibre-gl-layer-control/)** — open the Full Demo for pattern fills, nested folders, and interactive layer styling.
10
+
9
11
  A comprehensive layer control for MapLibre GL with advanced styling capabilities. Built with TypeScript and React, providing both vanilla JavaScript and React integration options.
10
12
 
11
13
  ## Features
@@ -16,11 +18,12 @@ A comprehensive layer control for MapLibre GL with advanced styling capabilities
16
18
  - ✅ **Layer symbols** - Visual type indicators (colored shapes) next to layer names, auto-detected from layer paint properties
17
19
  - ✅ **Resizable panel** - **Drag either edge of the panel to resize it**; double-click an edge to reset to the default width. The panel also grows to fill the available height so long layer lists only scroll once they exceed the map.
18
20
  - ✅ **Advanced style editor** - Per-layer-type styling controls:
19
- - **Fill layers**: color, opacity, outline-color
21
+ - **Fill layers**: color, opacity, outline-color, [pattern picker](#fill-pattern-editor) (sprite/`addImage()` images, SDF-tinted by color)
20
22
  - **Line layers**: color, width, opacity, blur
21
- - **Circle layers**: color, radius, opacity, blur, stroke properties
22
- - **Symbol layers**: text-color, text-halo-color, halo-width, text/icon-opacity
23
+ - **Circle layers**: color, radius, opacity, stroke color, stroke width
24
+ - **Symbol layers**: text-color, text-opacity, icon-opacity
23
25
  - **Raster layers**: opacity, brightness, saturation, contrast, hue-rotate
26
+ - **Exact numeric entry**: Click a displayed numeric value (or focus it and press Enter/Space) to enter an exact value without removing the slider. Enter or leaving the input applies; Escape cancels. Values are clamped to the control's range and rounded to its displayed decimal precision, rather than snapped to the slider's coarse increment.
24
27
  - ✅ **Dynamic layer detection** - Automatically detect and manage new layers
25
28
  - ✅ **Background layer grouping** - Control all basemap layers as one group
26
29
  - ✅ **Background layer legend** - Gear icon to toggle individual background layer visibility
@@ -31,21 +34,77 @@ A comprehensive layer control for MapLibre GL with advanced styling capabilities
31
34
  - ✅ **Custom layer adapters** - Integrate non-MapLibre layers (deck.gl, Zarr, etc.)
32
35
  - ✅ **Layer groups** - Nest an adapter's layers in collapsible, nestable folders with their own visibility checkbox and opacity slider
33
36
 
37
+ ### Pattern-fill previews
38
+
39
+ Fill-layer symbols and background-legend symbols display `fill-pattern` images
40
+ from the map's sprite or `map.addImage()`. Regular images retain their colours
41
+ and transparency. Images registered with `{ sdf: true }` are decoded from their
42
+ alpha distance field and tinted with the preview's `fill-color` (black when
43
+ unspecified), rather than shown as raw distance-field pixels.
44
+
45
+ The preview respects image `pixelRatio` and scales large tiles down to keep the
46
+ repeating motif visible. Paint changes and newly loaded images refresh existing
47
+ symbols. `map.updateImage()` changes appear on the next map `idle` event; call
48
+ `map.triggerRepaint()` if no render is pending. Missing images retain the solid
49
+ fill preview until the image becomes available.
50
+
51
+ For expressions, the preview uses the first available literal image output; it
52
+ does not evaluate feature-dependent image names. MapLibre GL JS 6.8.0 and later
53
+ colour SDF fill patterns on the map with `fill-color`; this package supports
54
+ `maplibre-gl >= 3`, and older renderers may show raw SDF pixels even when the
55
+ control displays a tinted preview.
56
+
57
+ #### Fill pattern editor
58
+
59
+ The fill style editor's **Fill Pattern** list shows each image from
60
+ `map.listImages()` (style sprite and `map.addImage()`) with its ID, a preview,
61
+ and an `SDF` badge when applicable. The current value is shown above the list.
62
+ **None (solid fill)** clears `fill-pattern`; selecting an image applies it
63
+ immediately and calls
64
+ `onLayerStyleChange(layerId, 'fill-pattern', idOrUndefined)`.
65
+
66
+ For grouped editors, differing pattern values appear as **Multiple patterns**;
67
+ choosing an option updates every layer in the group. Fill Color is disabled if
68
+ any grouped layer uses a loaded non-SDF image.
69
+
70
+ SDF images are recoloured through that layer's Fill Color; sprite pixels are
71
+ never modified, so layers sharing an image can use separate colours. MapLibre
72
+ GL JS 6.8.0 or later is required to render the tinted SDF pattern on the map.
73
+ Non-SDF images keep their own colours. Fill Color is disabled while one is
74
+ selected, and the existing `fill-color` value is left unchanged; it is
75
+ available again for SDF patterns and solid fills.
76
+ Data-driven `fill-pattern` expressions remain untouched until an option is
77
+ selected. An unavailable current image appears as `(not loaded)` and remains
78
+ unchanged until another option is selected.
79
+
80
+ The list reflects images loaded when the editor opens. Call
81
+ `refreshStyleEditor()` after loading images to refresh an open editor. **Reset
82
+ Style** restores the original pattern, including removing a pattern added to a
83
+ layer that had none.
84
+
34
85
  ## Installation
35
86
 
36
87
  ```bash
37
88
  npm install maplibre-gl-layer-control
38
89
  ```
39
90
 
91
+ The development setup and examples use MapLibre GL JS 6.13.0. Version 6 uses
92
+ named ESM exports. Vite apps must configure the worker with `?worker&url`, as
93
+ shown below; direct CDN module imports auto-detect it. See the
94
+ [v5-to-v6 migration guide](https://maplibre.org/maplibre-gl-js/docs/guides/v5-to-v6-migration-guide/).
95
+
40
96
  ## Quick Start
41
97
 
42
98
  ### Vanilla JavaScript
43
99
 
44
100
  ```typescript
45
- import maplibregl from 'maplibre-gl';
101
+ import * as maplibregl from 'maplibre-gl';
102
+ import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url';
46
103
  import { LayerControl } from 'maplibre-gl-layer-control';
47
104
  import 'maplibre-gl-layer-control/style.css';
48
105
 
106
+ maplibregl.setWorkerUrl(workerUrl);
107
+
49
108
  const map = new maplibregl.Map({
50
109
  container: 'map',
51
110
  style: 'https://demotiles.maplibre.org/style.json',
@@ -117,11 +176,15 @@ map.on('load', () => {
117
176
 
118
177
  ```typescript
119
178
  import { useState, useEffect } from 'react';
120
- import maplibregl, { Map as MapLibreMap } from 'maplibre-gl';
179
+ import * as maplibregl from 'maplibre-gl';
180
+ import type { Map as MapLibreMap } from 'maplibre-gl';
181
+ import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url';
121
182
  import { LayerControlReact } from 'maplibre-gl-layer-control/react';
122
183
  import 'maplibre-gl/dist/maplibre-gl.css';
123
184
  import 'maplibre-gl-layer-control/style.css';
124
185
 
186
+ maplibregl.setWorkerUrl(workerUrl);
187
+
125
188
  function MapComponent() {
126
189
  const [map, setMap] = useState<MapLibreMap | null>(null);
127
190
 
@@ -166,17 +229,23 @@ function MapComponent() {
166
229
  | `collapsed` | `boolean` | `true` | Start with panel collapsed |
167
230
  | `layers` | `string[]` | `undefined` | Layer IDs to control (auto-detects all if omitted) |
168
231
  | `layerStates` | `Record<string, LayerState>` | `undefined` | Manual layer state configuration |
169
- | `panelWidth` | `number` | `320` | Initial panel width in pixels |
232
+ | `panelWidth` | `number` | `350` | Initial panel width in pixels |
170
233
  | `panelMinWidth` | `number` | `240` | Minimum panel width |
171
234
  | `panelMaxWidth` | `number` | `960` | Maximum panel width |
172
235
  | `panelMaxHeight` | `number` | `undefined` | Maximum panel height in pixels. Omit to fill the available vertical space (scrollable only when the layer list is taller than the map) |
173
236
  | `showStyleEditor` | `boolean` | `true` | Show gear icon for style editor |
237
+ | `showAllStyleProperties` | `boolean` | `false` | Show every style control the editor supports for a layer's type (e.g. fill opacity and outline color, line opacity/blur, circle stroke, symbol text/icon opacity), even when the layer style does not set that property. Unset properties show their effective value (style-spec default, or the inherited value: `fill-outline-color` follows `fill-color`). Opening the editor never writes to the map style; a property is set only when you edit its control, and Reset Style unsets it again. Controls that cannot take effect (outline color with `fill-antialias: false` or a fill pattern, line color with a line pattern, text properties without `text-field`, icon opacity without `icon-image`) or whose value is an expression stay visible but disabled with an explanation; expressions are never replaced. Applies to custom-layer adapters' native sublayers too. When `false`, controls for optional properties appear only once the layer sets them |
174
238
  | `showOpacitySlider` | `boolean` | `true` | Show opacity slider for layers |
175
239
  | `showLayerSymbol` | `boolean` | `true` | Show layer type symbols (colored icons) next to layer names |
176
240
  | `excludeDrawnLayers` | `boolean` | `true` | Exclude layers from drawing libraries (Geoman, Mapbox GL Draw, etc.) |
177
241
  | `excludeLayers` | `string[]` | `undefined` | Array of wildcard patterns to exclude layers by name (e.g., `['*-temp-*', 'debug-*']`) |
178
242
  | `customLayerAdapters` | `CustomLayerAdapter[]` | `undefined` | Adapters for non-MapLibre layers (deck.gl, Zarr, etc.) |
179
243
  | `basemapStyleUrl` | `string` | `undefined` | URL of basemap style JSON for reliable layer detection (see below) |
244
+ | `enableContextMenu` | `boolean` | `true` | Enable the right-click context menu on layers (Rename, Zoom to Layer, Move Up/Down/to Top/to Bottom, Remove Layer) |
245
+ | `enableDragAndDrop` | `boolean` | `true` | Enable drag-and-drop reordering of layers in the panel |
246
+ | `onLayerRename` | `(layerId: string, oldName: string, newName: string) => void` | `undefined` | Called when a layer is renamed through the context menu |
247
+ | `onLayerReorder` | `(layerOrder: string[]) => void` | `undefined` | Called when layers are reordered, via drag-and-drop or the context-menu move actions; receives the new layer ID order |
248
+ | `onLayerRemove` | `(layerId: string) => void` | `undefined` | Called when a layer is removed from the map through the context menu or the style editor's **Remove Layer** button |
180
249
  | `enableBackgroundPresets` | `boolean` | `true` | Show the "Saved configurations" controls in the Background Layers panel |
181
250
  | `backgroundPresetStorageKey` | `string` | `'maplibre-layer-control:background-presets'` | `localStorage` key under which background visibility presets are stored |
182
251
  | `onBackgroundPresetsChange` | `(presets: BackgroundPresets) => void` | `undefined` | Called whenever the saved preset set changes (created or deleted); applying a preset does not change the set, so it does not fire |
@@ -258,7 +327,8 @@ map.on('load', () => {
258
327
  See the [examples](./examples) folder for complete working examples:
259
328
 
260
329
  - **[basic](./examples/basic)** - Simple vanilla JavaScript example
261
- - **[full-demo](./examples/full-demo)** - Full demo with multiple layer types and `basemapStyleUrl` for reliable basemap detection
330
+ - **[full-demo](./examples/full-demo)** - Full demo with fill, line, circle and raster layers, regular and SDF-tinted `fill-pattern` previews, nested layer folders through the custom-layer adapter, and `basemapStyleUrl` for reliable basemap detection
331
+ - **[categorized-legend](./examples/categorized-legend)** - Match fill and case line legends with live category/plain-color switching and local GeoJSON
262
332
  - **[dynamic-layers](./examples/dynamic-layers)** - Auto-detect layers added before or after control
263
333
  - **[background-legend](./examples/background-legend)** - Background layer visibility control
264
334
  - **[react](./examples/react)** - React integration example
@@ -285,6 +355,14 @@ The symbol color is automatically extracted from the layer's paint properties (e
285
355
 
286
356
  Line previews reflect `line-dasharray`, scaled to fit the swatch. Circle previews use `circle-stroke-color` for the border; setting `circle-stroke-width` to `0` removes the border.
287
357
 
358
+ Top-level `match` and `case` color expressions on fill, line, circle, symbol, and fill-extrusion layers show a fixed four-color categorized icon instead of a single-color preview. The icon identifies the renderer, not the actual layer colors. Use the chevron beside the layer name to expand its initially collapsed category legend; each entry uses the layer's geometry swatch, and the fallback appears last as **Other**. Expansion is retained when layer rows are rebuilt.
359
+
360
+ Match labels list their values (grouped values are comma-separated). Case labels summarize simple comparisons and property-presence checks; complex conditions use JSON. Labels are limited to 40 characters, with full text in tooltips. Category colors accept hex and RGB(A) string literals; named colors, HSL(A), and nested output expressions use neutral gray. Only top-level `match`/`case` is categorized: `interpolate` and `step` retain their existing first-color behavior. Patterned fills, custom layers, and Background legends are unchanged. Setting `showLayerSymbol: false` also hides category toggles and legends.
361
+
362
+ Paint changes refresh category legends in place without closing an open style editor. Switching a categorized expression to a plain color removes its chevron and restores the normal geometry symbol.
363
+
364
+ For custom integrations, `getLayerColorCategories(map, layerId, layerType)` returns `{ property, categories }` or `null`, where each `ColorCategory` has `label`, normalized hex `color` (or `null`), and full `title`. `createCategorizedSymbolSVG(size = 16)` returns the fixed indicator SVG. Both helpers and the `ColorCategory` / `ColorCategories` types are exported from the package.
365
+
288
366
  To disable layer symbols:
289
367
 
290
368
  ```typescript
@@ -474,6 +552,8 @@ deckAdapter.notifyLayerAdded('my-deck-layer');
474
552
 
475
553
  #### Layer Groups
476
554
 
555
+ See the [Full Demo](https://opengeos.org/maplibre-gl-layer-control/examples/full-demo/index.html) (`examples/full-demo/main.ts`) for a working adapter with two-level folders whose visibility and opacity are combined with each layer's own and applied to the map.
556
+
477
557
  An adapter can organize its layers into collapsible folders by implementing the optional group methods. The panel then nests each layer under its group, nested groups included, and gives every group a collapse toggle, a visibility checkbox, and an opacity slider.
478
558
 
479
559
  ```typescript