@geolonia/maps-react 0.4.0 → 0.6.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.js CHANGED
@@ -23,6 +23,12 @@ function useMaps() {
23
23
  }
24
24
 
25
25
  // src/components/control.tsx
26
+ function safeRemoveControl(map, ctrl) {
27
+ try {
28
+ if (map.hasControl(ctrl)) map.removeControl(ctrl);
29
+ } catch {
30
+ }
31
+ }
26
32
  function Control({
27
33
  position = "top-right",
28
34
  onAdd,
@@ -52,26 +58,17 @@ function Control({
52
58
  };
53
59
  map.addControl(control, position);
54
60
  return () => {
55
- map.removeControl(control);
61
+ safeRemoveControl(map, control);
56
62
  };
57
63
  }, [map]);
58
64
  return container ? createPortal(children, container) : null;
59
65
  }
60
66
 
61
- // src/components/controls.tsx
67
+ // src/components/controls/attribution-control.tsx
62
68
  import * as maplibregl from "maplibre-gl";
63
69
  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
- }
70
+
71
+ // src/components/controls/defined.ts
75
72
  function defined(obj) {
76
73
  const out = {};
77
74
  for (const k of Object.keys(obj)) {
@@ -79,30 +76,332 @@ function defined(obj) {
79
76
  }
80
77
  return out;
81
78
  }
82
- function safeRemoveControl(map, ctrl) {
83
- try {
84
- map.removeControl(ctrl);
85
- } catch {
86
- }
87
- }
88
- function NavigationControl2({
89
- position = "top-right",
90
- showCompass,
91
- showZoom,
92
- visualizePitch
79
+
80
+ // src/components/controls/attribution-control.tsx
81
+ function AttributionControl2({
82
+ position = "bottom-right",
83
+ compact,
84
+ customAttribution
93
85
  } = {}) {
94
86
  const map = useMap();
95
87
  useEffect2(() => {
96
- const ctrl = new maplibregl.NavigationControl(
97
- defined({ showCompass, showZoom, visualizePitch })
88
+ const ctrl = new maplibregl.AttributionControl(
89
+ defined({ compact, customAttribution })
98
90
  );
99
91
  map.addControl(ctrl, position);
100
92
  return () => {
101
93
  safeRemoveControl(map, ctrl);
102
94
  };
103
- }, [map, position, showCompass, showZoom, visualizePitch]);
95
+ }, [map, position, compact, customAttribution]);
96
+ return null;
97
+ }
98
+
99
+ // src/components/controls/fullscreen-control.tsx
100
+ import * as maplibregl2 from "maplibre-gl";
101
+ import { useEffect as useEffect3 } from "react";
102
+ function FullscreenControl2({
103
+ position = "top-right",
104
+ container
105
+ } = {}) {
106
+ const map = useMap();
107
+ useEffect3(() => {
108
+ const ctrl = new maplibregl2.FullscreenControl(defined({ container }));
109
+ map.addControl(ctrl, position);
110
+ return () => {
111
+ safeRemoveControl(map, ctrl);
112
+ };
113
+ }, [map, position, container]);
104
114
  return null;
105
115
  }
116
+
117
+ // src/components/controls/geocoder-control.tsx
118
+ import { useEffect as useEffect5, useRef as useRef2, useState as useState2 } from "react";
119
+
120
+ // src/geocode.ts
121
+ var GeocodeError = class extends Error {
122
+ constructor(message, code) {
123
+ super(message);
124
+ this.name = "GeocodeError";
125
+ this.code = code;
126
+ }
127
+ };
128
+ var HALF_SPAN = {
129
+ 1: 180 / 2 ** 9,
130
+ 2: 180 / 2 ** 12,
131
+ 3: 180 / 2 ** 15,
132
+ 8: 180 / 2 ** 18
133
+ };
134
+ var TIMEOUT_MS = 1e4;
135
+ function isValidPoint(p) {
136
+ return Number.isFinite(p.lat) && Number.isFinite(p.lng) && Math.abs(p.lat) <= 90 && Math.abs(p.lng) <= 180;
137
+ }
138
+ function cityPoint(r) {
139
+ const lngLat = r.metadata?.city?.point;
140
+ if (!r.city || !lngLat) return void 0;
141
+ return { lat: lngLat[1], lng: lngLat[0], level: 2 };
142
+ }
143
+ function toResult(r, point) {
144
+ const parts = [
145
+ [r.addr ?? "", 8],
146
+ [r.town ?? "", 3],
147
+ [r.city ?? "", 2],
148
+ [r.pref ?? "", 1]
149
+ ];
150
+ const parsed = parts.filter(([name]) => name);
151
+ const included = parsed.filter(([, level]) => level <= point.level);
152
+ const half = HALF_SPAN[point.level] ?? HALF_SPAN[1];
153
+ const parsedLevel = parsed[0]?.[1] ?? 0;
154
+ return {
155
+ center: [point.lng, point.lat],
156
+ bounds: [
157
+ [point.lng - half, point.lat - half],
158
+ [point.lng + half, point.lat + half]
159
+ ],
160
+ level: point.level,
161
+ address: included.map(([name]) => name).reverse().join(""),
162
+ partial: point.level < parsedLevel || (r.other ?? "").trim() !== ""
163
+ };
164
+ }
165
+ async function geocode(address) {
166
+ const input = typeof address === "string" ? address.trim() : "";
167
+ if (!input) {
168
+ throw new GeocodeError(
169
+ "geocode: an address is required.",
170
+ "INVALID_REQUEST"
171
+ );
172
+ }
173
+ let r;
174
+ let timer;
175
+ try {
176
+ const load = async () => {
177
+ const { normalize } = await import("@geolonia/normalize-japanese-addresses");
178
+ return normalize(input);
179
+ };
180
+ const timeout = new Promise((_, reject) => {
181
+ timer = setTimeout(
182
+ () => reject(new Error(`no answer in ${TIMEOUT_MS / 1e3}s`)),
183
+ TIMEOUT_MS
184
+ );
185
+ });
186
+ r = await Promise.race([load(), timeout]);
187
+ } catch (e) {
188
+ throw new GeocodeError(
189
+ `geocode: failed to load address data (${e instanceof Error ? e.message : String(e)}).`,
190
+ "ERROR"
191
+ );
192
+ } finally {
193
+ clearTimeout(timer);
194
+ }
195
+ const point = r.point ?? cityPoint(r);
196
+ if (!point) {
197
+ throw new GeocodeError(
198
+ `geocode: no result for "${input}".`,
199
+ "ZERO_RESULTS"
200
+ );
201
+ }
202
+ if (!isValidPoint(point)) {
203
+ throw new GeocodeError(
204
+ `geocode: the address data has invalid coordinates for "${input}".`,
205
+ "ERROR"
206
+ );
207
+ }
208
+ return toResult(r, point);
209
+ }
210
+
211
+ // src/components/marker.tsx
212
+ import { GeoloniaMarker } from "@geolonia/maps-core";
213
+ import * as maplibregl3 from "maplibre-gl";
214
+ import { useEffect as useEffect4, useRef } from "react";
215
+ import { createPortal as createPortal2 } from "react-dom";
216
+ function Marker(props) {
217
+ const {
218
+ lat,
219
+ lng,
220
+ color,
221
+ openPopup,
222
+ draggable,
223
+ rotation,
224
+ offset,
225
+ anchor,
226
+ rotationAlignment,
227
+ pitchAlignment,
228
+ className,
229
+ children
230
+ } = props;
231
+ const map = useMap();
232
+ const markerRef = useRef(null);
233
+ const popupEl = useRef(document.createElement("div"));
234
+ const handlersRef = useRef({});
235
+ handlersRef.current = {
236
+ onClick: props.onClick,
237
+ onDragStart: props.onDragStart,
238
+ onDrag: props.onDrag,
239
+ onDragEnd: props.onDragEnd
240
+ };
241
+ useEffect4(() => {
242
+ const marker = new GeoloniaMarker({
243
+ color,
244
+ draggable,
245
+ rotation,
246
+ offset,
247
+ anchor,
248
+ rotationAlignment,
249
+ pitchAlignment,
250
+ className
251
+ }).setLngLat([lng, lat]).addTo(map);
252
+ if (children) {
253
+ marker.setPopup(
254
+ new maplibregl3.Popup({ offset: [0, -30] }).setDOMContent(
255
+ popupEl.current
256
+ )
257
+ );
258
+ if (openPopup) marker.togglePopup();
259
+ }
260
+ const onClickWrapper = (e) => {
261
+ handlersRef.current.onClick?.(e);
262
+ };
263
+ const onDragStartWrapper = (e) => {
264
+ handlersRef.current.onDragStart?.(e);
265
+ };
266
+ const onDragWrapper = (e) => {
267
+ handlersRef.current.onDrag?.(e);
268
+ };
269
+ const onDragEndWrapper = (e) => {
270
+ handlersRef.current.onDragEnd?.(e);
271
+ };
272
+ const element = marker.getElement();
273
+ element.addEventListener("click", onClickWrapper);
274
+ marker.on("dragstart", onDragStartWrapper);
275
+ marker.on("drag", onDragWrapper);
276
+ marker.on("dragend", onDragEndWrapper);
277
+ markerRef.current = marker;
278
+ return () => {
279
+ element.removeEventListener("click", onClickWrapper);
280
+ marker.off("dragstart", onDragStartWrapper);
281
+ marker.off("drag", onDragWrapper);
282
+ marker.off("dragend", onDragEndWrapper);
283
+ marker.remove();
284
+ };
285
+ }, [map, color, anchor, className]);
286
+ useEffect4(() => {
287
+ markerRef.current?.setLngLat([lng, lat]);
288
+ }, [lat, lng]);
289
+ useEffect4(() => {
290
+ if (draggable === void 0) return;
291
+ markerRef.current?.setDraggable(draggable);
292
+ }, [draggable]);
293
+ useEffect4(() => {
294
+ if (rotation === void 0) return;
295
+ markerRef.current?.setRotation(rotation);
296
+ }, [rotation]);
297
+ useEffect4(() => {
298
+ if (offset === void 0) return;
299
+ markerRef.current?.setOffset(offset);
300
+ }, [offset]);
301
+ useEffect4(() => {
302
+ if (rotationAlignment === void 0) return;
303
+ markerRef.current?.setRotationAlignment(rotationAlignment);
304
+ }, [rotationAlignment]);
305
+ useEffect4(() => {
306
+ if (pitchAlignment === void 0) return;
307
+ markerRef.current?.setPitchAlignment(pitchAlignment);
308
+ }, [pitchAlignment]);
309
+ useEffect4(() => {
310
+ const marker = markerRef.current;
311
+ const popup = marker?.getPopup();
312
+ if (!popup) return;
313
+ const isOpen = popup.isOpen();
314
+ if (!!openPopup !== isOpen) marker?.togglePopup();
315
+ }, [openPopup]);
316
+ return children ? createPortal2(children, popupEl.current) : null;
317
+ }
318
+
319
+ // src/components/controls/geocoder-control.tsx
320
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
321
+ var DEFAULT_LABEL = "\u4F4F\u6240\u3092\u5165\u529B";
322
+ function createContainer() {
323
+ const div = document.createElement("div");
324
+ div.className = "maplibregl-ctrl maplibregl-ctrl-group maps-react-geocoder";
325
+ return div;
326
+ }
327
+ function GeocoderControl({
328
+ position = "top-left",
329
+ placeholder = DEFAULT_LABEL,
330
+ marker = true,
331
+ fitBounds = true,
332
+ onResult,
333
+ onError
334
+ } = {}) {
335
+ const map = useMap();
336
+ const [input, setInput] = useState2("");
337
+ const [found, setFound] = useState2(null);
338
+ const [failed, setFailed] = useState2(false);
339
+ const seqRef = useRef2(0);
340
+ const aliveRef = useRef2(true);
341
+ useEffect5(() => {
342
+ aliveRef.current = true;
343
+ return () => {
344
+ aliveRef.current = false;
345
+ };
346
+ }, []);
347
+ const latest = useRef2({ fitBounds, onResult, onError });
348
+ useEffect5(() => {
349
+ latest.current = { fitBounds, onResult, onError };
350
+ });
351
+ async function search(e) {
352
+ e.preventDefault();
353
+ const address = input;
354
+ const seq = ++seqRef.current;
355
+ let result;
356
+ try {
357
+ result = await geocode(address);
358
+ } catch (err) {
359
+ if (!aliveRef.current || seq !== seqRef.current) return;
360
+ setFailed(true);
361
+ latest.current.onError?.(
362
+ err instanceof GeocodeError ? err : new GeocodeError(String(err), "ERROR")
363
+ );
364
+ return;
365
+ }
366
+ if (!aliveRef.current || seq !== seqRef.current) return;
367
+ setFailed(false);
368
+ setFound(result);
369
+ if (latest.current.fitBounds) map.fitBounds(result.bounds);
370
+ latest.current.onResult?.(result, address);
371
+ }
372
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
373
+ /* @__PURE__ */ jsx(Control, { position, onAdd: createContainer, children: /* @__PURE__ */ jsxs(
374
+ "form",
375
+ {
376
+ onSubmit: search,
377
+ style: { display: "flex", gap: 4, padding: 4, background: "#fff" },
378
+ children: [
379
+ /* @__PURE__ */ jsx(
380
+ "input",
381
+ {
382
+ type: "search",
383
+ value: input,
384
+ placeholder,
385
+ "aria-label": placeholder || DEFAULT_LABEL,
386
+ "aria-invalid": failed,
387
+ onChange: (e) => {
388
+ setInput(e.target.value);
389
+ setFailed(false);
390
+ },
391
+ style: { border: "1px solid #ccc", padding: "4px 8px" }
392
+ }
393
+ ),
394
+ /* @__PURE__ */ jsx("button", { type: "submit", children: "\u691C\u7D22" })
395
+ ]
396
+ }
397
+ ) }),
398
+ marker && found && /* @__PURE__ */ jsx(Marker, { lng: found.center[0], lat: found.center[1] })
399
+ ] });
400
+ }
401
+
402
+ // src/components/controls/geolocate-control.tsx
403
+ import * as maplibregl4 from "maplibre-gl";
404
+ import { useEffect as useEffect6 } from "react";
106
405
  function GeolocateControl2({
107
406
  position = "top-right",
108
407
  positionOptions,
@@ -112,8 +411,8 @@ function GeolocateControl2({
112
411
  showUserLocation
113
412
  } = {}) {
114
413
  const map = useMap();
115
- useEffect2(() => {
116
- const ctrl = new maplibregl.GeolocateControl(
414
+ useEffect6(() => {
415
+ const ctrl = new maplibregl4.GeolocateControl(
117
416
  defined({
118
417
  positionOptions,
119
418
  fitBoundsOptions,
@@ -137,55 +436,75 @@ function GeolocateControl2({
137
436
  ]);
138
437
  return null;
139
438
  }
140
- function FullscreenControl2({
141
- position = "top-right",
142
- container
143
- } = {}) {
439
+
440
+ // src/components/controls/globe-control.tsx
441
+ import * as maplibregl5 from "maplibre-gl";
442
+
443
+ // src/components/controls/use-control.ts
444
+ import { useEffect as useEffect7 } from "react";
445
+ function useControl(factory, options) {
144
446
  const map = useMap();
145
- useEffect2(() => {
146
- const ctrl = new maplibregl.FullscreenControl(defined({ container }));
447
+ const position = options?.position;
448
+ useEffect7(() => {
449
+ const ctrl = factory();
147
450
  map.addControl(ctrl, position);
148
451
  return () => {
149
452
  safeRemoveControl(map, ctrl);
150
453
  };
151
- }, [map, position, container]);
454
+ }, [map, position]);
455
+ }
456
+
457
+ // src/components/controls/globe-control.tsx
458
+ function GlobeControl2({
459
+ position = "top-right"
460
+ } = {}) {
461
+ useControl(() => new maplibregl5.GlobeControl(), { position });
152
462
  return null;
153
463
  }
154
- function ScaleControl2({
155
- position = "bottom-left",
156
- maxWidth,
157
- unit
464
+
465
+ // src/components/controls/navigation-control.tsx
466
+ import * as maplibregl6 from "maplibre-gl";
467
+ import { useEffect as useEffect8 } from "react";
468
+ function NavigationControl2({
469
+ position = "top-right",
470
+ showCompass,
471
+ showZoom,
472
+ visualizePitch
158
473
  } = {}) {
159
474
  const map = useMap();
160
- useEffect2(() => {
161
- const ctrl = new maplibregl.ScaleControl(defined({ maxWidth, unit }));
475
+ useEffect8(() => {
476
+ const ctrl = new maplibregl6.NavigationControl(
477
+ defined({ showCompass, showZoom, visualizePitch })
478
+ );
162
479
  map.addControl(ctrl, position);
163
480
  return () => {
164
481
  safeRemoveControl(map, ctrl);
165
482
  };
166
- }, [map, position, maxWidth, unit]);
483
+ }, [map, position, showCompass, showZoom, visualizePitch]);
167
484
  return null;
168
485
  }
169
- function AttributionControl2({
170
- position = "bottom-right",
171
- compact,
172
- customAttribution
486
+
487
+ // src/components/controls/scale-control.tsx
488
+ import * as maplibregl7 from "maplibre-gl";
489
+ import { useEffect as useEffect9 } from "react";
490
+ function ScaleControl2({
491
+ position = "bottom-left",
492
+ maxWidth,
493
+ unit
173
494
  } = {}) {
174
495
  const map = useMap();
175
- useEffect2(() => {
176
- const ctrl = new maplibregl.AttributionControl(
177
- defined({ compact, customAttribution })
178
- );
496
+ useEffect9(() => {
497
+ const ctrl = new maplibregl7.ScaleControl(defined({ maxWidth, unit }));
179
498
  map.addControl(ctrl, position);
180
499
  return () => {
181
500
  safeRemoveControl(map, ctrl);
182
501
  };
183
- }, [map, position, compact, customAttribution]);
502
+ }, [map, position, maxWidth, unit]);
184
503
  return null;
185
504
  }
186
505
 
187
506
  // src/components/layer.tsx
188
- import { useEffect as useEffect3, useRef } from "react";
507
+ import { useEffect as useEffect10, useRef as useRef3 } from "react";
189
508
 
190
509
  // src/source-context.ts
191
510
  import { createContext as createContext2, useContext as useContext2 } from "react";
@@ -230,8 +549,19 @@ function Layer({ beforeId, ...layerSpec }) {
230
549
  const map = useMap();
231
550
  const ctxSourceId = useSourceId();
232
551
  const spec = ctxSourceId && !("source" in layerSpec && layerSpec.source) ? { ...layerSpec, source: ctxSourceId } : layerSpec;
233
- const prevRef = useRef({});
234
- useEffect3(() => {
552
+ if (spec.type !== "background" && !("source" in spec && spec.source)) {
553
+ throw new Error(
554
+ `<Layer id="${layerSpec.id}"> needs a source: put it inside <Source>, or pass the source prop.`
555
+ );
556
+ }
557
+ const specRef = useRef3(spec);
558
+ specRef.current = spec;
559
+ const beforeIdRef = useRef3(beforeId);
560
+ beforeIdRef.current = beforeId;
561
+ const insideSourceRef = useRef3(false);
562
+ insideSourceRef.current = Boolean(ctxSourceId);
563
+ const prevRef = useRef3({});
564
+ useEffect10(() => {
235
565
  map.addLayer(spec, beforeId);
236
566
  prevRef.current = {
237
567
  paint: layerSpec.paint ?? void 0,
@@ -250,7 +580,37 @@ function Layer({ beforeId, ...layerSpec }) {
250
580
  }
251
581
  };
252
582
  }, [map, layerSpec.id]);
253
- useEffect3(() => {
583
+ useEffect10(() => {
584
+ const id = layerSpec.id;
585
+ function restore() {
586
+ if (insideSourceRef.current || !map.isStyleLoaded()) return;
587
+ const current = specRef.current;
588
+ try {
589
+ if (map.getLayer(id)) return;
590
+ if (current.source && !map.getSource(current.source)) return;
591
+ const before = beforeIdRef.current;
592
+ map.addLayer(
593
+ current,
594
+ before && map.getLayer(before) ? before : void 0
595
+ );
596
+ prevRef.current = {
597
+ paint: current.paint ?? void 0,
598
+ layout: current.layout ?? void 0,
599
+ filter: current.filter,
600
+ minzoom: current.minzoom,
601
+ maxzoom: current.maxzoom,
602
+ beforeId: beforeIdRef.current
603
+ };
604
+ } catch {
605
+ }
606
+ }
607
+ const events = ["styledata", "style.load", "idle"];
608
+ for (const event of events) map.on(event, restore);
609
+ return () => {
610
+ for (const event of events) map.off(event, restore);
611
+ };
612
+ }, [map, layerSpec.id]);
613
+ useEffect10(() => {
254
614
  try {
255
615
  if (!map.getLayer(layerSpec.id)) return;
256
616
  const prev = prevRef.current;
@@ -316,12 +676,12 @@ import { MapMouseEvent } from "maplibre-gl";
316
676
  import {
317
677
  forwardRef,
318
678
  useContext as useContext3,
319
- useEffect as useEffect4,
679
+ useEffect as useEffect11,
320
680
  useImperativeHandle,
321
- useRef as useRef2,
322
- useState as useState2
681
+ useRef as useRef4,
682
+ useState as useState3
323
683
  } from "react";
324
- import { jsx } from "react/jsx-runtime";
684
+ import { jsx as jsx2 } from "react/jsx-runtime";
325
685
  var DEFAULT_CENTER = [0, 0];
326
686
  var DEFAULT_ZOOM = 0;
327
687
  var HANDLER_NAMES = {
@@ -392,23 +752,23 @@ var Map = forwardRef((props, ref) => {
392
752
  children,
393
753
  ...rest
394
754
  } = props;
395
- const containerRef = useRef2(null);
396
- const [map, setMap] = useState2(null);
755
+ const containerRef = useRef4(null);
756
+ const [map, setMap] = useState3(null);
397
757
  const registry = useContext3(MapProviderContext);
398
- const registryRef = useRef2(registry);
758
+ const registryRef = useRef4(registry);
399
759
  registryRef.current = registry;
400
- const handlersRef = useRef2({});
760
+ const handlersRef = useRef4({});
401
761
  for (const key of Object.keys(HANDLER_NAMES)) {
402
762
  handlersRef.current[key] = props[key];
403
763
  }
404
- const interactiveLayerIdsRef = useRef2(
764
+ const interactiveLayerIdsRef = useRef4(
405
765
  interactiveLayerIds
406
766
  );
407
767
  interactiveLayerIdsRef.current = interactiveLayerIds;
408
768
  const layerIdsKey = interactiveLayerIds?.join("|") ?? "";
409
- const hoverStateRef = useRef2({ over: false });
769
+ const hoverStateRef = useRef4({ over: false });
410
770
  useImperativeHandle(ref, () => map, [map]);
411
- useEffect4(() => {
771
+ useEffect11(() => {
412
772
  if (!map || !id) return;
413
773
  const r = registryRef.current;
414
774
  if (!r) return;
@@ -417,7 +777,7 @@ var Map = forwardRef((props, ref) => {
417
777
  registryRef.current?.unregister(id);
418
778
  };
419
779
  }, [map, id]);
420
- useEffect4(() => {
780
+ useEffect11(() => {
421
781
  if (!containerRef.current) return;
422
782
  const mapOptions = { ...rest };
423
783
  for (const key of Object.keys(HANDLER_NAMES)) {
@@ -426,8 +786,9 @@ var Map = forwardRef((props, ref) => {
426
786
  if (mapOptions.stage === void 0) {
427
787
  delete mapOptions.stage;
428
788
  }
789
+ const fitsToGeojson = Boolean(mapOptions.geojson) && !mapOptions.center;
429
790
  const instance = new GeoloniaMap({
430
- center: DEFAULT_CENTER,
791
+ ...fitsToGeojson ? {} : { center: DEFAULT_CENTER },
431
792
  zoom: DEFAULT_ZOOM,
432
793
  stage: DEFAULT_STAGE,
433
794
  ...mapOptions,
@@ -505,12 +866,12 @@ var Map = forwardRef((props, ref) => {
505
866
  setMap(null);
506
867
  };
507
868
  }, []);
508
- useEffect4(() => {
869
+ useEffect11(() => {
509
870
  if (!hoverStateRef.current.over) return;
510
871
  hoverStateRef.current.over = false;
511
872
  handlersRef.current.onMouseLeave?.({ features: [] });
512
873
  }, [layerIdsKey]);
513
- useEffect4(() => {
874
+ useEffect11(() => {
514
875
  if (!map || cursor === void 0) return;
515
876
  const canvas = map.getCanvas();
516
877
  const prev = canvas.style.cursor;
@@ -530,8 +891,8 @@ var Map = forwardRef((props, ref) => {
530
891
  const minPitch = rest.minPitch;
531
892
  const maxPitch = rest.maxPitch;
532
893
  const style = rest.style;
533
- const prevStyleRef = useRef2(style);
534
- useEffect4(() => {
894
+ const prevStyleRef = useRef4(style);
895
+ useEffect11(() => {
535
896
  if (!map) return;
536
897
  const m = map;
537
898
  const opts = {};
@@ -548,46 +909,46 @@ var Map = forwardRef((props, ref) => {
548
909
  if (transitionType === "jump") m.jumpTo(opts);
549
910
  else m.flyTo(opts);
550
911
  }, [map, lng, lat, zoom, bearing, pitch, transitionType]);
551
- useEffect4(() => {
912
+ useEffect11(() => {
552
913
  if (!map) return;
553
914
  map.setMinZoom(
554
915
  minZoom ?? null
555
916
  );
556
917
  }, [map, minZoom]);
557
- useEffect4(() => {
918
+ useEffect11(() => {
558
919
  if (!map) return;
559
920
  map.setMaxZoom(
560
921
  maxZoom ?? null
561
922
  );
562
923
  }, [map, maxZoom]);
563
- useEffect4(() => {
924
+ useEffect11(() => {
564
925
  if (!map) return;
565
926
  map.setMinPitch(
566
927
  minPitch ?? null
567
928
  );
568
929
  }, [map, minPitch]);
569
- useEffect4(() => {
930
+ useEffect11(() => {
570
931
  if (!map) return;
571
932
  map.setMaxPitch(
572
933
  maxPitch ?? null
573
934
  );
574
935
  }, [map, maxPitch]);
575
- useEffect4(() => {
936
+ useEffect11(() => {
576
937
  if (!map) return;
577
938
  if (!style) return;
578
939
  if (prevStyleRef.current === style) return;
579
940
  prevStyleRef.current = style;
580
941
  map.setStyle(style);
581
942
  }, [map, style]);
582
- return /* @__PURE__ */ jsx("div", { ref: containerRef, className, style: containerStyle, children: map && /* @__PURE__ */ jsx(MapContext.Provider, { value: map, children }) });
943
+ return /* @__PURE__ */ jsx2("div", { ref: containerRef, className, style: containerStyle, children: map && /* @__PURE__ */ jsx2(MapContext.Provider, { value: map, children }) });
583
944
  });
584
945
  Map.displayName = "Map";
585
946
 
586
947
  // src/components/map-provider.tsx
587
- import { useCallback, useMemo, useState as useState3 } from "react";
588
- import { jsx as jsx2 } from "react/jsx-runtime";
948
+ import { useCallback, useMemo, useState as useState4 } from "react";
949
+ import { jsx as jsx3 } from "react/jsx-runtime";
589
950
  function MapProvider({ children }) {
590
- const [maps, setMaps] = useState3({});
951
+ const [maps, setMaps] = useState4({});
591
952
  const register = useCallback((id, map) => {
592
953
  setMaps((prev) => {
593
954
  if (prev[id] === map) return prev;
@@ -606,120 +967,12 @@ function MapProvider({ children }) {
606
967
  () => ({ maps, register, unregister }),
607
968
  [maps, register, unregister]
608
969
  );
609
- return /* @__PURE__ */ jsx2(MapProviderContext.Provider, { value, children });
610
- }
611
-
612
- // src/components/marker.tsx
613
- import { GeoloniaMarker } from "@geolonia/maps-core";
614
- import * as maplibregl2 from "maplibre-gl";
615
- import { useEffect as useEffect5, useRef as useRef3 } from "react";
616
- import { createPortal as createPortal2 } from "react-dom";
617
- function Marker(props) {
618
- const {
619
- lat,
620
- lng,
621
- color,
622
- openPopup,
623
- draggable,
624
- rotation,
625
- offset,
626
- anchor,
627
- rotationAlignment,
628
- pitchAlignment,
629
- className,
630
- children
631
- } = props;
632
- const map = useMap();
633
- const markerRef = useRef3(null);
634
- const popupEl = useRef3(document.createElement("div"));
635
- const handlersRef = useRef3({});
636
- handlersRef.current = {
637
- onClick: props.onClick,
638
- onDragStart: props.onDragStart,
639
- onDrag: props.onDrag,
640
- onDragEnd: props.onDragEnd
641
- };
642
- useEffect5(() => {
643
- const marker = new GeoloniaMarker({
644
- color,
645
- draggable,
646
- rotation,
647
- offset,
648
- anchor,
649
- rotationAlignment,
650
- pitchAlignment,
651
- className
652
- }).setLngLat([lng, lat]).addTo(map);
653
- if (children) {
654
- marker.setPopup(
655
- new maplibregl2.Popup({ offset: [0, -30] }).setDOMContent(
656
- popupEl.current
657
- )
658
- );
659
- if (openPopup) marker.togglePopup();
660
- }
661
- const onClickWrapper = (e) => {
662
- handlersRef.current.onClick?.(e);
663
- };
664
- const onDragStartWrapper = (e) => {
665
- handlersRef.current.onDragStart?.(e);
666
- };
667
- const onDragWrapper = (e) => {
668
- handlersRef.current.onDrag?.(e);
669
- };
670
- const onDragEndWrapper = (e) => {
671
- handlersRef.current.onDragEnd?.(e);
672
- };
673
- const element = marker.getElement();
674
- element.addEventListener("click", onClickWrapper);
675
- marker.on("dragstart", onDragStartWrapper);
676
- marker.on("drag", onDragWrapper);
677
- marker.on("dragend", onDragEndWrapper);
678
- markerRef.current = marker;
679
- return () => {
680
- element.removeEventListener("click", onClickWrapper);
681
- marker.off("dragstart", onDragStartWrapper);
682
- marker.off("drag", onDragWrapper);
683
- marker.off("dragend", onDragEndWrapper);
684
- marker.remove();
685
- };
686
- }, [map, color, anchor, className]);
687
- useEffect5(() => {
688
- markerRef.current?.setLngLat([lng, lat]);
689
- }, [lat, lng]);
690
- useEffect5(() => {
691
- if (draggable === void 0) return;
692
- markerRef.current?.setDraggable(draggable);
693
- }, [draggable]);
694
- useEffect5(() => {
695
- if (rotation === void 0) return;
696
- markerRef.current?.setRotation(rotation);
697
- }, [rotation]);
698
- useEffect5(() => {
699
- if (offset === void 0) return;
700
- markerRef.current?.setOffset(offset);
701
- }, [offset]);
702
- useEffect5(() => {
703
- if (rotationAlignment === void 0) return;
704
- markerRef.current?.setRotationAlignment(rotationAlignment);
705
- }, [rotationAlignment]);
706
- useEffect5(() => {
707
- if (pitchAlignment === void 0) return;
708
- markerRef.current?.setPitchAlignment(pitchAlignment);
709
- }, [pitchAlignment]);
710
- useEffect5(() => {
711
- const marker = markerRef.current;
712
- const popup = marker?.getPopup();
713
- if (!popup) return;
714
- const isOpen = popup.isOpen();
715
- if (!!openPopup !== isOpen) marker?.togglePopup();
716
- }, [openPopup]);
717
- return children ? createPortal2(children, popupEl.current) : null;
970
+ return /* @__PURE__ */ jsx3(MapProviderContext.Provider, { value, children });
718
971
  }
719
972
 
720
973
  // src/components/popup.tsx
721
- import * as maplibregl3 from "maplibre-gl";
722
- import { useEffect as useEffect6, useRef as useRef4 } from "react";
974
+ import * as maplibregl8 from "maplibre-gl";
975
+ import { useEffect as useEffect12, useRef as useRef5 } from "react";
723
976
  import { createPortal as createPortal3 } from "react-dom";
724
977
  function defined2(obj) {
725
978
  const out = {};
@@ -743,12 +996,17 @@ function Popup3(props) {
743
996
  onClose
744
997
  } = props;
745
998
  const map = useMap();
746
- const containerEl = useRef4(document.createElement("div"));
747
- const popupRef = useRef4(null);
748
- const onCloseRef = useRef4(onClose);
999
+ const containerEl = useRef5(document.createElement("div"));
1000
+ const popupRef = useRef5(null);
1001
+ const onCloseRef = useRef5(onClose);
749
1002
  onCloseRef.current = onClose;
750
- useEffect6(() => {
751
- const popup = new maplibregl3.Popup(
1003
+ useEffect12(() => {
1004
+ let mapRemoving = false;
1005
+ const handleMapRemove = () => {
1006
+ mapRemoving = true;
1007
+ };
1008
+ map.on("remove", handleMapRemove);
1009
+ const popup = new maplibregl8.Popup(
752
1010
  defined2({
753
1011
  anchor,
754
1012
  offset,
@@ -759,23 +1017,28 @@ function Popup3(props) {
759
1017
  className
760
1018
  })
761
1019
  ).setLngLat([lng, lat]).setDOMContent(containerEl.current).addTo(map);
762
- popup.on("close", () => onCloseRef.current?.());
1020
+ const handleClose = () => {
1021
+ if (!mapRemoving) onCloseRef.current?.();
1022
+ };
1023
+ popup.on("close", handleClose);
763
1024
  popupRef.current = popup;
764
1025
  return () => {
765
- popup.remove();
1026
+ map.off("remove", handleMapRemove);
1027
+ popup.off("close", handleClose);
1028
+ if (popup.isOpen()) popup.remove();
766
1029
  };
767
1030
  }, [map, anchor, closeButton, closeOnClick, closeOnMove]);
768
- useEffect6(() => {
1031
+ useEffect12(() => {
769
1032
  popupRef.current?.setLngLat([lng, lat]);
770
1033
  }, [lat, lng]);
771
- useEffect6(() => {
1034
+ useEffect12(() => {
772
1035
  popupRef.current?.setOffset(offset);
773
1036
  }, [offset]);
774
- useEffect6(() => {
1037
+ useEffect12(() => {
775
1038
  popupRef.current?.setMaxWidth(maxWidth ?? "240px");
776
1039
  }, [maxWidth]);
777
- const prevClassNameRef = useRef4(className);
778
- useEffect6(() => {
1040
+ const prevClassNameRef = useRef5(className);
1041
+ useEffect12(() => {
779
1042
  const popup = popupRef.current;
780
1043
  if (!popup) return;
781
1044
  const prev = prevClassNameRef.current;
@@ -787,9 +1050,88 @@ function Popup3(props) {
787
1050
  return createPortal3(children, containerEl.current);
788
1051
  }
789
1052
 
1053
+ // src/components/simple-style.tsx
1054
+ import {
1055
+ SimpleStyle as CoreSimpleStyle
1056
+ } from "@geolonia/maps-core";
1057
+ import { useEffect as useEffect13, useRef as useRef6 } from "react";
1058
+ var DEFAULT_ID = "geolonia-simple-style";
1059
+ function SimpleStyle({
1060
+ geojson,
1061
+ id,
1062
+ cluster,
1063
+ clusterColor,
1064
+ choropleth,
1065
+ heatmap,
1066
+ circle,
1067
+ fitBounds
1068
+ }) {
1069
+ const map = useMap();
1070
+ const geojsonRef = useRef6(geojson);
1071
+ geojsonRef.current = geojson;
1072
+ const fitBoundsRef = useRef6(fitBounds);
1073
+ fitBoundsRef.current = fitBounds;
1074
+ const instanceRef = useRef6(null);
1075
+ const appliedRef = useRef6(geojson);
1076
+ const optionsKey = JSON.stringify([
1077
+ id,
1078
+ cluster,
1079
+ clusterColor,
1080
+ choropleth,
1081
+ heatmap,
1082
+ circle
1083
+ ]);
1084
+ useEffect13(() => {
1085
+ const options = {};
1086
+ const given = { id, cluster, clusterColor, choropleth, heatmap, circle };
1087
+ for (const [key, value] of Object.entries(given)) {
1088
+ if (value !== void 0) options[key] = value;
1089
+ }
1090
+ let cancelled = false;
1091
+ let instance = null;
1092
+ function build() {
1093
+ instance = new CoreSimpleStyle(geojsonRef.current, options);
1094
+ appliedRef.current = geojsonRef.current;
1095
+ instance.addTo(map);
1096
+ instanceRef.current = instance;
1097
+ if (fitBoundsRef.current) instance.fitBounds();
1098
+ }
1099
+ function sync() {
1100
+ if (cancelled || !map.isStyleLoaded()) return;
1101
+ if (!instance) {
1102
+ build();
1103
+ } else if (!map.getSource(options.id ?? DEFAULT_ID)) {
1104
+ instance.remove();
1105
+ build();
1106
+ }
1107
+ }
1108
+ const events = ["styledata", "style.load", "idle"];
1109
+ for (const event of events) map.on(event, sync);
1110
+ sync();
1111
+ return () => {
1112
+ cancelled = true;
1113
+ for (const event of events) map.off(event, sync);
1114
+ instanceRef.current = null;
1115
+ try {
1116
+ instance?.remove();
1117
+ } catch {
1118
+ }
1119
+ };
1120
+ }, [map, optionsKey]);
1121
+ useEffect13(() => {
1122
+ const instance = instanceRef.current;
1123
+ if (!instance || appliedRef.current === geojson) return;
1124
+ if (!map.getSource(id ?? DEFAULT_ID)) return;
1125
+ appliedRef.current = geojson;
1126
+ if (typeof geojson === "string") instance.setGeoJSON(geojson);
1127
+ else instance.updateData(geojson);
1128
+ }, [map, id, geojson]);
1129
+ return null;
1130
+ }
1131
+
790
1132
  // src/components/source.tsx
791
- import { useEffect as useEffect7, useRef as useRef5, useState as useState4 } from "react";
792
- import { jsx as jsx3 } from "react/jsx-runtime";
1133
+ import { Fragment as Fragment2, useEffect as useEffect14, useRef as useRef7, useState as useState5 } from "react";
1134
+ import { jsx as jsx4 } from "react/jsx-runtime";
793
1135
  var SUPPORTED_UPDATE_PROPS = {
794
1136
  geojson: ["data"],
795
1137
  image: ["url", "coordinates"],
@@ -802,16 +1144,40 @@ var SUPPORTED_UPDATE_PROPS = {
802
1144
  };
803
1145
  function Source({ id, children, ...spec }) {
804
1146
  const map = useMap();
805
- const [ready, setReady] = useState4(false);
806
- const prevSpecRef = useRef5(null);
807
- useEffect7(() => {
1147
+ const [ready, setReady] = useState5(false);
1148
+ const [generation, setGeneration] = useState5(0);
1149
+ const prevSpecRef = useRef7(null);
1150
+ const specRef = useRef7(spec);
1151
+ specRef.current = spec;
1152
+ useEffect14(() => {
808
1153
  let cancelled = false;
1154
+ let added = false;
1155
+ let ownSource;
809
1156
  function add() {
810
1157
  if (cancelled || map.getSource(id)) return;
811
1158
  map.addSource(id, spec);
1159
+ added = true;
1160
+ ownSource = map.getSource(id);
812
1161
  prevSpecRef.current = { ...spec };
813
1162
  if (!cancelled) setReady(true);
814
1163
  }
1164
+ function restore() {
1165
+ if (cancelled || !added || !map.isStyleLoaded()) return;
1166
+ const current = map.getSource(id);
1167
+ if (current && current === ownSource) return;
1168
+ if (!current) {
1169
+ try {
1170
+ map.addSource(id, specRef.current);
1171
+ } catch {
1172
+ return;
1173
+ }
1174
+ }
1175
+ ownSource = map.getSource(id);
1176
+ prevSpecRef.current = { ...specRef.current };
1177
+ setGeneration((g) => g + 1);
1178
+ }
1179
+ const restoreEvents = ["styledata", "style.load", "idle"];
1180
+ for (const event of restoreEvents) map.on(event, restore);
815
1181
  if (map.isStyleLoaded()) {
816
1182
  add();
817
1183
  } else {
@@ -820,6 +1186,7 @@ function Source({ id, children, ...spec }) {
820
1186
  return () => {
821
1187
  cancelled = true;
822
1188
  map.off("styledata", add);
1189
+ for (const event of restoreEvents) map.off(event, restore);
823
1190
  setReady(false);
824
1191
  try {
825
1192
  if (map.getSource(id)) {
@@ -832,7 +1199,7 @@ function Source({ id, children, ...spec }) {
832
1199
  }
833
1200
  };
834
1201
  }, [map, id]);
835
- useEffect7(() => {
1202
+ useEffect14(() => {
836
1203
  if (!ready) return;
837
1204
  const source = map.getSource(id);
838
1205
  if (!source) return;
@@ -905,13 +1272,16 @@ function Source({ id, children, ...spec }) {
905
1272
  }
906
1273
  prevSpecRef.current = { ...current };
907
1274
  });
908
- return /* @__PURE__ */ jsx3(SourceContext.Provider, { value: id, children: ready && children });
1275
+ return /* @__PURE__ */ jsx4(SourceContext.Provider, { value: id, children: /* @__PURE__ */ jsx4(Fragment2, { children: ready && children }, generation) });
909
1276
  }
910
1277
  export {
911
1278
  AttributionControl2 as AttributionControl,
912
1279
  Control,
913
1280
  FullscreenControl2 as FullscreenControl,
1281
+ GeocodeError,
1282
+ GeocoderControl,
914
1283
  GeolocateControl2 as GeolocateControl,
1284
+ GlobeControl2 as GlobeControl,
915
1285
  Layer,
916
1286
  Map,
917
1287
  MapContext,
@@ -921,8 +1291,10 @@ export {
921
1291
  NavigationControl2 as NavigationControl,
922
1292
  Popup3 as Popup,
923
1293
  ScaleControl2 as ScaleControl,
1294
+ SimpleStyle,
924
1295
  Source,
925
1296
  SourceContext,
1297
+ geocode,
926
1298
  useControl,
927
1299
  useMap,
928
1300
  useMaps,