@classic-homes/maps-react 0.4.0 → 0.5.2

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 CHANGED
@@ -773,6 +773,7 @@ var Source = ({
773
773
  const { map, loaded, styleVersion } = useMap();
774
774
  const initialFitDone = React26__namespace.useRef(false);
775
775
  const lastStyleVersionRef = React26__namespace.useRef(styleVersion);
776
+ const appliedDataRef = React26__namespace.useRef(void 0);
776
777
  const fitBoundsParamsRef = React26__namespace.useRef({
777
778
  shouldFitBounds,
778
779
  data,
@@ -926,6 +927,7 @@ var Source = ({
926
927
  };
927
928
  map.once("idle", handleIdle);
928
929
  map.addSource(id, sourceConfig);
930
+ appliedDataRef.current = type === "geojson" ? data : void 0;
929
931
  const source = map.getSource(id);
930
932
  if (source && map.isSourceLoaded(id)) {
931
933
  map.off("sourcedata", handleSourceData);
@@ -992,6 +994,7 @@ var Source = ({
992
994
  try {
993
995
  const newData = typeof data === "string" ? data : data && typeof data === "object" ? data : null;
994
996
  if (!newData) return;
997
+ if (data === appliedDataRef.current) return;
995
998
  const handleSourceData = (e) => {
996
999
  if (cancelled) return;
997
1000
  if (e.sourceId === id && e.isSourceLoaded) {
@@ -1003,6 +1006,7 @@ var Source = ({
1003
1006
  map.on("sourcedata", handleSourceData);
1004
1007
  }
1005
1008
  source.setData(newData);
1009
+ appliedDataRef.current = data;
1006
1010
  return () => {
1007
1011
  cancelled = true;
1008
1012
  if (cluster) {
@@ -1270,6 +1274,7 @@ var Popup = React26__namespace.forwardRef(
1270
1274
  containerRef.current.className = "maplibregl-popup-content-wrapper";
1271
1275
  }
1272
1276
  const [contentReady, setContentReady] = React26__namespace.useState(false);
1277
+ const [popupVersion, setPopupVersion] = React26__namespace.useState(0);
1273
1278
  const contentCallbackRef = React26__namespace.useCallback((node) => {
1274
1279
  if (node && containerRef.current) {
1275
1280
  setContentReady(true);
@@ -1288,6 +1293,7 @@ var Popup = React26__namespace.forwardRef(
1288
1293
  });
1289
1294
  popup.setDOMContent(containerRef.current);
1290
1295
  popupRef.current = popup;
1296
+ setPopupVersion((v) => v + 1);
1291
1297
  popup.on("open", () => {
1292
1298
  onOpenRef.current?.({
1293
1299
  type: "open",
@@ -1322,7 +1328,7 @@ var Popup = React26__namespace.forwardRef(
1322
1328
  } else {
1323
1329
  ref.current = popupRef.current;
1324
1330
  }
1325
- }, [ref]);
1331
+ }, [ref, popupVersion]);
1326
1332
  React26__namespace.useEffect(() => {
1327
1333
  if (popupRef.current) {
1328
1334
  popupRef.current.setLngLat(coordinates);
@@ -1348,7 +1354,7 @@ var Popup = React26__namespace.forwardRef(
1348
1354
  cancelAnimationFrame(rafHandle);
1349
1355
  }
1350
1356
  };
1351
- }, [map, open, coordinates, autoPan, autoPanPadding, contentReady]);
1357
+ }, [map, open, coordinates, autoPan, autoPanPadding, contentReady, popupVersion]);
1352
1358
  if (!containerRef.current) return null;
1353
1359
  return ReactDOM__namespace.createPortal(
1354
1360
  /* @__PURE__ */ jsxRuntime.jsx("div", { ref: contentCallbackRef, children }),
@@ -1538,10 +1544,34 @@ function useLayer({
1538
1544
  } catch {
1539
1545
  }
1540
1546
  }, [map, id, paint, layerAdded]);
1547
+ const lastAppliedLayoutRef = React26__namespace.useRef(void 0);
1541
1548
  React26__namespace.useEffect(() => {
1542
- if (!map || !layerAdded || !filter) return;
1549
+ if (!map || !layerAdded) return;
1543
1550
  if (!map.getLayer(id)) return;
1544
- map.setFilter(id, filter);
1551
+ const next = { ...layout };
1552
+ delete next.visibility;
1553
+ const diff = mapsCore.getChangedPaintProps(lastAppliedLayoutRef.current, next);
1554
+ if (!diff.hasChanges) return;
1555
+ try {
1556
+ for (const [key, value] of Object.entries(diff.changed)) {
1557
+ map.setLayoutProperty(id, key, value);
1558
+ }
1559
+ for (const key of diff.removed) {
1560
+ map.setLayoutProperty(id, key, void 0);
1561
+ }
1562
+ lastAppliedLayoutRef.current = next;
1563
+ } catch {
1564
+ }
1565
+ }, [map, id, layout, layerAdded]);
1566
+ React26__namespace.useEffect(() => {
1567
+ if (!map || !layerAdded) return;
1568
+ if (!map.getLayer(id)) return;
1569
+ map.setLayerZoomRange(id, minZoom ?? 0, maxZoom ?? 24);
1570
+ }, [map, id, minZoom, maxZoom, layerAdded]);
1571
+ React26__namespace.useEffect(() => {
1572
+ if (!map || !layerAdded) return;
1573
+ if (!map.getLayer(id)) return;
1574
+ map.setFilter(id, filter ?? null);
1545
1575
  }, [map, id, filter, layerAdded]);
1546
1576
  React26__namespace.useEffect(() => {
1547
1577
  if (!map || !layerAdded) return;
@@ -2738,7 +2768,7 @@ var BasemapSwitcher = ({
2738
2768
  children: /* @__PURE__ */ jsxRuntime.jsx(
2739
2769
  "div",
2740
2770
  {
2741
- role: "listbox",
2771
+ role: "group",
2742
2772
  "aria-label": "Select basemap",
2743
2773
  className: "grid gap-1.5",
2744
2774
  style: {
@@ -2751,8 +2781,7 @@ var BasemapSwitcher = ({
2751
2781
  "button",
2752
2782
  {
2753
2783
  type: "button",
2754
- role: "option",
2755
- "aria-selected": isActive,
2784
+ "aria-pressed": isActive,
2756
2785
  className: cn(
2757
2786
  "flex flex-col items-center gap-1 p-1 rounded cursor-pointer",
2758
2787
  "hover:bg-muted transition-colors",
@@ -2765,7 +2794,7 @@ var BasemapSwitcher = ({
2765
2794
  "img",
2766
2795
  {
2767
2796
  src: thumbUrl,
2768
- alt: basemap.name,
2797
+ alt: "",
2769
2798
  loading: "lazy",
2770
2799
  className: "rounded object-cover",
2771
2800
  style: {
@@ -2850,6 +2879,15 @@ var LayerManager = ({
2850
2879
  const uniqueId = React26__namespace.useId();
2851
2880
  const managerId = `layer-manager${uniqueId}`;
2852
2881
  const lastLayerIdsRef = React26__namespace.useRef(new Set(layers.map((l) => l.id)));
2882
+ const tilesCacheRef = React26__namespace.useRef(/* @__PURE__ */ new Map());
2883
+ const getTiles = (url) => {
2884
+ let tiles = tilesCacheRef.current.get(url);
2885
+ if (!tiles) {
2886
+ tiles = [url];
2887
+ tilesCacheRef.current.set(url, tiles);
2888
+ }
2889
+ return tiles;
2890
+ };
2853
2891
  const { style, visible, setExpanded } = useControlPosition(containerRef, {
2854
2892
  id: managerId,
2855
2893
  type: "layer-manager",
@@ -3103,8 +3141,7 @@ var LayerManager = ({
3103
3141
  id: sourceId,
3104
3142
  type: layer.source.type === "vector-tiles" ? "vector" : "geojson",
3105
3143
  data: layer.source.type !== "vector-tiles" ? sourceData : void 0,
3106
- url: layer.source.type === "vector-tiles" ? sourceUrl ?? void 0 : void 0,
3107
- tiles: layer.source.type === "vector-tiles" ? [sourceUrl ?? ""] : void 0,
3144
+ tiles: layer.source.type === "vector-tiles" ? getTiles(sourceUrl ?? "") : void 0,
3108
3145
  attribution: layer.source.attribution,
3109
3146
  children: layerComponent
3110
3147
  },
@@ -3181,7 +3218,8 @@ var LayerManager = ({
3181
3218
  type: "button",
3182
3219
  className: "text-muted-foreground hover:text-foreground p-1",
3183
3220
  onClick: () => setExpandedOpacity(expandedOpacity === layer.id ? null : layer.id),
3184
- "aria-label": "Adjust opacity",
3221
+ "aria-label": `Adjust ${layer.name} opacity`,
3222
+ "aria-expanded": expandedOpacity === layer.id,
3185
3223
  children: /* @__PURE__ */ jsxRuntime.jsxs(
3186
3224
  "svg",
3187
3225
  {
@@ -3208,6 +3246,7 @@ var LayerManager = ({
3208
3246
  if (expandedOpacity !== layerId) return null;
3209
3247
  const state = layerStates.get(layerId);
3210
3248
  if (!state) return null;
3249
+ const layerName = layers.find((l) => l.id === layerId)?.name ?? layerId;
3211
3250
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-3 pb-2", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
3212
3251
  /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-xs text-muted-foreground w-12", children: "Opacity" }),
3213
3252
  /* @__PURE__ */ jsxRuntime.jsx(
@@ -3217,6 +3256,8 @@ var LayerManager = ({
3217
3256
  min: "0",
3218
3257
  max: "100",
3219
3258
  value: state.opacity * 100,
3259
+ "aria-label": `${layerName} opacity`,
3260
+ "aria-valuetext": `${Math.round(state.opacity * 100)}%`,
3220
3261
  onChange: (e) => handleOpacityChange(layerId, Number(e.target.value) / 100),
3221
3262
  className: "classic-layer-opacity-slider flex-1"
3222
3263
  }
package/dist/index.js CHANGED
@@ -749,6 +749,7 @@ var Source = ({
749
749
  const { map, loaded, styleVersion } = useMap();
750
750
  const initialFitDone = React26.useRef(false);
751
751
  const lastStyleVersionRef = React26.useRef(styleVersion);
752
+ const appliedDataRef = React26.useRef(void 0);
752
753
  const fitBoundsParamsRef = React26.useRef({
753
754
  shouldFitBounds,
754
755
  data,
@@ -902,6 +903,7 @@ var Source = ({
902
903
  };
903
904
  map.once("idle", handleIdle);
904
905
  map.addSource(id, sourceConfig);
906
+ appliedDataRef.current = type === "geojson" ? data : void 0;
905
907
  const source = map.getSource(id);
906
908
  if (source && map.isSourceLoaded(id)) {
907
909
  map.off("sourcedata", handleSourceData);
@@ -968,6 +970,7 @@ var Source = ({
968
970
  try {
969
971
  const newData = typeof data === "string" ? data : data && typeof data === "object" ? data : null;
970
972
  if (!newData) return;
973
+ if (data === appliedDataRef.current) return;
971
974
  const handleSourceData = (e) => {
972
975
  if (cancelled) return;
973
976
  if (e.sourceId === id && e.isSourceLoaded) {
@@ -979,6 +982,7 @@ var Source = ({
979
982
  map.on("sourcedata", handleSourceData);
980
983
  }
981
984
  source.setData(newData);
985
+ appliedDataRef.current = data;
982
986
  return () => {
983
987
  cancelled = true;
984
988
  if (cluster) {
@@ -1246,6 +1250,7 @@ var Popup = React26.forwardRef(
1246
1250
  containerRef.current.className = "maplibregl-popup-content-wrapper";
1247
1251
  }
1248
1252
  const [contentReady, setContentReady] = React26.useState(false);
1253
+ const [popupVersion, setPopupVersion] = React26.useState(0);
1249
1254
  const contentCallbackRef = React26.useCallback((node) => {
1250
1255
  if (node && containerRef.current) {
1251
1256
  setContentReady(true);
@@ -1264,6 +1269,7 @@ var Popup = React26.forwardRef(
1264
1269
  });
1265
1270
  popup.setDOMContent(containerRef.current);
1266
1271
  popupRef.current = popup;
1272
+ setPopupVersion((v) => v + 1);
1267
1273
  popup.on("open", () => {
1268
1274
  onOpenRef.current?.({
1269
1275
  type: "open",
@@ -1298,7 +1304,7 @@ var Popup = React26.forwardRef(
1298
1304
  } else {
1299
1305
  ref.current = popupRef.current;
1300
1306
  }
1301
- }, [ref]);
1307
+ }, [ref, popupVersion]);
1302
1308
  React26.useEffect(() => {
1303
1309
  if (popupRef.current) {
1304
1310
  popupRef.current.setLngLat(coordinates);
@@ -1324,7 +1330,7 @@ var Popup = React26.forwardRef(
1324
1330
  cancelAnimationFrame(rafHandle);
1325
1331
  }
1326
1332
  };
1327
- }, [map, open, coordinates, autoPan, autoPanPadding, contentReady]);
1333
+ }, [map, open, coordinates, autoPan, autoPanPadding, contentReady, popupVersion]);
1328
1334
  if (!containerRef.current) return null;
1329
1335
  return ReactDOM.createPortal(
1330
1336
  /* @__PURE__ */ jsx("div", { ref: contentCallbackRef, children }),
@@ -1514,10 +1520,34 @@ function useLayer({
1514
1520
  } catch {
1515
1521
  }
1516
1522
  }, [map, id, paint, layerAdded]);
1523
+ const lastAppliedLayoutRef = React26.useRef(void 0);
1517
1524
  React26.useEffect(() => {
1518
- if (!map || !layerAdded || !filter) return;
1525
+ if (!map || !layerAdded) return;
1519
1526
  if (!map.getLayer(id)) return;
1520
- map.setFilter(id, filter);
1527
+ const next = { ...layout };
1528
+ delete next.visibility;
1529
+ const diff = getChangedPaintProps(lastAppliedLayoutRef.current, next);
1530
+ if (!diff.hasChanges) return;
1531
+ try {
1532
+ for (const [key, value] of Object.entries(diff.changed)) {
1533
+ map.setLayoutProperty(id, key, value);
1534
+ }
1535
+ for (const key of diff.removed) {
1536
+ map.setLayoutProperty(id, key, void 0);
1537
+ }
1538
+ lastAppliedLayoutRef.current = next;
1539
+ } catch {
1540
+ }
1541
+ }, [map, id, layout, layerAdded]);
1542
+ React26.useEffect(() => {
1543
+ if (!map || !layerAdded) return;
1544
+ if (!map.getLayer(id)) return;
1545
+ map.setLayerZoomRange(id, minZoom ?? 0, maxZoom ?? 24);
1546
+ }, [map, id, minZoom, maxZoom, layerAdded]);
1547
+ React26.useEffect(() => {
1548
+ if (!map || !layerAdded) return;
1549
+ if (!map.getLayer(id)) return;
1550
+ map.setFilter(id, filter ?? null);
1521
1551
  }, [map, id, filter, layerAdded]);
1522
1552
  React26.useEffect(() => {
1523
1553
  if (!map || !layerAdded) return;
@@ -2714,7 +2744,7 @@ var BasemapSwitcher = ({
2714
2744
  children: /* @__PURE__ */ jsx(
2715
2745
  "div",
2716
2746
  {
2717
- role: "listbox",
2747
+ role: "group",
2718
2748
  "aria-label": "Select basemap",
2719
2749
  className: "grid gap-1.5",
2720
2750
  style: {
@@ -2727,8 +2757,7 @@ var BasemapSwitcher = ({
2727
2757
  "button",
2728
2758
  {
2729
2759
  type: "button",
2730
- role: "option",
2731
- "aria-selected": isActive,
2760
+ "aria-pressed": isActive,
2732
2761
  className: cn(
2733
2762
  "flex flex-col items-center gap-1 p-1 rounded cursor-pointer",
2734
2763
  "hover:bg-muted transition-colors",
@@ -2741,7 +2770,7 @@ var BasemapSwitcher = ({
2741
2770
  "img",
2742
2771
  {
2743
2772
  src: thumbUrl,
2744
- alt: basemap.name,
2773
+ alt: "",
2745
2774
  loading: "lazy",
2746
2775
  className: "rounded object-cover",
2747
2776
  style: {
@@ -2826,6 +2855,15 @@ var LayerManager = ({
2826
2855
  const uniqueId = React26.useId();
2827
2856
  const managerId = `layer-manager${uniqueId}`;
2828
2857
  const lastLayerIdsRef = React26.useRef(new Set(layers.map((l) => l.id)));
2858
+ const tilesCacheRef = React26.useRef(/* @__PURE__ */ new Map());
2859
+ const getTiles = (url) => {
2860
+ let tiles = tilesCacheRef.current.get(url);
2861
+ if (!tiles) {
2862
+ tiles = [url];
2863
+ tilesCacheRef.current.set(url, tiles);
2864
+ }
2865
+ return tiles;
2866
+ };
2829
2867
  const { style, visible, setExpanded } = useControlPosition(containerRef, {
2830
2868
  id: managerId,
2831
2869
  type: "layer-manager",
@@ -3079,8 +3117,7 @@ var LayerManager = ({
3079
3117
  id: sourceId,
3080
3118
  type: layer.source.type === "vector-tiles" ? "vector" : "geojson",
3081
3119
  data: layer.source.type !== "vector-tiles" ? sourceData : void 0,
3082
- url: layer.source.type === "vector-tiles" ? sourceUrl ?? void 0 : void 0,
3083
- tiles: layer.source.type === "vector-tiles" ? [sourceUrl ?? ""] : void 0,
3120
+ tiles: layer.source.type === "vector-tiles" ? getTiles(sourceUrl ?? "") : void 0,
3084
3121
  attribution: layer.source.attribution,
3085
3122
  children: layerComponent
3086
3123
  },
@@ -3157,7 +3194,8 @@ var LayerManager = ({
3157
3194
  type: "button",
3158
3195
  className: "text-muted-foreground hover:text-foreground p-1",
3159
3196
  onClick: () => setExpandedOpacity(expandedOpacity === layer.id ? null : layer.id),
3160
- "aria-label": "Adjust opacity",
3197
+ "aria-label": `Adjust ${layer.name} opacity`,
3198
+ "aria-expanded": expandedOpacity === layer.id,
3161
3199
  children: /* @__PURE__ */ jsxs(
3162
3200
  "svg",
3163
3201
  {
@@ -3184,6 +3222,7 @@ var LayerManager = ({
3184
3222
  if (expandedOpacity !== layerId) return null;
3185
3223
  const state = layerStates.get(layerId);
3186
3224
  if (!state) return null;
3225
+ const layerName = layers.find((l) => l.id === layerId)?.name ?? layerId;
3187
3226
  return /* @__PURE__ */ jsx("div", { className: "px-3 pb-2", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
3188
3227
  /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground w-12", children: "Opacity" }),
3189
3228
  /* @__PURE__ */ jsx(
@@ -3193,6 +3232,8 @@ var LayerManager = ({
3193
3232
  min: "0",
3194
3233
  max: "100",
3195
3234
  value: state.opacity * 100,
3235
+ "aria-label": `${layerName} opacity`,
3236
+ "aria-valuetext": `${Math.round(state.opacity * 100)}%`,
3196
3237
  onChange: (e) => handleOpacityChange(layerId, Number(e.target.value) / 100),
3197
3238
  className: "classic-layer-opacity-slider flex-1"
3198
3239
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/maps-react",
3
- "version": "0.4.0",
3
+ "version": "0.5.2",
4
4
  "description": "React MapLibre components for Classic Theme",
5
5
  "keywords": [
6
6
  "maps",
@@ -74,7 +74,8 @@
74
74
  "react": "^18.2.0",
75
75
  "react-dom": "^18.2.0",
76
76
  "tsup": "^8.0.0",
77
- "typescript": "^5.3.0"
77
+ "typescript": "^5.3.0",
78
+ "vitest": "^4.1.10"
78
79
  },
79
80
  "publishConfig": {
80
81
  "access": "public"