@geolonia/maps-react 0.0.1 → 0.1.1

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