@classic-homes/maps-react 0.6.0 → 0.7.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/README.md CHANGED
@@ -12,6 +12,25 @@ The ESM build imports the MapLibre and component styles itself. The CommonJS
12
12
  build does not (Node cannot `require()` CSS); import them once from
13
13
  `@classic-homes/maps-react/styles.css` when you load the package that way.
14
14
 
15
+ ### MapLibre 6: set the worker URL
16
+
17
+ MapLibre 6 loads its tile worker from a separate file, which it cannot find on
18
+ its own under a bundler. Set the URL once at app startup, before the first map
19
+ renders; without it every map fails with "Worker failed to load".
20
+
21
+ ```ts
22
+ import { setWorkerUrl } from 'maplibre-gl';
23
+ import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url'; // Vite
24
+
25
+ setWorkerUrl(workerUrl);
26
+ ```
27
+
28
+ With webpack: `setWorkerUrl(new URL('maplibre-gl/dist/maplibre-gl-worker.mjs', import.meta.url).toString())`.
29
+ MapLibre 5 bundles its worker and needs none of this.
30
+
31
+ MapLibre 6 is ESM-only, with no `require` entry, so CommonJS consumers stay
32
+ on MapLibre 5.
33
+
15
34
  ## Features
16
35
 
17
36
  - **Declarative Components**: React-friendly API for MapLibre GL
