maplibre-gl-layer-control 0.18.2 → 0.19.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/README.md +108 -7
- package/dist/LayerControl-BxSfUXK6.cjs +6251 -0
- package/dist/LayerControl-BxSfUXK6.cjs.map +1 -0
- package/dist/LayerControl-CvH3r0T2.js +6252 -0
- package/dist/LayerControl-CvH3r0T2.js.map +1 -0
- package/dist/index.cjs +20 -5166
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +20 -5166
- package/dist/index.mjs.map +1 -1
- package/dist/maplibre-gl-layer-control.css +168 -1
- package/dist/react.cjs +269 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.mjs +269 -0
- package/dist/react.mjs.map +1 -0
- package/dist/types/index.d.ts +236 -18
- package/dist/types/react.d.ts +528 -0
- package/package.json +12 -2
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
[](https://codesandbox.io/p/github/opengeos/maplibre-gl-layer-control)
|
|
7
7
|
[](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,
|
|
22
|
-
- **Symbol layers**: text-color, text-
|
|
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
|
|
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,24 @@ 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` | `
|
|
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
|
+
| `basemapLayerIds` | `string[]` | `undefined` | Basemap layer IDs, when you already know them; takes precedence over `basemapStyleUrl`, and an empty array means "no basemap layers" (see below) |
|
|
245
|
+
| `enableContextMenu` | `boolean` | `true` | Enable the right-click context menu on layers (Rename, Zoom to Layer, Move Up/Down/to Top/to Bottom, Remove Layer) |
|
|
246
|
+
| `enableDragAndDrop` | `boolean` | `true` | Enable drag-and-drop reordering of layers in the panel |
|
|
247
|
+
| `onLayerRename` | `(layerId: string, oldName: string, newName: string) => void` | `undefined` | Called when a layer is renamed through the context menu |
|
|
248
|
+
| `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 |
|
|
249
|
+
| `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
250
|
| `enableBackgroundPresets` | `boolean` | `true` | Show the "Saved configurations" controls in the Background Layers panel |
|
|
181
251
|
| `backgroundPresetStorageKey` | `string` | `'maplibre-layer-control:background-presets'` | `localStorage` key under which background visibility presets are stored |
|
|
182
252
|
| `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 |
|
|
@@ -234,6 +304,16 @@ When `basemapStyleUrl` is provided:
|
|
|
234
304
|
- All other layers (user-added) are shown individually in the control
|
|
235
305
|
- New layers added later are automatically detected as user layers
|
|
236
306
|
|
|
307
|
+
### Explicit basemap layer IDs
|
|
308
|
+
|
|
309
|
+
When the application already knows which layers are the basemap (for example, a style built in code with no URL to fetch), pass them directly. Nothing is fetched, and an empty array is honoured as "no basemap layers" instead of falling back to the heuristics below:
|
|
310
|
+
|
|
311
|
+
```javascript
|
|
312
|
+
const layerControl = new LayerControl({
|
|
313
|
+
basemapLayerIds: map.getStyle().layers.map((layer) => layer.id),
|
|
314
|
+
});
|
|
315
|
+
```
|
|
316
|
+
|
|
237
317
|
### Automatic Detection Without basemapStyleUrl
|
|
238
318
|
|
|
239
319
|
Even without `basemapStyleUrl`, the control uses source-based heuristics to detect user-added layers. Custom MapLibre layers (using `map.addLayer()`) are automatically detected whether they are added **before** or **after** the layer control - no custom adapter is needed for standard MapLibre layer types!
|
|
@@ -258,7 +338,8 @@ map.on('load', () => {
|
|
|
258
338
|
See the [examples](./examples) folder for complete working examples:
|
|
259
339
|
|
|
260
340
|
- **[basic](./examples/basic)** - Simple vanilla JavaScript example
|
|
261
|
-
- **[full-demo](./examples/full-demo)** - Full demo with
|
|
341
|
+
- **[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
|
|
342
|
+
- **[categorized-legend](./examples/categorized-legend)** - Match fill and case line legends with live category/plain-color switching and local GeoJSON
|
|
262
343
|
- **[dynamic-layers](./examples/dynamic-layers)** - Auto-detect layers added before or after control
|
|
263
344
|
- **[background-legend](./examples/background-legend)** - Background layer visibility control
|
|
264
345
|
- **[react](./examples/react)** - React integration example
|
|
@@ -285,6 +366,14 @@ The symbol color is automatically extracted from the layer's paint properties (e
|
|
|
285
366
|
|
|
286
367
|
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
368
|
|
|
369
|
+
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.
|
|
370
|
+
|
|
371
|
+
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.
|
|
372
|
+
|
|
373
|
+
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.
|
|
374
|
+
|
|
375
|
+
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.
|
|
376
|
+
|
|
288
377
|
To disable layer symbols:
|
|
289
378
|
|
|
290
379
|
```typescript
|
|
@@ -474,6 +563,8 @@ deckAdapter.notifyLayerAdded('my-deck-layer');
|
|
|
474
563
|
|
|
475
564
|
#### Layer Groups
|
|
476
565
|
|
|
566
|
+
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.
|
|
567
|
+
|
|
477
568
|
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
569
|
|
|
479
570
|
```typescript
|
|
@@ -502,6 +593,16 @@ interface CustomLayerAdapter {
|
|
|
502
593
|
- **Show All** / **Hide All** also show or hide every group.
|
|
503
594
|
- When groups change outside the control, call `layerControl.refreshGroups()`. It rebuilds the panel when a group was added, removed, or re-parented, or a layer changed group, and otherwise updates the group rows in place.
|
|
504
595
|
|
|
596
|
+
### Driving the panel from outside
|
|
597
|
+
|
|
598
|
+
Applications that keep layer state in their own store (and drive the map from it) can mirror that state into the panel without rebuilding it:
|
|
599
|
+
|
|
600
|
+
| Method | Description |
|
|
601
|
+
|--------|-------------|
|
|
602
|
+
| `setLayerState(layerId, { visible?, opacity?, name? })` | Update a row's checkbox, opacity slider and name in place, without touching the map or firing callbacks. Only listed layers are updated, except that `"Background"` adds the Background row when there is none (a style whose basemap lives in style imports). Returns `false` (and does nothing) until the control has listed the map's layers, which is after `onAdd` and after any `basemapStyleUrl` fetch; otherwise `true` when a state was updated or created. |
|
|
603
|
+
| `setLayerOrder(layerIds)` | Set the panel order (top first) for layers with nothing on the map to sort them by, then redraw the rows. Use it to put the panel back after refusing a drag or context-menu move reported through `onLayerReorder`. A hidden custom layer whose adapter has other layers on the map keeps its slot beside them in the adapter's list. |
|
|
604
|
+
| `isCollapsed()` / `expand()` / `collapse()` | Read or change whether the panel is open, for example to remount a control in the state the user left it. |
|
|
605
|
+
|
|
505
606
|
#### Limitations
|
|
506
607
|
|
|
507
608
|
- **Style Editor**: The style editor (gear icon) is not available for custom layers since they don't use MapLibre's paint properties. Clicking the gear icon will show an info panel explaining this.
|