@geolonia/maps-react 0.0.1 → 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/dist/index.cjs ADDED
@@ -0,0 +1,957 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ AttributionControl: () => AttributionControl,
34
+ Control: () => Control,
35
+ FullscreenControl: () => FullscreenControl,
36
+ GeolocateControl: () => GeolocateControl,
37
+ Layer: () => Layer,
38
+ Map: () => Map,
39
+ MapContext: () => MapContext,
40
+ MapProvider: () => MapProvider,
41
+ MapProviderContext: () => MapProviderContext,
42
+ Marker: () => Marker,
43
+ NavigationControl: () => NavigationControl,
44
+ Popup: () => Popup,
45
+ ScaleControl: () => ScaleControl,
46
+ Source: () => Source,
47
+ SourceContext: () => SourceContext,
48
+ useControl: () => useControl,
49
+ useMap: () => useMap,
50
+ useMaps: () => useMaps,
51
+ useSourceId: () => useSourceId
52
+ });
53
+ module.exports = __toCommonJS(index_exports);
54
+
55
+ // src/components/control.tsx
56
+ var import_react2 = require("react");
57
+ var import_react_dom = require("react-dom");
58
+
59
+ // src/map-context.ts
60
+ var import_react = require("react");
61
+ var MapContext = (0, import_react.createContext)(null);
62
+ var MapProviderContext = (0, import_react.createContext)(null);
63
+ function useMap() {
64
+ const map = (0, import_react.useContext)(MapContext);
65
+ if (!map) {
66
+ throw new Error("useMap must be used within a <Map> component");
67
+ }
68
+ return map;
69
+ }
70
+ function useMaps() {
71
+ const current = (0, import_react.useContext)(MapContext) ?? void 0;
72
+ const registry = (0, import_react.useContext)(MapProviderContext);
73
+ return {
74
+ ...current ? { current } : {},
75
+ maps: { ...registry?.maps ?? {} }
76
+ };
77
+ }
78
+
79
+ // src/components/control.tsx
80
+ function Control({
81
+ position = "top-right",
82
+ onAdd,
83
+ onRemove,
84
+ children
85
+ }) {
86
+ const map = useMap();
87
+ const [container, setContainer] = (0, import_react2.useState)(null);
88
+ (0, import_react2.useEffect)(() => {
89
+ let el = null;
90
+ const control = {
91
+ onAdd: (m) => {
92
+ el = onAdd ? onAdd(m) : (() => {
93
+ const div = document.createElement("div");
94
+ div.className = "maplibregl-ctrl";
95
+ return div;
96
+ })();
97
+ setContainer(el);
98
+ return el;
99
+ },
100
+ onRemove: (m) => {
101
+ onRemove?.(m);
102
+ el?.remove();
103
+ el = null;
104
+ setContainer(null);
105
+ }
106
+ };
107
+ map.addControl(control, position);
108
+ return () => {
109
+ map.removeControl(control);
110
+ };
111
+ }, [map]);
112
+ return container ? (0, import_react_dom.createPortal)(children, container) : null;
113
+ }
114
+
115
+ // src/components/controls.tsx
116
+ var import_maplibre_gl = __toESM(require("maplibre-gl"), 1);
117
+ var import_react3 = require("react");
118
+ function useControl(factory, options) {
119
+ const map = useMap();
120
+ const position = options?.position;
121
+ (0, import_react3.useEffect)(() => {
122
+ const ctrl = factory();
123
+ map.addControl(ctrl, position);
124
+ return () => {
125
+ safeRemoveControl(map, ctrl);
126
+ };
127
+ }, [map, position]);
128
+ }
129
+ function defined(obj) {
130
+ const out = {};
131
+ for (const k of Object.keys(obj)) {
132
+ if (obj[k] !== void 0) out[k] = obj[k];
133
+ }
134
+ return out;
135
+ }
136
+ function safeRemoveControl(map, ctrl) {
137
+ try {
138
+ map.removeControl(ctrl);
139
+ } catch {
140
+ }
141
+ }
142
+ function NavigationControl({
143
+ position = "top-right",
144
+ showCompass,
145
+ showZoom,
146
+ visualizePitch
147
+ } = {}) {
148
+ const map = useMap();
149
+ (0, import_react3.useEffect)(() => {
150
+ const ctrl = new import_maplibre_gl.default.NavigationControl(
151
+ defined({ showCompass, showZoom, visualizePitch })
152
+ );
153
+ map.addControl(ctrl, position);
154
+ return () => {
155
+ safeRemoveControl(map, ctrl);
156
+ };
157
+ }, [map, position, showCompass, showZoom, visualizePitch]);
158
+ return null;
159
+ }
160
+ function GeolocateControl({
161
+ position = "top-right",
162
+ positionOptions,
163
+ fitBoundsOptions,
164
+ trackUserLocation,
165
+ showAccuracyCircle,
166
+ showUserLocation
167
+ } = {}) {
168
+ const map = useMap();
169
+ (0, import_react3.useEffect)(() => {
170
+ const ctrl = new import_maplibre_gl.default.GeolocateControl(
171
+ defined({
172
+ positionOptions,
173
+ fitBoundsOptions,
174
+ trackUserLocation,
175
+ showAccuracyCircle,
176
+ showUserLocation
177
+ })
178
+ );
179
+ map.addControl(ctrl, position);
180
+ return () => {
181
+ safeRemoveControl(map, ctrl);
182
+ };
183
+ }, [
184
+ map,
185
+ position,
186
+ positionOptions,
187
+ fitBoundsOptions,
188
+ trackUserLocation,
189
+ showAccuracyCircle,
190
+ showUserLocation
191
+ ]);
192
+ return null;
193
+ }
194
+ function FullscreenControl({
195
+ position = "top-right",
196
+ container
197
+ } = {}) {
198
+ const map = useMap();
199
+ (0, import_react3.useEffect)(() => {
200
+ const ctrl = new import_maplibre_gl.default.FullscreenControl(defined({ container }));
201
+ map.addControl(ctrl, position);
202
+ return () => {
203
+ safeRemoveControl(map, ctrl);
204
+ };
205
+ }, [map, position, container]);
206
+ return null;
207
+ }
208
+ function ScaleControl({
209
+ position = "bottom-left",
210
+ maxWidth,
211
+ unit
212
+ } = {}) {
213
+ const map = useMap();
214
+ (0, import_react3.useEffect)(() => {
215
+ const ctrl = new import_maplibre_gl.default.ScaleControl(defined({ maxWidth, unit }));
216
+ map.addControl(ctrl, position);
217
+ return () => {
218
+ safeRemoveControl(map, ctrl);
219
+ };
220
+ }, [map, position, maxWidth, unit]);
221
+ return null;
222
+ }
223
+ function AttributionControl({
224
+ position = "bottom-right",
225
+ compact,
226
+ customAttribution
227
+ } = {}) {
228
+ const map = useMap();
229
+ (0, import_react3.useEffect)(() => {
230
+ const ctrl = new import_maplibre_gl.default.AttributionControl(
231
+ defined({ compact, customAttribution })
232
+ );
233
+ map.addControl(ctrl, position);
234
+ return () => {
235
+ safeRemoveControl(map, ctrl);
236
+ };
237
+ }, [map, position, compact, customAttribution]);
238
+ return null;
239
+ }
240
+
241
+ // src/components/layer.tsx
242
+ var import_react5 = require("react");
243
+
244
+ // src/source-context.ts
245
+ var import_react4 = require("react");
246
+ var SourceContext = (0, import_react4.createContext)(null);
247
+ function useSourceId() {
248
+ return (0, import_react4.useContext)(SourceContext);
249
+ }
250
+
251
+ // src/components/layer.tsx
252
+ function shallowEqual(a, b) {
253
+ if (a === b) return true;
254
+ if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
255
+ const ka = Object.keys(a);
256
+ const kb = Object.keys(b);
257
+ if (ka.length !== kb.length) return false;
258
+ for (const k of ka) {
259
+ if (a[k] !== b[k]) {
260
+ return false;
261
+ }
262
+ }
263
+ return true;
264
+ }
265
+ function deepEqual(a, b) {
266
+ if (a === b) return true;
267
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
268
+ return false;
269
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
270
+ const ka = Object.keys(a);
271
+ const kb = Object.keys(b);
272
+ if (ka.length !== kb.length) return false;
273
+ for (const k of ka) {
274
+ if (!deepEqual(
275
+ a[k],
276
+ b[k]
277
+ )) {
278
+ return false;
279
+ }
280
+ }
281
+ return true;
282
+ }
283
+ function Layer({ beforeId, ...layerSpec }) {
284
+ const map = useMap();
285
+ const ctxSourceId = useSourceId();
286
+ const spec = ctxSourceId && !("source" in layerSpec && layerSpec.source) ? { ...layerSpec, source: ctxSourceId } : layerSpec;
287
+ const prevRef = (0, import_react5.useRef)({});
288
+ (0, import_react5.useEffect)(() => {
289
+ map.addLayer(spec, beforeId);
290
+ prevRef.current = {
291
+ paint: layerSpec.paint ?? void 0,
292
+ layout: layerSpec.layout ?? void 0,
293
+ filter: layerSpec.filter,
294
+ minzoom: layerSpec.minzoom,
295
+ maxzoom: layerSpec.maxzoom,
296
+ beforeId
297
+ };
298
+ return () => {
299
+ try {
300
+ if (map.getLayer(layerSpec.id)) {
301
+ map.removeLayer(layerSpec.id);
302
+ }
303
+ } catch {
304
+ }
305
+ };
306
+ }, [map, layerSpec.id]);
307
+ (0, import_react5.useEffect)(() => {
308
+ try {
309
+ if (!map.getLayer(layerSpec.id)) return;
310
+ const prev = prevRef.current;
311
+ const id = layerSpec.id;
312
+ const nextPaint = layerSpec.paint ?? {};
313
+ const prevPaint = prev.paint ?? {};
314
+ if (!shallowEqual(prevPaint, nextPaint)) {
315
+ const keys = /* @__PURE__ */ new Set([
316
+ ...Object.keys(prevPaint),
317
+ ...Object.keys(nextPaint)
318
+ ]);
319
+ for (const k of keys) {
320
+ if (prevPaint[k] !== nextPaint[k]) {
321
+ map.setPaintProperty(id, k, nextPaint[k]);
322
+ }
323
+ }
324
+ }
325
+ const nextLayout = layerSpec.layout ?? {};
326
+ const prevLayout = prev.layout ?? {};
327
+ if (!shallowEqual(prevLayout, nextLayout)) {
328
+ const keys = /* @__PURE__ */ new Set([
329
+ ...Object.keys(prevLayout),
330
+ ...Object.keys(nextLayout)
331
+ ]);
332
+ for (const k of keys) {
333
+ if (prevLayout[k] !== nextLayout[k]) {
334
+ map.setLayoutProperty(id, k, nextLayout[k]);
335
+ }
336
+ }
337
+ }
338
+ const nextFilter = layerSpec.filter;
339
+ if (!deepEqual(prev.filter, nextFilter)) {
340
+ map.setFilter(id, nextFilter ?? null);
341
+ }
342
+ const nextMin = layerSpec.minzoom;
343
+ const nextMax = layerSpec.maxzoom;
344
+ if (prev.minzoom !== nextMin || prev.maxzoom !== nextMax) {
345
+ map.setLayerZoomRange(id, nextMin ?? 0, nextMax ?? 24);
346
+ }
347
+ if (prev.beforeId !== beforeId) {
348
+ map.moveLayer(id, beforeId);
349
+ }
350
+ prevRef.current = {
351
+ paint: nextPaint,
352
+ layout: nextLayout,
353
+ filter: nextFilter,
354
+ minzoom: nextMin,
355
+ maxzoom: nextMax,
356
+ beforeId
357
+ };
358
+ } catch {
359
+ }
360
+ });
361
+ return null;
362
+ }
363
+
364
+ // src/components/map.tsx
365
+ var import_maps_core = require("@geolonia/maps-core");
366
+ var import_react6 = require("react");
367
+ var import_jsx_runtime = require("react/jsx-runtime");
368
+ var HANDLER_NAMES = {
369
+ onClick: "click",
370
+ onDblClick: "dblclick",
371
+ onMouseDown: "mousedown",
372
+ onMouseUp: "mouseup",
373
+ onMouseMove: "mousemove",
374
+ onMouseEnter: "mouseenter",
375
+ onMouseLeave: "mouseleave",
376
+ onMouseOver: "mouseover",
377
+ onMouseOut: "mouseout",
378
+ onContextMenu: "contextmenu",
379
+ onWheel: "wheel",
380
+ onTouchStart: "touchstart",
381
+ onTouchEnd: "touchend",
382
+ onTouchMove: "touchmove",
383
+ onTouchCancel: "touchcancel",
384
+ onMoveStart: "movestart",
385
+ onMove: "move",
386
+ onMoveEnd: "moveend",
387
+ onZoomStart: "zoomstart",
388
+ onZoom: "zoom",
389
+ onZoomEnd: "zoomend",
390
+ onRotateStart: "rotatestart",
391
+ onRotate: "rotate",
392
+ onRotateEnd: "rotateend",
393
+ onPitchStart: "pitchstart",
394
+ onPitch: "pitch",
395
+ onPitchEnd: "pitchend",
396
+ onDragStart: "dragstart",
397
+ onDrag: "drag",
398
+ onDragEnd: "dragend",
399
+ onError: "error",
400
+ onIdle: "idle",
401
+ onRender: "render",
402
+ onResize: "resize",
403
+ onData: "data",
404
+ onStyleData: "styledata",
405
+ onSourceData: "sourcedata"
406
+ };
407
+ var POINTER_HANDLERS = /* @__PURE__ */ new Set([
408
+ "onClick",
409
+ "onDblClick",
410
+ "onMouseDown",
411
+ "onMouseUp",
412
+ "onMouseMove",
413
+ "onMouseOver",
414
+ "onMouseOut",
415
+ "onContextMenu"
416
+ ]);
417
+ function toLngLatTuple(c) {
418
+ if (!c) return void 0;
419
+ if (Array.isArray(c)) return [c[0], c[1]];
420
+ if ("lng" in c) return [c.lng, c.lat];
421
+ if ("lon" in c) return [c.lon, c.lat];
422
+ return void 0;
423
+ }
424
+ var Map = (0, import_react6.forwardRef)((props, ref) => {
425
+ const {
426
+ id,
427
+ className,
428
+ containerStyle,
429
+ interactiveLayerIds,
430
+ cursor,
431
+ transitionType,
432
+ onLoad,
433
+ children,
434
+ ...rest
435
+ } = props;
436
+ const containerRef = (0, import_react6.useRef)(null);
437
+ const [map, setMap] = (0, import_react6.useState)(null);
438
+ const registry = (0, import_react6.useContext)(MapProviderContext);
439
+ const registryRef = (0, import_react6.useRef)(registry);
440
+ registryRef.current = registry;
441
+ const handlersRef = (0, import_react6.useRef)({});
442
+ for (const key of Object.keys(HANDLER_NAMES)) {
443
+ handlersRef.current[key] = props[key];
444
+ }
445
+ const interactiveLayerIdsRef = (0, import_react6.useRef)(
446
+ interactiveLayerIds
447
+ );
448
+ interactiveLayerIdsRef.current = interactiveLayerIds;
449
+ const layerIdsKey = interactiveLayerIds?.join("|") ?? "";
450
+ const hoverStateRef = (0, import_react6.useRef)({ over: false });
451
+ (0, import_react6.useImperativeHandle)(ref, () => map, [map]);
452
+ (0, import_react6.useEffect)(() => {
453
+ if (!map || !id) return;
454
+ const r = registryRef.current;
455
+ if (!r) return;
456
+ r.register(id, map);
457
+ return () => {
458
+ registryRef.current?.unregister(id);
459
+ };
460
+ }, [map, id]);
461
+ (0, import_react6.useEffect)(() => {
462
+ if (!containerRef.current) return;
463
+ const mapOptions = { ...rest };
464
+ for (const key of Object.keys(HANDLER_NAMES)) {
465
+ delete mapOptions[key];
466
+ }
467
+ const instance = new import_maps_core.GeoloniaMap({
468
+ ...mapOptions,
469
+ container: containerRef.current
470
+ });
471
+ instance.on("load", () => {
472
+ onLoad?.(instance);
473
+ });
474
+ const queryFeatures = (point) => {
475
+ const layers = interactiveLayerIdsRef.current;
476
+ if (!layers?.length) return [];
477
+ const queryable = instance;
478
+ return queryable.queryRenderedFeatures?.(point, { layers }) ?? [];
479
+ };
480
+ const wrappers = [];
481
+ for (const [propName, eventName] of Object.entries(HANDLER_NAMES)) {
482
+ if (propName === "onMouseEnter" || propName === "onMouseLeave") {
483
+ const wrapper2 = (e) => {
484
+ if (interactiveLayerIdsRef.current?.length) return;
485
+ handlersRef.current[propName]?.(e);
486
+ };
487
+ instance.on(eventName, wrapper2);
488
+ wrappers.push([eventName, wrapper2]);
489
+ continue;
490
+ }
491
+ const wrapper = (e) => {
492
+ const handler = handlersRef.current[propName];
493
+ if (handler && POINTER_HANDLERS.has(propName) && interactiveLayerIdsRef.current?.length) {
494
+ e.features = queryFeatures(e.point);
495
+ }
496
+ handler?.(e);
497
+ };
498
+ instance.on(eventName, wrapper);
499
+ wrappers.push([eventName, wrapper]);
500
+ }
501
+ const hoverState = hoverStateRef.current;
502
+ const hoverMove = (e) => {
503
+ if (!interactiveLayerIdsRef.current?.length) return;
504
+ const features = e.features ?? queryFeatures(e.point);
505
+ const nowOver = features.length > 0;
506
+ if (nowOver && !hoverState.over) {
507
+ hoverState.over = true;
508
+ handlersRef.current.onMouseEnter?.({ ...e, features });
509
+ } else if (!nowOver && hoverState.over) {
510
+ hoverState.over = false;
511
+ handlersRef.current.onMouseLeave?.({ ...e, features });
512
+ }
513
+ };
514
+ instance.on("mousemove", hoverMove);
515
+ wrappers.push(["mousemove", hoverMove]);
516
+ const hoverCanvasLeave = (e) => {
517
+ if (!interactiveLayerIdsRef.current?.length) return;
518
+ if (hoverState.over) {
519
+ hoverState.over = false;
520
+ handlersRef.current.onMouseLeave?.({ ...e, features: [] });
521
+ }
522
+ };
523
+ instance.on("mouseleave", hoverCanvasLeave);
524
+ wrappers.push(["mouseleave", hoverCanvasLeave]);
525
+ setMap(instance);
526
+ return () => {
527
+ for (const [eventName, wrapper] of wrappers) {
528
+ instance.off(eventName, wrapper);
529
+ }
530
+ instance.remove();
531
+ setMap(null);
532
+ };
533
+ }, []);
534
+ (0, import_react6.useEffect)(() => {
535
+ if (!hoverStateRef.current.over) return;
536
+ hoverStateRef.current.over = false;
537
+ handlersRef.current.onMouseLeave?.({ features: [] });
538
+ }, [layerIdsKey]);
539
+ (0, import_react6.useEffect)(() => {
540
+ if (!map || cursor === void 0) return;
541
+ const canvas = map.getCanvas();
542
+ const prev = canvas.style.cursor;
543
+ canvas.style.cursor = cursor;
544
+ return () => {
545
+ canvas.style.cursor = prev;
546
+ };
547
+ }, [map, cursor]);
548
+ const centerTuple = toLngLatTuple(rest.center);
549
+ const lng = centerTuple?.[0];
550
+ const lat = centerTuple?.[1];
551
+ const zoom = rest.zoom;
552
+ const bearing = rest.bearing;
553
+ const pitch = rest.pitch;
554
+ const minZoom = rest.minZoom;
555
+ const maxZoom = rest.maxZoom;
556
+ const minPitch = rest.minPitch;
557
+ const maxPitch = rest.maxPitch;
558
+ const style = rest.style;
559
+ const prevStyleRef = (0, import_react6.useRef)(style);
560
+ (0, import_react6.useEffect)(() => {
561
+ if (!map) return;
562
+ const m = map;
563
+ const opts = {};
564
+ if (lng !== void 0 && lat !== void 0) {
565
+ const c = m.getCenter();
566
+ if (c.lng !== lng || c.lat !== lat) opts.center = [lng, lat];
567
+ }
568
+ if (zoom !== void 0 && m.getZoom() !== zoom) opts.zoom = zoom;
569
+ if (bearing !== void 0 && m.getBearing() !== bearing) {
570
+ opts.bearing = bearing;
571
+ }
572
+ if (pitch !== void 0 && m.getPitch() !== pitch) opts.pitch = pitch;
573
+ if (Object.keys(opts).length === 0) return;
574
+ if (transitionType === "jump") m.jumpTo(opts);
575
+ else m.flyTo(opts);
576
+ }, [map, lng, lat, zoom, bearing, pitch, transitionType]);
577
+ (0, import_react6.useEffect)(() => {
578
+ if (!map) return;
579
+ map.setMinZoom(
580
+ minZoom ?? null
581
+ );
582
+ }, [map, minZoom]);
583
+ (0, import_react6.useEffect)(() => {
584
+ if (!map) return;
585
+ map.setMaxZoom(
586
+ maxZoom ?? null
587
+ );
588
+ }, [map, maxZoom]);
589
+ (0, import_react6.useEffect)(() => {
590
+ if (!map) return;
591
+ map.setMinPitch(
592
+ minPitch ?? null
593
+ );
594
+ }, [map, minPitch]);
595
+ (0, import_react6.useEffect)(() => {
596
+ if (!map) return;
597
+ map.setMaxPitch(
598
+ maxPitch ?? null
599
+ );
600
+ }, [map, maxPitch]);
601
+ (0, import_react6.useEffect)(() => {
602
+ if (!map) return;
603
+ if (!style) return;
604
+ if (prevStyleRef.current === style) return;
605
+ prevStyleRef.current = style;
606
+ map.setStyle(style);
607
+ }, [map, style]);
608
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref: containerRef, className, style: containerStyle, children: map && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MapContext.Provider, { value: map, children }) });
609
+ });
610
+ Map.displayName = "Map";
611
+
612
+ // src/components/map-provider.tsx
613
+ var import_react7 = require("react");
614
+ var import_jsx_runtime2 = require("react/jsx-runtime");
615
+ function MapProvider({ children }) {
616
+ const [maps, setMaps] = (0, import_react7.useState)({});
617
+ const register = (0, import_react7.useCallback)((id, map) => {
618
+ setMaps((prev) => {
619
+ if (prev[id] === map) return prev;
620
+ return { ...prev, [id]: map };
621
+ });
622
+ }, []);
623
+ const unregister = (0, import_react7.useCallback)((id) => {
624
+ setMaps((prev) => {
625
+ if (!(id in prev)) return prev;
626
+ const next = { ...prev };
627
+ delete next[id];
628
+ return next;
629
+ });
630
+ }, []);
631
+ const value = (0, import_react7.useMemo)(
632
+ () => ({ maps, register, unregister }),
633
+ [maps, register, unregister]
634
+ );
635
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MapProviderContext.Provider, { value, children });
636
+ }
637
+
638
+ // src/components/marker.tsx
639
+ var import_maps_core2 = require("@geolonia/maps-core");
640
+ var import_maplibre_gl2 = __toESM(require("maplibre-gl"), 1);
641
+ var import_react8 = require("react");
642
+ var import_react_dom2 = require("react-dom");
643
+ function Marker(props) {
644
+ const {
645
+ lat,
646
+ lng,
647
+ color,
648
+ openPopup,
649
+ draggable,
650
+ rotation,
651
+ offset,
652
+ anchor,
653
+ rotationAlignment,
654
+ pitchAlignment,
655
+ className,
656
+ children
657
+ } = props;
658
+ const map = useMap();
659
+ const markerRef = (0, import_react8.useRef)(null);
660
+ const popupEl = (0, import_react8.useRef)(document.createElement("div"));
661
+ const handlersRef = (0, import_react8.useRef)({});
662
+ handlersRef.current = {
663
+ onClick: props.onClick,
664
+ onDragStart: props.onDragStart,
665
+ onDrag: props.onDrag,
666
+ onDragEnd: props.onDragEnd
667
+ };
668
+ (0, import_react8.useEffect)(() => {
669
+ const marker = new import_maps_core2.GeoloniaMarker({
670
+ color,
671
+ draggable,
672
+ rotation,
673
+ offset,
674
+ anchor,
675
+ rotationAlignment,
676
+ pitchAlignment,
677
+ className
678
+ }).setLngLat([lng, lat]).addTo(map);
679
+ if (children) {
680
+ marker.setPopup(
681
+ new import_maplibre_gl2.default.Popup({ offset: [0, -30] }).setDOMContent(
682
+ popupEl.current
683
+ )
684
+ );
685
+ if (openPopup) marker.togglePopup();
686
+ }
687
+ const onClickWrapper = (e) => {
688
+ handlersRef.current.onClick?.(e);
689
+ };
690
+ const onDragStartWrapper = (e) => {
691
+ handlersRef.current.onDragStart?.(e);
692
+ };
693
+ const onDragWrapper = (e) => {
694
+ handlersRef.current.onDrag?.(e);
695
+ };
696
+ const onDragEndWrapper = (e) => {
697
+ handlersRef.current.onDragEnd?.(e);
698
+ };
699
+ const element = marker.getElement();
700
+ element.addEventListener("click", onClickWrapper);
701
+ marker.on("dragstart", onDragStartWrapper);
702
+ marker.on("drag", onDragWrapper);
703
+ marker.on("dragend", onDragEndWrapper);
704
+ markerRef.current = marker;
705
+ return () => {
706
+ element.removeEventListener("click", onClickWrapper);
707
+ marker.off("dragstart", onDragStartWrapper);
708
+ marker.off("drag", onDragWrapper);
709
+ marker.off("dragend", onDragEndWrapper);
710
+ marker.remove();
711
+ };
712
+ }, [map, color, anchor, className]);
713
+ (0, import_react8.useEffect)(() => {
714
+ markerRef.current?.setLngLat([lng, lat]);
715
+ }, [lat, lng]);
716
+ (0, import_react8.useEffect)(() => {
717
+ if (draggable === void 0) return;
718
+ markerRef.current?.setDraggable(draggable);
719
+ }, [draggable]);
720
+ (0, import_react8.useEffect)(() => {
721
+ if (rotation === void 0) return;
722
+ markerRef.current?.setRotation(rotation);
723
+ }, [rotation]);
724
+ (0, import_react8.useEffect)(() => {
725
+ if (offset === void 0) return;
726
+ markerRef.current?.setOffset(offset);
727
+ }, [offset]);
728
+ (0, import_react8.useEffect)(() => {
729
+ if (rotationAlignment === void 0) return;
730
+ markerRef.current?.setRotationAlignment(rotationAlignment);
731
+ }, [rotationAlignment]);
732
+ (0, import_react8.useEffect)(() => {
733
+ if (pitchAlignment === void 0) return;
734
+ markerRef.current?.setPitchAlignment(pitchAlignment);
735
+ }, [pitchAlignment]);
736
+ (0, import_react8.useEffect)(() => {
737
+ const marker = markerRef.current;
738
+ const popup = marker?.getPopup();
739
+ if (!popup) return;
740
+ const isOpen = popup.isOpen();
741
+ if (!!openPopup !== isOpen) marker?.togglePopup();
742
+ }, [openPopup]);
743
+ return children ? (0, import_react_dom2.createPortal)(children, popupEl.current) : null;
744
+ }
745
+
746
+ // src/components/popup.tsx
747
+ var import_maplibre_gl3 = __toESM(require("maplibre-gl"), 1);
748
+ var import_react9 = require("react");
749
+ var import_react_dom3 = require("react-dom");
750
+ function defined2(obj) {
751
+ const out = {};
752
+ for (const k of Object.keys(obj)) {
753
+ if (obj[k] !== void 0) out[k] = obj[k];
754
+ }
755
+ return out;
756
+ }
757
+ function Popup(props) {
758
+ const {
759
+ lat,
760
+ lng,
761
+ anchor,
762
+ offset,
763
+ closeButton,
764
+ closeOnClick,
765
+ closeOnMove,
766
+ maxWidth,
767
+ className,
768
+ children,
769
+ onClose
770
+ } = props;
771
+ const map = useMap();
772
+ const containerEl = (0, import_react9.useRef)(document.createElement("div"));
773
+ const popupRef = (0, import_react9.useRef)(null);
774
+ const onCloseRef = (0, import_react9.useRef)(onClose);
775
+ onCloseRef.current = onClose;
776
+ (0, import_react9.useEffect)(() => {
777
+ const popup = new import_maplibre_gl3.default.Popup(
778
+ defined2({
779
+ anchor,
780
+ offset,
781
+ closeButton,
782
+ closeOnClick,
783
+ closeOnMove,
784
+ maxWidth,
785
+ className
786
+ })
787
+ ).setLngLat([lng, lat]).setDOMContent(containerEl.current).addTo(map);
788
+ popup.on("close", () => onCloseRef.current?.());
789
+ popupRef.current = popup;
790
+ return () => {
791
+ popup.remove();
792
+ };
793
+ }, [map, anchor, closeButton, closeOnClick, closeOnMove]);
794
+ (0, import_react9.useEffect)(() => {
795
+ popupRef.current?.setLngLat([lng, lat]);
796
+ }, [lat, lng]);
797
+ (0, import_react9.useEffect)(() => {
798
+ popupRef.current?.setOffset(offset);
799
+ }, [offset]);
800
+ (0, import_react9.useEffect)(() => {
801
+ popupRef.current?.setMaxWidth(maxWidth ?? "240px");
802
+ }, [maxWidth]);
803
+ const prevClassNameRef = (0, import_react9.useRef)(className);
804
+ (0, import_react9.useEffect)(() => {
805
+ const popup = popupRef.current;
806
+ if (!popup) return;
807
+ const prev = prevClassNameRef.current;
808
+ if (prev === className) return;
809
+ if (prev) popup.removeClassName(prev);
810
+ if (className) popup.addClassName(className);
811
+ prevClassNameRef.current = className;
812
+ }, [className]);
813
+ return (0, import_react_dom3.createPortal)(children, containerEl.current);
814
+ }
815
+
816
+ // src/components/source.tsx
817
+ var import_react10 = require("react");
818
+ var import_jsx_runtime3 = require("react/jsx-runtime");
819
+ var SUPPORTED_UPDATE_PROPS = {
820
+ geojson: ["data"],
821
+ image: ["url", "coordinates"],
822
+ // VideoSource exposes setCoordinates but no API to swap source urls at runtime
823
+ video: ["coordinates"],
824
+ canvas: ["coordinates"],
825
+ vector: ["url", "tiles"],
826
+ raster: ["url", "tiles"],
827
+ "raster-dem": ["url", "tiles"]
828
+ };
829
+ function Source({ id, children, ...spec }) {
830
+ const map = useMap();
831
+ const [ready, setReady] = (0, import_react10.useState)(false);
832
+ const prevSpecRef = (0, import_react10.useRef)(null);
833
+ (0, import_react10.useEffect)(() => {
834
+ let cancelled = false;
835
+ function add() {
836
+ if (cancelled || map.getSource(id)) return;
837
+ map.addSource(id, spec);
838
+ prevSpecRef.current = { ...spec };
839
+ if (!cancelled) setReady(true);
840
+ }
841
+ if (map.isStyleLoaded()) {
842
+ add();
843
+ } else {
844
+ map.once("styledata", add);
845
+ }
846
+ return () => {
847
+ cancelled = true;
848
+ map.off("styledata", add);
849
+ setReady(false);
850
+ try {
851
+ if (map.getSource(id)) {
852
+ map.getStyle()?.layers?.filter((l) => "source" in l && l.source === id).forEach((l) => {
853
+ map.removeLayer(l.id);
854
+ });
855
+ map.removeSource(id);
856
+ }
857
+ } catch {
858
+ }
859
+ };
860
+ }, [map, id]);
861
+ (0, import_react10.useEffect)(() => {
862
+ if (!ready) return;
863
+ const source = map.getSource(id);
864
+ if (!source) return;
865
+ const prev = prevSpecRef.current;
866
+ if (!prev) return;
867
+ const supported = SUPPORTED_UPDATE_PROPS[spec.type] ?? [];
868
+ const current = spec;
869
+ if (prev.type !== current.type) {
870
+ console.warn(
871
+ `[<Source id="${id}">] prop "type" changed from "${prev.type}" to "${current.type}" but is not updatable. Source must be re-mounted with a different id to apply.`
872
+ );
873
+ return;
874
+ }
875
+ const allKeys = /* @__PURE__ */ new Set([...Object.keys(prev), ...Object.keys(current)]);
876
+ for (const key of allKeys) {
877
+ if (key === "id" || key === "type") continue;
878
+ if (prev[key] === current[key]) continue;
879
+ if (!supported.includes(key)) {
880
+ console.warn(
881
+ `[<Source id="${id}">] prop "${key}" is not updatable for type "${spec.type}". Source must be re-mounted to apply.`
882
+ );
883
+ }
884
+ }
885
+ switch (spec.type) {
886
+ case "geojson": {
887
+ const data = current.data;
888
+ if (data !== void 0 && data !== prev.data) {
889
+ source.setData(data);
890
+ }
891
+ break;
892
+ }
893
+ case "image": {
894
+ const url = current.url;
895
+ const coordinates = current.coordinates;
896
+ if (url !== void 0 && url !== prev.url) {
897
+ source.updateImage?.({ url });
898
+ }
899
+ if (coordinates !== void 0 && coordinates !== prev.coordinates) {
900
+ source.setCoordinates?.(coordinates);
901
+ }
902
+ break;
903
+ }
904
+ case "video": {
905
+ const coordinates = current.coordinates;
906
+ if (coordinates !== void 0 && coordinates !== prev.coordinates) {
907
+ source.setCoordinates?.(coordinates);
908
+ }
909
+ break;
910
+ }
911
+ case "canvas": {
912
+ const coordinates = current.coordinates;
913
+ if (coordinates !== void 0 && coordinates !== prev.coordinates) {
914
+ source.setCoordinates(coordinates);
915
+ }
916
+ break;
917
+ }
918
+ case "vector":
919
+ case "raster":
920
+ case "raster-dem": {
921
+ const url = current.url;
922
+ const tiles = current.tiles;
923
+ if (url !== void 0 && url !== prev.url) {
924
+ source.setUrl(url);
925
+ }
926
+ if (tiles !== void 0 && tiles !== prev.tiles) {
927
+ source.setTiles(tiles);
928
+ }
929
+ break;
930
+ }
931
+ }
932
+ prevSpecRef.current = { ...current };
933
+ });
934
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(SourceContext.Provider, { value: id, children: ready && children });
935
+ }
936
+ // Annotate the CommonJS export names for ESM import in node:
937
+ 0 && (module.exports = {
938
+ AttributionControl,
939
+ Control,
940
+ FullscreenControl,
941
+ GeolocateControl,
942
+ Layer,
943
+ Map,
944
+ MapContext,
945
+ MapProvider,
946
+ MapProviderContext,
947
+ Marker,
948
+ NavigationControl,
949
+ Popup,
950
+ ScaleControl,
951
+ Source,
952
+ SourceContext,
953
+ useControl,
954
+ useMap,
955
+ useMaps,
956
+ useSourceId
957
+ });