@@ -440,7 +459,7 @@ import type {
440
459
 
441
460
  - `react` >= 18.0.0
442
461
  - `react-dom` >= 18.0.0
443
- - `maplibre-gl` >= 4.0.0 || >= 5.0.0
462
+ - `maplibre-gl` ^5.0.0 || ^6.11.0
444
463
 
445
464
  ## License
446
465
 
package/dist/index.cjs CHANGED
@@ -11,8 +11,6 @@ var utils = require('@classic-homes/maps-core/utils');
11
11
  var classVarianceAuthority = require('class-variance-authority');
12
12
  var themeReact = require('@classic-homes/theme-react');
13
13
 
14
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
-
16
14
  function _interopNamespace(e) {
17
15
  if (e && e.__esModule) return e;
18
16
  var n = Object.create(null);
@@ -32,7 +30,7 @@ function _interopNamespace(e) {
32
30
  }
33
31
 
34
32
  var React6__namespace = /*#__PURE__*/_interopNamespace(React6);
35
- var maplibregl__default = /*#__PURE__*/_interopDefault(maplibregl);
33
+ var maplibregl__namespace = /*#__PURE__*/_interopNamespace(maplibregl);
36
34
  var ReactDOM__namespace = /*#__PURE__*/_interopNamespace(ReactDOM);
37
35
 
38
36
  // src/components/base/MapContainer.tsx
@@ -314,7 +312,7 @@ function getStyleKey(style) {
314
312
  if (!style) return "";
315
313
  return typeof style === "string" ? style : JSON.stringify(style);
316
314
  }
317
- var Map2 = React6__namespace.forwardRef(
315
+ var Map3 = React6__namespace.forwardRef(
318
316
  ({
319
317
  // Base props
320
318
  title,
@@ -444,7 +442,8 @@ var Map2 = React6__namespace.forwardRef(
444
442
  React6__namespace.useEffect(() => {
445
443
  if (!containerRef.current || mapRef.current) return;
446
444
  try {
447
- const map = new maplibregl__default.default.Map({
445
+ mapsCore.warnIfWorkerUrlMissing(maplibregl__namespace);
446
+ const map = new maplibregl__namespace.Map({
448
447
  container: containerRef.current,
449
448
  style: mapStyle,
450
449
  center,
@@ -476,7 +475,7 @@ var Map2 = React6__namespace.forwardRef(
476
475
  mapRef.current = map;
477
476
  if (navigationControl) {
478
477
  map.addControl(
479
- new maplibregl__default.default.NavigationControl({
478
+ new maplibregl__namespace.NavigationControl({
480
479
  showCompass: true,
481
480
  showZoom: true,
482
481
  visualizePitch: true
@@ -496,7 +495,7 @@ var Map2 = React6__namespace.forwardRef(
496
495
  });
497
496
  }
498
497
  if (scaleControl) {
499
- map.addControl(new maplibregl__default.default.ScaleControl({ unit: "metric" }), scaleControlPosition);
498
+ map.addControl(new maplibregl__namespace.ScaleControl({ unit: "metric" }), scaleControlPosition);
500
499
  positioningCoordinator.register({
501
500
  id: "maplibre-scale-control",
502
501
  type: "scale",
@@ -510,7 +509,7 @@ var Map2 = React6__namespace.forwardRef(
510
509
  });
511
510
  }
512
511
  if (fullscreenControl) {
513
- map.addControl(new maplibregl__default.default.FullscreenControl(), fullscreenControlPosition);
512
+ map.addControl(new maplibregl__namespace.FullscreenControl(), fullscreenControlPosition);
514
513
  positioningCoordinator.register({
515
514
  id: "maplibre-fullscreen-control",
516
515
  type: "custom",
@@ -525,7 +524,7 @@ var Map2 = React6__namespace.forwardRef(
525
524
  }
526
525
  if (geolocateControl) {
527
526
  map.addControl(
528
- new maplibregl__default.default.GeolocateControl({
527
+ new maplibregl__namespace.GeolocateControl({
529
528
  positionOptions: { enableHighAccuracy: true },
530
529
  trackUserLocation: true
531
530
  }),
@@ -753,8 +752,54 @@ var Map2 = React6__namespace.forwardRef(
753
752
  );
754
753
  }
755
754
  );
756
- Map2.displayName = "Map";
757
- var SourceContext = React6__namespace.createContext(null);
755
+ Map3.displayName = "Map";
756
+ function createSourceRegistry() {
757
+ const versions = /* @__PURE__ */ new Map();
758
+ const stacking = /* @__PURE__ */ new Map();
759
+ const listeners = /* @__PURE__ */ new Set();
760
+ let counter = 0;
761
+ const notify = () => {
762
+ for (const listener of [...listeners]) listener();
763
+ };
764
+ return {
765
+ version: (id) => versions.get(id) ?? 0,
766
+ added(id) {
767
+ versions.set(id, ++counter);
768
+ notify();
769
+ },
770
+ removed(id, layersAbove) {
771
+ stacking.set(id, layersAbove ?? /* @__PURE__ */ new Map());
772
+ if (versions.delete(id)) notify();
773
+ },
774
+ layersAbove: (sourceId, layerId) => stacking.get(sourceId)?.get(layerId) ?? [],
775
+ subscribe(listener) {
776
+ listeners.add(listener);
777
+ return () => {
778
+ listeners.delete(listener);
779
+ };
780
+ }
781
+ };
782
+ }
783
+ var registries = /* @__PURE__ */ new WeakMap();
784
+ function getSourceRegistry(map) {
785
+ let registry = registries.get(map);
786
+ if (!registry) {
787
+ registry = createSourceRegistry();
788
+ registries.set(map, registry);
789
+ }
790
+ return registry;
791
+ }
792
+ var noopSubscribe = () => () => {
793
+ };
794
+ var zero = () => 0;
795
+ function useSourceVersion(map, sourceId) {
796
+ const registry = map ? getSourceRegistry(map) : null;
797
+ const getSnapshot = React6__namespace.useCallback(
798
+ () => registry ? registry.version(sourceId) : 0,
799
+ [registry, sourceId]
800
+ );
801
+ return React6__namespace.useSyncExternalStore(registry?.subscribe ?? noopSubscribe, getSnapshot, zero);
802
+ }
758
803
  var Source = ({
759
804
  id,
760
805
  type,
@@ -777,7 +822,6 @@ var Source = ({
777
822
  const { map, loaded, styleVersion } = useMap();
778
823
  const initialFitDone = React6__namespace.useRef(false);
779
824
  const appliedDataRef = React6__namespace.useRef(void 0);
780
- const [sourceVersion, setSourceVersion] = React6__namespace.useState(0);
781
825
  const fitBoundsParamsRef = React6__namespace.useRef({
782
826
  shouldFitBounds,
783
827
  data,
@@ -928,7 +972,8 @@ var Source = ({
928
972
  map.once("idle", handleIdle);
929
973
  map.addSource(id, sourceConfig);
930
974
  appliedDataRef.current = type === "geojson" ? data : void 0;
931
- setSourceVersion((v) => v + 1);
975
+ const registry = getSourceRegistry(map);
976
+ registry.added(id);
932
977
  const source = map.getSource(id);
933
978
  if (source && map.isSourceLoaded(id)) {
934
979
  map.off("sourcedata", handleSourceData);
@@ -947,11 +992,14 @@ var Source = ({
947
992
  return () => {
948
993
  cleanupListeners();
949
994
  if (map) {
995
+ const layersAbove = /* @__PURE__ */ new Map();
950
996
  try {
951
997
  const style = map.getStyle();
952
998
  if (style?.layers) {
953
- style.layers.forEach((layer) => {
999
+ const ids = style.layers.map((layer) => layer.id);
1000
+ style.layers.forEach((layer, index) => {
954
1001
  if ("source" in layer && layer.source === id) {
1002
+ layersAbove.set(layer.id, ids.slice(index + 1));
955
1003
  if (map.getLayer(layer.id)) {
956
1004
  map.removeLayer(layer.id);
957
1005
  }
@@ -963,6 +1011,7 @@ var Source = ({
963
1011
  }
964
1012
  } catch {
965
1013
  }
1014
+ registry.removed(id, layersAbove);
966
1015
  }
967
1016
  };
968
1017
  } catch (err) {
@@ -1018,11 +1067,10 @@ var Source = ({
1018
1067
  console.error(`[maps-react] Failed to update source "${id}" data:`, err);
1019
1068
  }
1020
1069
  }, [map, loaded, id, type, data, cluster]);
1021
- const sourceContext = React6__namespace.useMemo(() => ({ id, version: sourceVersion }), [id, sourceVersion]);
1022
- return /* @__PURE__ */ jsxRuntime.jsx(SourceContext.Provider, { value: sourceContext, children });
1070
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
1023
1071
  };
1024
1072
  Source.displayName = "Source";
1025
- var Marker = React6__namespace.forwardRef(
1073
+ var Marker2 = React6__namespace.forwardRef(
1026
1074
  ({
1027
1075
  id,
1028
1076
  coordinates,
@@ -1079,7 +1127,7 @@ var Marker = React6__namespace.forwardRef(
1079
1127
  if (children && elementRef.current) {
1080
1128
  markerOptions.element = elementRef.current;
1081
1129
  }
1082
- const marker = new maplibregl__default.default.Marker(markerOptions).setLngLat(coordinates).addTo(map);
1130
+ const marker = new maplibregl__namespace.Marker(markerOptions).setLngLat(coordinates).addTo(map);
1083
1131
  const el = marker.getElement();
1084
1132
  mapsCore.applyMarkerProtection(el);
1085
1133
  if (children && elementRef.current) {
@@ -1234,7 +1282,7 @@ var Marker = React6__namespace.forwardRef(
1234
1282
  return null;
1235
1283
  }
1236
1284
  );
1237
- Marker.displayName = "Marker";
1285
+ Marker2.displayName = "Marker";
1238
1286
  function useStructuralValue(next) {
1239
1287
  const ref = React6__namespace.useRef(next);
1240
1288
  if (ref.current !== next && JSON.stringify(ref.current) !== JSON.stringify(next)) {
@@ -1242,7 +1290,7 @@ function useStructuralValue(next) {
1242
1290
  }
1243
1291
  return ref.current;
1244
1292
  }
1245
- var Popup = React6__namespace.forwardRef(
1293
+ var Popup2 = React6__namespace.forwardRef(
1246
1294
  ({
1247
1295
  coordinates: coordinatesProp,
1248
1296
  closeButton = true,
@@ -1284,7 +1332,7 @@ var Popup = React6__namespace.forwardRef(
1284
1332
  }, []);
1285
1333
  React6__namespace.useEffect(() => {
1286
1334
  if (!map || !loaded || !containerRef.current) return;
1287
- const popup = new maplibregl__default.default.Popup({
1335
+ const popup = new maplibregl__namespace.Popup({
1288
1336
  closeButton,
1289
1337
  closeOnClick,
1290
1338
  closeOnMove,
@@ -1365,7 +1413,7 @@ var Popup = React6__namespace.forwardRef(
1365
1413
  );
1366
1414
  }
1367
1415
  );
1368
- Popup.displayName = "Popup";
1416
+ Popup2.displayName = "Popup";
1369
1417
  var PopupCard = React6__namespace.forwardRef(
1370
1418
  ({ className, children, ...props }, ref) => {
1371
1419
  return /* @__PURE__ */ jsxRuntime.jsx("div", { ref, className: cn("bg-popover text-popover-foreground", className), ...props, children });
@@ -1452,14 +1500,16 @@ function useLayer({
1452
1500
  hoverThrottleMs = 0
1453
1501
  }) {
1454
1502
  const { map, loaded, styleVersion } = useMap();
1455
- const sourceContext = React6__namespace.useContext(SourceContext);
1456
- const sourceVersion = sourceContext?.id === source ? sourceContext.version : 0;
1503
+ const sourceVersion = useSourceVersion(map, source);
1457
1504
  const [layerAdded, setLayerAdded] = React6__namespace.useState(false);
1458
1505
  const hoveredFeatureId = React6__namespace.useRef(null);
1459
1506
  const lastHoverCallTime = React6__namespace.useRef(0);
1460
1507
  const lastMousePosition = React6__namespace.useRef(null);
1461
1508
  const lastAppliedPaintRef = React6__namespace.useRef(void 0);
1462
1509
  const lastAppliedLayoutRef = React6__namespace.useRef(void 0);
1510
+ const latestRef = React6__namespace.useRef({ visible, minZoom, maxZoom, filter, paint, layout });
1511
+ latestRef.current = { visible, minZoom, maxZoom, filter, paint, layout };
1512
+ const restoreLayerRef = React6__namespace.useRef(null);
1463
1513
  React6__namespace.useEffect(() => {
1464
1514
  hoveredFeatureId.current = null;
1465
1515
  }, [styleVersion]);
@@ -1467,52 +1517,63 @@ function useLayer({
1467
1517
  if (!map || !loaded) return;
1468
1518
  let added = false;
1469
1519
  let timeoutId = null;
1470
- const checkSource = () => {
1520
+ const stopWaiting = () => {
1521
+ if (timeoutId) clearTimeout(timeoutId);
1522
+ timeoutId = null;
1523
+ map.off("sourcedata", checkSource);
1524
+ };
1525
+ function checkSource() {
1471
1526
  if (map.getSource(source)) {
1472
- if (timeoutId) clearTimeout(timeoutId);
1473
- timeoutId = null;
1474
- map.off("sourcedata", checkSource);
1527
+ stopWaiting();
1475
1528
  addLayer();
1476
1529
  }
1477
- };
1478
- function addLayer() {
1530
+ }
1531
+ function addLayer(insertBefore = beforeId) {
1479
1532
  if (map.getLayer(id)) {
1480
1533
  added = true;
1481
1534
  setLayerAdded(true);
1482
1535
  return;
1483
1536
  }
1537
+ const { visible: visible2, minZoom: minZoom2, maxZoom: maxZoom2, filter: filter2, paint: paint2, layout: layout2 } = latestRef.current;
1484
1538
  try {
1485
1539
  const layerConfig = {
1486
1540
  id,
1487
1541
  type,
1488
1542
  source,
1489
- paint,
1543
+ paint: paint2,
1490
1544
  layout: {
1491
- visibility: visible ? "visible" : "none",
1492
- ...layout
1545
+ visibility: visible2 ? "visible" : "none",
1546
+ ...layout2
1493
1547
  }
1494
1548
  };
1495
1549
  if (sourceLayer) {
1496
1550
  layerConfig["source-layer"] = sourceLayer;
1497
1551
  }
1498
- if (minZoom !== void 0) {
1499
- layerConfig.minzoom = minZoom;
1552
+ if (minZoom2 !== void 0) {
1553
+ layerConfig.minzoom = minZoom2;
1500
1554
  }
1501
- if (maxZoom !== void 0) {
1502
- layerConfig.maxzoom = maxZoom;
1555
+ if (maxZoom2 !== void 0) {
1556
+ layerConfig.maxzoom = maxZoom2;
1503
1557
  }
1504
- if (filter) {
1505
- layerConfig.filter = filter;
1558
+ if (filter2) {
1559
+ layerConfig.filter = filter2;
1506
1560
  }
1507
- map.addLayer(layerConfig, beforeId);
1561
+ map.addLayer(layerConfig, insertBefore);
1508
1562
  added = true;
1509
- lastAppliedPaintRef.current = { ...paint };
1510
- lastAppliedLayoutRef.current = withoutVisibility(layout);
1563
+ lastAppliedPaintRef.current = { ...paint2 };
1564
+ lastAppliedLayoutRef.current = withoutVisibility(layout2);
1511
1565
  setLayerAdded(true);
1512
1566
  } catch (err) {
1513
1567
  console.error(`[maps-react] Failed to add layer "${id}":`, err);
1514
1568
  }
1515
1569
  }
1570
+ restoreLayerRef.current = () => {
1571
+ if (map.getLayer(id) || !map.getSource(source)) return;
1572
+ hoveredFeatureId.current = null;
1573
+ stopWaiting();
1574
+ const above = beforeId ? void 0 : getSourceRegistry(map).layersAbove(source, id).find((layerId) => map.getLayer(layerId));
1575
+ addLayer(beforeId ?? above);
1576
+ };
1516
1577
  if (map.getSource(source)) {
1517
1578
  addLayer();
1518
1579
  } else {
@@ -1526,8 +1587,8 @@ function useLayer({
1526
1587
  map.on("sourcedata", checkSource);
1527
1588
  }
1528
1589
  return () => {
1529
- if (timeoutId) clearTimeout(timeoutId);
1530
- map.off("sourcedata", checkSource);
1590
+ restoreLayerRef.current = null;
1591
+ stopWaiting();
1531
1592
  if (added) {
1532
1593
  try {
1533
1594
  if (map.getLayer(id)) {
@@ -1538,7 +1599,10 @@ function useLayer({
1538
1599
  }
1539
1600
  setLayerAdded(false);
1540
1601
  };
1541
- }, [map, loaded, id, source, sourceLayer, type, beforeId, styleVersion, sourceVersion]);
1602
+ }, [map, loaded, id, source, sourceLayer, type, beforeId, styleVersion]);
1603
+ React6__namespace.useEffect(() => {
1604
+ if (sourceVersion) restoreLayerRef.current?.();
1605
+ }, [sourceVersion]);
1542
1606
  React6__namespace.useEffect(() => {
1543
1607
  if (!map || !layerAdded) return;
1544
1608
  if (!map.getLayer(id)) return;
@@ -3840,14 +3904,14 @@ exports.LayerManager = LayerManager;
3840
3904
  exports.Legend = Legend;
3841
3905
  exports.LegendGroup = LegendGroup;
3842
3906
  exports.LineLayer = LineLayer;
3843
- exports.Map = Map2;
3907
+ exports.Map = Map3;
3844
3908
  exports.MapContainer = MapContainer;
3845
3909
  exports.MapContext = MapContext;
3846
3910
  exports.MapEmpty = MapEmpty;
3847
3911
  exports.MapError = MapError;
3848
3912
  exports.MapSkeleton = MapSkeleton;
3849
- exports.Marker = Marker;
3850
- exports.Popup = Popup;
3913
+ exports.Marker = Marker2;
3914
+ exports.Popup = Popup2;
3851
3915
  exports.PopupCard = PopupCard;
3852
3916
  exports.PopupContent = PopupContent;
3853
3917
  exports.PopupFooter = PopupFooter;