maplibre-gl-basemap-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.
Files changed (36) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +190 -0
  3. package/dist/BasemapControl-C40PophU.js +1343 -0
  4. package/dist/BasemapControl-C40PophU.js.map +1 -0
  5. package/dist/BasemapControl-pnHGO378.cjs +1342 -0
  6. package/dist/BasemapControl-pnHGO378.cjs.map +1 -0
  7. package/dist/index.cjs +57 -0
  8. package/dist/index.cjs.map +1 -0
  9. package/dist/index.mjs +57 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/maplibre-gl-basemap-control.css +272 -0
  12. package/dist/react.cjs +126 -0
  13. package/dist/react.cjs.map +1 -0
  14. package/dist/react.mjs +126 -0
  15. package/dist/react.mjs.map +1 -0
  16. package/dist/types/index.d.ts +5 -0
  17. package/dist/types/index.d.ts.map +1 -0
  18. package/dist/types/lib/core/BasemapControl.d.ts +57 -0
  19. package/dist/types/lib/core/BasemapControl.d.ts.map +1 -0
  20. package/dist/types/lib/core/BasemapControlReact.d.ts +3 -0
  21. package/dist/types/lib/core/BasemapControlReact.d.ts.map +1 -0
  22. package/dist/types/lib/core/catalog.d.ts +14 -0
  23. package/dist/types/lib/core/catalog.d.ts.map +1 -0
  24. package/dist/types/lib/core/types.d.ts +85 -0
  25. package/dist/types/lib/core/types.d.ts.map +1 -0
  26. package/dist/types/lib/hooks/index.d.ts +2 -0
  27. package/dist/types/lib/hooks/index.d.ts.map +1 -0
  28. package/dist/types/lib/hooks/useBasemapState.d.ts +12 -0
  29. package/dist/types/lib/hooks/useBasemapState.d.ts.map +1 -0
  30. package/dist/types/lib/utils/helpers.d.ts +86 -0
  31. package/dist/types/lib/utils/helpers.d.ts.map +1 -0
  32. package/dist/types/lib/utils/index.d.ts +2 -0
  33. package/dist/types/lib/utils/index.d.ts.map +1 -0
  34. package/dist/types/react.d.ts +4 -0
  35. package/dist/types/react.d.ts.map +1 -0
  36. package/package.json +107 -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,190 @@
