maplibre-gl-layer-control 0.1.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/LICENSE +21 -0
- package/README.md +192 -0
- package/dist/index.cjs +1166 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +1166 -0
- package/dist/index.mjs.map +1 -0
- package/dist/maplibre-gl-layer-control.css +644 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Qiusheng Wu
|
|
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,192 @@
|
|
|
1
|
+
# maplibre-gl-layer-control
|
|
2
|
+
|
|
3
|
+
A comprehensive layer control for MapLibre GL with advanced styling capabilities. Built with TypeScript and React, providing both vanilla JavaScript and React integration options.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ **Auto-detection** - Automatically detects layer properties (opacity, visibility) and generates friendly names
|
|
8
|
+
- ✅ **Layer visibility toggle** - Checkbox control for each layer
|
|
9
|
+
- ✅ **Layer opacity control** - Smooth opacity slider with type-aware property mapping
|
|
10
|
+
- ✅ **Resizable panel** - Adjustable panel width (240-420px) with keyboard support
|
|
11
|
+
- ✅ **Advanced style editor** - Per-layer-type styling controls:
|
|
12
|
+
- **Fill layers**: color, opacity, outline-color
|
|
13
|
+
- **Line layers**: color, width, opacity, blur
|
|
14
|
+
- **Circle layers**: color, radius, opacity, blur, stroke properties
|
|
15
|
+
- **Symbol layers**: text-color, text-halo-color, halo-width, text/icon-opacity
|
|
16
|
+
- **Raster layers**: opacity, brightness, saturation, contrast, hue-rotate
|
|
17
|
+
- ✅ **Dynamic layer detection** - Automatically detect and manage new layers
|
|
18
|
+
- ✅ **Background layer grouping** - Control all basemap layers as one group
|
|
19
|
+
- ✅ **Accessibility** - Full ARIA support and keyboard navigation
|
|
20
|
+
- ✅ **TypeScript** - Full type safety and IntelliSense support
|
|
21
|
+
- ✅ **React integration** - Optional React components and hooks
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install maplibre-gl-layer-control
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### Vanilla JavaScript
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import maplibregl from 'maplibre-gl';
|
|
35
|
+
import { LayerControl } from 'maplibre-gl-layer-control';
|
|
36
|
+
import 'maplibre-gl-layer-control/style.css';
|
|
37
|
+
|
|
38
|
+
const map = new maplibregl.Map({
|
|
39
|
+
container: 'map',
|
|
40
|
+
style: 'https://demotiles.maplibre.org/style.json',
|
|
41
|
+
center: [0, 0],
|
|
42
|
+
zoom: 2
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
map.on('load', () => {
|
|
46
|
+
// Add your custom layers
|
|
47
|
+
map.addLayer({
|
|
48
|
+
id: 'my-layer',
|
|
49
|
+
type: 'fill',
|
|
50
|
+
source: 'my-source',
|
|
51
|
+
paint: {
|
|
52
|
+
'fill-color': '#088',
|
|
53
|
+
'fill-opacity': 0.5
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Create layer control with auto-detection
|
|
58
|
+
// Option 1: Specify which layers to control (recommended for most use cases)
|
|
59
|
+
// - Shows specified layers with auto-detected opacity, visibility, and friendly names
|
|
60
|
+
// - Groups all other layers as "Background"
|
|
61
|
+
const layerControl = new LayerControl({
|
|
62
|
+
collapsed: false,
|
|
63
|
+
layers: ['my-layer'], // LayerControl auto-detects opacity, visibility, and generates friendly names
|
|
64
|
+
panelWidth: 340,
|
|
65
|
+
panelMinWidth: 240,
|
|
66
|
+
panelMaxWidth: 450
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Option 2: Show ALL layers individually (no layers parameter)
|
|
70
|
+
// - Auto-detects ALL layers from the map
|
|
71
|
+
// - Generates friendly names from layer IDs (e.g., 'countries-layer' → 'Countries Layer')
|
|
72
|
+
// const layerControl = new LayerControl({
|
|
73
|
+
// collapsed: false,
|
|
74
|
+
// panelWidth: 340,
|
|
75
|
+
// panelMinWidth: 240,
|
|
76
|
+
// panelMaxWidth: 450
|
|
77
|
+
// });
|
|
78
|
+
|
|
79
|
+
// Option 3: Manually specify layer states (for full control over names)
|
|
80
|
+
// const layerControl = new LayerControl({
|
|
81
|
+
// collapsed: false,
|
|
82
|
+
// layerStates: {
|
|
83
|
+
// 'my-layer': {
|
|
84
|
+
// visible: true,
|
|
85
|
+
// opacity: 0.5,
|
|
86
|
+
// name: 'My Custom Layer Name'
|
|
87
|
+
// }
|
|
88
|
+
// }
|
|
89
|
+
// });
|
|
90
|
+
|
|
91
|
+
map.addControl(layerControl, 'top-right');
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### React
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { useState, useEffect } from 'react';
|
|
99
|
+
import maplibregl, { Map as MapLibreMap } from 'maplibre-gl';
|
|
100
|
+
import { LayerControlReact } from 'maplibre-gl-layer-control/react';
|
|
101
|
+
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
102
|
+
import 'maplibre-gl-layer-control/style.css';
|
|
103
|
+
|
|
104
|
+
function MapComponent() {
|
|
105
|
+
const [map, setMap] = useState<MapLibreMap | null>(null);
|
|
106
|
+
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
const newMap = new maplibregl.Map({
|
|
109
|
+
container: 'map',
|
|
110
|
+
style: 'https://demotiles.maplibre.org/style.json',
|
|
111
|
+
center: [0, 0],
|
|
112
|
+
zoom: 2
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
newMap.on('load', () => {
|
|
116
|
+
// Add your custom layers here
|
|
117
|
+
setMap(newMap);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return () => newMap.remove();
|
|
121
|
+
}, []);
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<div>
|
|
125
|
+
<div id="map" style={{ width: '100%', height: '600px' }} />
|
|
126
|
+
{map && (
|
|
127
|
+
<LayerControlReact
|
|
128
|
+
map={map}
|
|
129
|
+
position="top-right"
|
|
130
|
+
layers={['my-layer']}
|
|
131
|
+
collapsed={false}
|
|
132
|
+
/>
|
|
133
|
+
)}
|
|
134
|
+
</div>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## API
|
|
140
|
+
|
|
141
|
+
### LayerControl Options
|
|
142
|
+
|
|
143
|
+
| Option | Type | Default | Description |
|
|
144
|
+
|--------|------|---------|-------------|
|
|
145
|
+
| `collapsed` | `boolean` | `true` | Start with panel collapsed |
|
|
146
|
+
| `layers` | `string[]` | `undefined` | Layer IDs to control (auto-detects all if omitted) |
|
|
147
|
+
| `layerStates` | `Record<string, LayerState>` | `undefined` | Manual layer state configuration |
|
|
148
|
+
| `panelWidth` | `number` | `320` | Initial panel width in pixels |
|
|
149
|
+
| `panelMinWidth` | `number` | `240` | Minimum panel width |
|
|
150
|
+
| `panelMaxWidth` | `number` | `420` | Maximum panel width |
|
|
151
|
+
|
|
152
|
+
### LayerState
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
interface LayerState {
|
|
156
|
+
visible: boolean; // Layer visibility
|
|
157
|
+
opacity: number; // Opacity (0-1)
|
|
158
|
+
name?: string; // Display name (auto-generated if omitted)
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Examples
|
|
163
|
+
|
|
164
|
+
See the [examples](./examples) folder for complete working examples:
|
|
165
|
+
|
|
166
|
+
- **[basic](./examples/basic)** - Simple vanilla JavaScript example
|
|
167
|
+
- **[full-demo](./examples/full-demo)** - Full demo with multiple layer types
|
|
168
|
+
- **[react](./examples/react)** - React integration example
|
|
169
|
+
|
|
170
|
+
## Development
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
# Install dependencies
|
|
174
|
+
npm install
|
|
175
|
+
|
|
176
|
+
# Run development server
|
|
177
|
+
npm run dev
|
|
178
|
+
|
|
179
|
+
# Run tests
|
|
180
|
+
npm test
|
|
181
|
+
|
|
182
|
+
# Build for production
|
|
183
|
+
npm run build
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT © Qiusheng Wu
|
|
189
|
+
|
|
190
|
+
## Contributing
|
|
191
|
+
|
|
192
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|