1
+ # MapLibre GL Basemap Control
2
+
3
+ A MapLibre GL JS control for searching and switching public basemaps. It keeps the standard compact MapLibre control button, opens a floating searchable panel, and can be used from vanilla TypeScript or React.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/maplibre-gl-basemap-control.svg)](https://www.npmjs.com/package/maplibre-gl-basemap-control)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Features
9
+
10
+ - Search-first basemap picker inspired by QuickMapServices
11
+ - Built-in no-key catalog for common public basemaps
12
+ - Custom basemap and provider definitions
13
+ - MapLibre `IControl` implementation
14
+ - React wrapper and state hook
15
+ - Vite library build with ESM/CJS outputs and TypeScript declarations
16
+ - Docker and GitHub Actions examples workflow
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install maplibre-gl-basemap-control maplibre-gl
22
+ ```
23
+
24
+ ## Vanilla TypeScript
25
+
26
+ ```typescript
27
+ import maplibregl from 'maplibre-gl';
28
+ import { BasemapControl } from 'maplibre-gl-basemap-control';
29
+ import 'maplibre-gl-basemap-control/style.css';
30
+
31
+ const map = new maplibregl.Map({
32
+ container: 'map',
33
+ style: {
34
+ version: 8,
35
+ sources: {},
36
+ layers: [{ id: 'background', type: 'background' }],
37
+ },
38
+ center: [0, 0],
39
+ zoom: 2,
40
+ });
41
+
42
+ map.on('load', () => {
43
+ const basemaps = new BasemapControl({
44
+ title: 'Basemaps',
45
+ collapsed: true,
46
+ defaultBasemapId: 'carto-positron',
47
+ });
48
+
49
+ map.addControl(basemaps, 'top-right');
50
+ });
51
+ ```
52
+
53
+ ## React
54
+
55
+ ```tsx
56
+ import { useEffect, useRef, useState } from 'react';
57
+ import maplibregl, { Map } from 'maplibre-gl';
58
+ import { BasemapControlReact, useBasemapState } from 'maplibre-gl-basemap-control/react';
59
+ import 'maplibre-gl-basemap-control/style.css';
60
+
61
+ function App() {
62
+ const mapContainer = useRef<HTMLDivElement>(null);
63
+ const [map, setMap] = useState<Map | null>(null);
64
+ const { state, setState } = useBasemapState({
65
+ collapsed: false,
66
+ activeBasemapId: 'carto-positron',
67
+ });
68
+
69
+ useEffect(() => {
70
+ if (!mapContainer.current) return;
71
+
72
+ const mapInstance = new maplibregl.Map({
73
+ container: mapContainer.current,
74
+ style: { version: 8, sources: {}, layers: [{ id: 'background', type: 'background' }] },
75
+ center: [0, 0],
76
+ zoom: 2,
77
+ });
78
+
79
+ mapInstance.on('load', () => setMap(mapInstance));
80
+ return () => mapInstance.remove();
81
+ }, []);
82
+
83
+ return (
84
+ <>
85
+ <div ref={mapContainer} />
86
+ {map && (
87
+ <BasemapControlReact
88
+ map={map}
89
+ collapsed={state.collapsed}
90
+ activeBasemapId={state.activeBasemapId}
91
+ onStateChange={setState}
92
+ onBasemapChange={(basemap) => console.log(basemap.name)}
93
+ />
94
+ )}
95
+ </>
96
+ );
97
+ }
98
+ ```
99
+
100
+ ## Custom Basemaps
101
+
102
+ The built-in catalog can be extended or replaced.
103
+
104
+ ```typescript
105
+ import type { BasemapDefinition } from 'maplibre-gl-basemap-control';
106
+
107
+ const customBasemaps: BasemapDefinition[] = [
108
+ {
109
+ id: 'example-raster',
110
+ name: 'Example Raster',
111
+ provider: 'example',
112
+ type: 'raster',
113
+ category: 'Custom',
114
+ attribution: '&copy; Example Provider',
115
+ source: {
116
+ type: 'raster',
117
+ tiles: ['https://tiles.example.com/{z}/{x}/{y}.png'],
118
+ tileSize: 256,
119
+ maxzoom: 19,
120
+ },
121
+ tags: ['custom', 'street'],
122
+ },
123
+ ];
124
+
125
+ const control = new BasemapControl({
126
+ basemaps: customBasemaps,
127
+ providers: [{ id: 'example', name: 'Example Provider', category: 'Custom' }],
128
+ includeDefaultBasemaps: true,
129
+ });
130
+ ```
131
+
132
+ Set `includeDefaultBasemaps: false` to use only your supplied catalog.
133
+
134
+ ## API
135
+
136
+ ### BasemapControl Options
137
+
138
+ | Option | Type | Default | Description |
139
+ |--------|------|---------|-------------|
140
+ | `collapsed` | `boolean` | `true` | Whether the panel starts collapsed |
141
+ | `position` | `string` | `'top-right'` | Preferred control position |
142
+ | `title` | `string` | `'Basemaps'` | Panel title and button label |
143
+ | `panelWidth` | `number` | `340` | Floating panel width in pixels |
144
+ | `className` | `string` | `''` | Extra class for the control button container |
145
+ | `basemaps` | `BasemapDefinition[]` | `[]` | Custom basemaps to add or use |
146
+ | `providers` | `BasemapProvider[]` | `[]` | Custom provider labels |
147
+ | `includeDefaultBasemaps` | `boolean` | `true` | Include the built-in public catalog |
148
+ | `defaultBasemapId` | `string` | `undefined` | Basemap to apply after the control is added |
149
+
150
+ ### Methods
151
+
152
+ - `setBasemap(id)` - Apply a basemap and remove the previous plugin-managed basemap
153
+ - `getActiveBasemap()` - Return the current basemap definition
154
+ - `getBasemaps()` - Return the catalog
155
+ - `setBasemaps(basemaps)` - Replace the catalog
156
+ - `toggle()`, `expand()`, `collapse()` - Control panel visibility
157
+ - `getState()`, `setState(state)` - Read or update UI state
158
+ - `on(event, handler)`, `off(event, handler)` - Subscribe to events
159
+ - `getMap()`, `getContainer()` - Access MapLibre/control internals
160
+
161
+ ### Events
162
+
163
+ - `basemapchange`
164
+ - `error`
165
+ - `collapse`
166
+ - `expand`
167
+ - `statechange`
168
+
169
+ ## Attribution
170
+
171
+ Built-in basemaps include attribution strings in their MapLibre source definitions. Consumers are responsible for confirming that selected providers and usage volumes match their project requirements.
172
+
173
+ ## Development
174
+
175
+ ```bash
176
+ npm install
177
+ npm run dev
178
+ npm test
179
+ npm run build
180
+ npm run build:examples
181
+ ```
182
+
183
+ ## Docker
184
+
185
+ ```bash
186
+ docker build -t maplibre-gl-basemap-control .
187
+ docker run -p 8080:80 maplibre-gl-basemap-control
188
+ ```
189
+
190
+ Open http://localhost:8080/maplibre-gl-basemap-control/ to view the examples.