@classic-homes/maps-react 0.2.16 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -374,7 +374,17 @@ var Map2 = React26__namespace.forwardRef(
374
374
  const mapRef = React26__namespace.useRef(null);
375
375
  const [mapLoaded, setMapLoaded] = React26__namespace.useState(false);
376
376
  const [mapError, setMapError] = React26__namespace.useState(null);
377
+ const [initAttempt, setInitAttempt] = React26__namespace.useState(0);
378
+ const retryInit = React26__namespace.useCallback(() => {
379
+ setMapError(null);
380
+ setInitAttempt((attempt) => attempt + 1);
381
+ }, []);
377
382
  const [styleVersion, setStyleVersion] = React26__namespace.useState(0);
383
+ const styleVersionRef = React26__namespace.useRef(0);
384
+ const bumpStyleVersion = React26__namespace.useCallback(() => {
385
+ styleVersionRef.current += 1;
386
+ setStyleVersion(styleVersionRef.current);
387
+ }, []);
378
388
  const activeStyleOverrideRef = React26__namespace.useRef(
379
389
  null
380
390
  );
@@ -383,16 +393,19 @@ var Map2 = React26__namespace.forwardRef(
383
393
  positioningCoordinatorRef.current = mapsCore.createPositioningCoordinator();
384
394
  }
385
395
  const positioningCoordinator = positioningCoordinatorRef.current;
386
- const setMapStyle = React26__namespace.useCallback((style) => {
387
- activeStyleOverrideRef.current = style;
388
- const map = mapRef.current;
389
- if (map) {
396
+ const setMapStyle = React26__namespace.useCallback(
397
+ (style) => {
398
+ activeStyleOverrideRef.current = style;
399
+ const map = mapRef.current;
400
+ if (!map) return;
401
+ const versionBefore = styleVersionRef.current;
390
402
  map.setStyle(style);
391
403
  map.once("idle", () => {
392
- setStyleVersion((v) => v + 1);
404
+ if (styleVersionRef.current === versionBefore) bumpStyleVersion();
393
405
  });
394
- }
395
- }, []);
406
+ },
407
+ [bumpStyleVersion]
408
+ );
396
409
  const prefersReducedMotion = useReducedMotion();
397
410
  const { style: themeStyle } = useMapTheme(themePref);
398
411
  const onLoadRef = React26__namespace.useRef(onLoad);
@@ -446,7 +459,16 @@ var Map2 = React26__namespace.forwardRef(
446
459
  touchZoomRotate,
447
460
  keyboard,
448
461
  cooperativeGestures,
449
- attributionControl: attributionControl ? { customAttribution } : false
462
+ attributionControl: attributionControl ? { customAttribution } : false,
463
+ // Frame the bounds from the first frame; `center`/`zoom` alone put
464
+ // the map at its defaults ([0, 0] z2) until the load handler's
465
+ // fitBounds, which then flew in from the other side of the world.
466
+ ...bounds ? {
467
+ bounds,
468
+ fitBoundsOptions: {
469
+ padding: boundsPadding
470
+ }
471
+ } : {}
450
472
  });
451
473
  mapRef.current = map;
452
474
  if (navigationControl) {
@@ -525,11 +547,14 @@ var Map2 = React26__namespace.forwardRef(
525
547
  onErrorRef.current?.({
526
548
  type: "error",
527
549
  message: errorMessage,
528
- error: new Error(errorMessage)
550
+ error: new Error(errorMessage),
551
+ fatal: true
529
552
  });
530
553
  }
531
554
  }, 3e4);
555
+ let hasLoaded = false;
532
556
  map.on("load", () => {
557
+ hasLoaded = true;
533
558
  clearTimeout(initTimeoutId);
534
559
  setMapLoaded(true);
535
560
  if (bounds) {
@@ -542,36 +567,44 @@ var Map2 = React26__namespace.forwardRef(
542
567
  });
543
568
  map.on("error", (e) => {
544
569
  const errorMessage = e.error?.message || "Map error";
545
- const isTransient = errorMessage.includes("non-existing layer") || errorMessage.includes("Cannot filter");
570
+ const isTransient = hasLoaded || errorMessage.includes("non-existing layer") || errorMessage.includes("Cannot filter");
546
571
  if (!isTransient) {
547
572
  clearTimeout(initTimeoutId);
548
573
  setMapError(errorMessage);
549
574
  }
550
- onErrorRef.current?.({ type: "error", message: errorMessage, error: e.error });
575
+ onErrorRef.current?.({
576
+ type: "error",
577
+ message: errorMessage,
578
+ error: e.error,
579
+ fatal: !isTransient
580
+ });
551
581
  });
552
582
  return () => {
553
583
  clearTimeout(initTimeoutId);
554
584
  map.remove();
555
585
  mapRef.current = null;
556
586
  setMapLoaded(false);
587
+ styleInitialized.current = false;
557
588
  };
558
589
  } catch (err) {
559
590
  const errorMessage = err instanceof Error ? err.message : "Failed to initialize map";
560
591
  setMapError(errorMessage);
561
- onErrorRef.current?.({ type: "error", message: errorMessage, error: err });
592
+ onErrorRef.current?.({
593
+ type: "error",
594
+ message: errorMessage,
595
+ error: err,
596
+ fatal: true
597
+ });
562
598
  }
563
- }, []);
599
+ }, [initAttempt]);
564
600
  React26__namespace.useEffect(() => {
565
601
  const map = mapRef.current;
566
602
  if (!map || !mapLoaded) return;
567
- const handleStyleLoad = () => {
568
- setStyleVersion((v) => v + 1);
569
- };
570
- map.on("style.load", handleStyleLoad);
603
+ map.on("style.load", bumpStyleVersion);
571
604
  return () => {
572
- map.off("style.load", handleStyleLoad);
605
+ map.off("style.load", bumpStyleVersion);
573
606
  };
574
- }, [mapLoaded]);
607
+ }, [mapLoaded, bumpStyleVersion]);
575
608
  const styleInitialized = React26__namespace.useRef(false);
576
609
  React26__namespace.useEffect(() => {
577
610
  if (!mapRef.current || !mapLoaded) return;
@@ -706,6 +739,7 @@ var Map2 = React26__namespace.forwardRef(
706
739
  width,
707
740
  loading,
708
741
  error: combinedError,
742
+ onRetry: mapError && !error ? retryInit : void 0,
709
743
  emptyMessage,
710
744
  className,
711
745
  children: /* @__PURE__ */ jsxRuntime.jsxs(MapContext.Provider, { value: contextValue, children: [
@@ -1195,24 +1229,34 @@ var Marker = React26__namespace.forwardRef(
1195
1229
  }
1196
1230
  );
1197
1231
  Marker.displayName = "Marker";
1232
+ function useStructuralValue(next) {
1233
+ const ref = React26__namespace.useRef(next);
1234
+ if (ref.current !== next && JSON.stringify(ref.current) !== JSON.stringify(next)) {
1235
+ ref.current = next;
1236
+ }
1237
+ return ref.current;
1238
+ }
1198
1239
  var Popup = React26__namespace.forwardRef(
1199
1240
  ({
1200
- coordinates,
1241
+ coordinates: coordinatesProp,
1201
1242
  closeButton = true,
1202
1243
  closeOnClick = true,
1203
1244
  closeOnMove = false,
1204
1245
  anchor,
1205
- offset,
1246
+ offset: offsetProp,
1206
1247
  maxWidth = "300px",
1207
1248
  focusAfterOpen = true,
1208
1249
  autoPan = true,
1209
- autoPanPadding = 50,
1250
+ autoPanPadding: autoPanPaddingProp = 50,
1210
1251
  open = true,
1211
1252
  onOpen,
1212
1253
  onClose,
1213
1254
  children
1214
1255
  }, ref) => {
1215
1256
  const { map, loaded } = useMap();
1257
+ const coordinates = useStructuralValue(coordinatesProp);
1258
+ const offset = useStructuralValue(offsetProp);
1259
+ const autoPanPadding = useStructuralValue(autoPanPaddingProp);
1216
1260
  const popupRef = React26__namespace.useRef(null);
1217
1261
  const onOpenRef = React26__namespace.useRef(onOpen);
1218
1262
  onOpenRef.current = onOpen;
package/dist/index.js CHANGED
@@ -350,7 +350,17 @@ var Map2 = React26.forwardRef(
350
350
  const mapRef = React26.useRef(null);
351
351
  const [mapLoaded, setMapLoaded] = React26.useState(false);
352
352
  const [mapError, setMapError] = React26.useState(null);
353
+ const [initAttempt, setInitAttempt] = React26.useState(0);
354
+ const retryInit = React26.useCallback(() => {
355
+ setMapError(null);
356
+ setInitAttempt((attempt) => attempt + 1);
357
+ }, []);
353
358
  const [styleVersion, setStyleVersion] = React26.useState(0);
359
+ const styleVersionRef = React26.useRef(0);
360
+ const bumpStyleVersion = React26.useCallback(() => {
361
+ styleVersionRef.current += 1;
362
+ setStyleVersion(styleVersionRef.current);
363
+ }, []);
354
364
  const activeStyleOverrideRef = React26.useRef(
355
365
  null
356
366
  );
@@ -359,16 +369,19 @@ var Map2 = React26.forwardRef(
359
369
  positioningCoordinatorRef.current = createPositioningCoordinator();
360
370
  }
361
371
  const positioningCoordinator = positioningCoordinatorRef.current;
362
- const setMapStyle = React26.useCallback((style) => {
363
- activeStyleOverrideRef.current = style;
364
- const map = mapRef.current;
365
- if (map) {
372
+ const setMapStyle = React26.useCallback(
373
+ (style) => {
374
+ activeStyleOverrideRef.current = style;
375
+ const map = mapRef.current;
376
+ if (!map) return;
377
+ const versionBefore = styleVersionRef.current;
366
378
  map.setStyle(style);
367
379
  map.once("idle", () => {
368
- setStyleVersion((v) => v + 1);
380
+ if (styleVersionRef.current === versionBefore) bumpStyleVersion();
369
381
  });
370
- }
371
- }, []);
382
+ },
383
+ [bumpStyleVersion]
384
+ );
372
385
  const prefersReducedMotion = useReducedMotion();
373
386
  const { style: themeStyle } = useMapTheme(themePref);
374
387
  const onLoadRef = React26.useRef(onLoad);
@@ -422,7 +435,16 @@ var Map2 = React26.forwardRef(
422
435
  touchZoomRotate,
423
436
  keyboard,
424
437
  cooperativeGestures,
425
- attributionControl: attributionControl ? { customAttribution } : false
438
+ attributionControl: attributionControl ? { customAttribution } : false,
439
+ // Frame the bounds from the first frame; `center`/`zoom` alone put
440
+ // the map at its defaults ([0, 0] z2) until the load handler's
441
+ // fitBounds, which then flew in from the other side of the world.
442
+ ...bounds ? {
443
+ bounds,
444
+ fitBoundsOptions: {
445
+ padding: boundsPadding
446
+ }
447
+ } : {}
426
448
  });
427
449
  mapRef.current = map;
428
450
  if (navigationControl) {
@@ -501,11 +523,14 @@ var Map2 = React26.forwardRef(
501
523
  onErrorRef.current?.({
502
524
  type: "error",
503
525
  message: errorMessage,
504
- error: new Error(errorMessage)
526
+ error: new Error(errorMessage),
527
+ fatal: true
505
528
  });
506
529
  }
507
530
  }, 3e4);
531
+ let hasLoaded = false;
508
532
  map.on("load", () => {
533
+ hasLoaded = true;
509
534
  clearTimeout(initTimeoutId);
510
535
  setMapLoaded(true);
511
536
  if (bounds) {
@@ -518,36 +543,44 @@ var Map2 = React26.forwardRef(
518
543
  });
519
544
  map.on("error", (e) => {
520
545
  const errorMessage = e.error?.message || "Map error";
521
- const isTransient = errorMessage.includes("non-existing layer") || errorMessage.includes("Cannot filter");
546
+ const isTransient = hasLoaded || errorMessage.includes("non-existing layer") || errorMessage.includes("Cannot filter");
522
547
  if (!isTransient) {
523
548
  clearTimeout(initTimeoutId);
524
549
  setMapError(errorMessage);
525
550
  }
526
- onErrorRef.current?.({ type: "error", message: errorMessage, error: e.error });
551
+ onErrorRef.current?.({
552
+ type: "error",
553
+ message: errorMessage,
554
+ error: e.error,
555
+ fatal: !isTransient
556
+ });
527
557
  });
528
558
  return () => {
529
559
  clearTimeout(initTimeoutId);
530
560
  map.remove();
531
561
  mapRef.current = null;
532
562
  setMapLoaded(false);
563
+ styleInitialized.current = false;
533
564
  };
534
565
  } catch (err) {
535
566
  const errorMessage = err instanceof Error ? err.message : "Failed to initialize map";
536
567
  setMapError(errorMessage);
537
- onErrorRef.current?.({ type: "error", message: errorMessage, error: err });
568
+ onErrorRef.current?.({
569
+ type: "error",
570
+ message: errorMessage,
571
+ error: err,
572
+ fatal: true
573
+ });
538
574
  }
539
- }, []);
575
+ }, [initAttempt]);
540
576
  React26.useEffect(() => {
541
577
  const map = mapRef.current;
542
578
  if (!map || !mapLoaded) return;
543
- const handleStyleLoad = () => {
544
- setStyleVersion((v) => v + 1);
545
- };
546
- map.on("style.load", handleStyleLoad);
579
+ map.on("style.load", bumpStyleVersion);
547
580
  return () => {
548
- map.off("style.load", handleStyleLoad);
581
+ map.off("style.load", bumpStyleVersion);
549
582
  };
550
- }, [mapLoaded]);
583
+ }, [mapLoaded, bumpStyleVersion]);
551
584
  const styleInitialized = React26.useRef(false);
552
585
  React26.useEffect(() => {
553
586
  if (!mapRef.current || !mapLoaded) return;
@@ -682,6 +715,7 @@ var Map2 = React26.forwardRef(
682
715
  width,
683
716
  loading,
684
717
  error: combinedError,
718
+ onRetry: mapError && !error ? retryInit : void 0,
685
719
  emptyMessage,
686
720
  className,
687
721
  children: /* @__PURE__ */ jsxs(MapContext.Provider, { value: contextValue, children: [
@@ -1171,24 +1205,34 @@ var Marker = React26.forwardRef(
1171
1205
  }
1172
1206
  );
1173
1207
  Marker.displayName = "Marker";
1208
+ function useStructuralValue(next) {
1209
+ const ref = React26.useRef(next);
1210
+ if (ref.current !== next && JSON.stringify(ref.current) !== JSON.stringify(next)) {
1211
+ ref.current = next;
1212
+ }
1213
+ return ref.current;
1214
+ }
1174
1215
  var Popup = React26.forwardRef(
1175
1216
  ({
1176
- coordinates,
1217
+ coordinates: coordinatesProp,
1177
1218
  closeButton = true,
1178
1219
  closeOnClick = true,
1179
1220
  closeOnMove = false,
1180
1221
  anchor,
1181
- offset,
1222
+ offset: offsetProp,
1182
1223
  maxWidth = "300px",
1183
1224
  focusAfterOpen = true,
1184
1225
  autoPan = true,
1185
- autoPanPadding = 50,
1226
+ autoPanPadding: autoPanPaddingProp = 50,
1186
1227
  open = true,
1187
1228
  onOpen,
1188
1229
  onClose,
1189
1230
  children
1190
1231
  }, ref) => {
1191
1232
  const { map, loaded } = useMap();
1233
+ const coordinates = useStructuralValue(coordinatesProp);
1234
+ const offset = useStructuralValue(offsetProp);
1235
+ const autoPanPadding = useStructuralValue(autoPanPaddingProp);
1192
1236
  const popupRef = React26.useRef(null);
1193
1237
  const onOpenRef = React26.useRef(onOpen);
1194
1238
  onOpenRef.current = onOpen;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/maps-react",
3
- "version": "0.2.16",
3
+ "version": "0.4.0",
4
4
  "description": "React MapLibre components for Classic Theme",
5
5
  "keywords": [
6
6
  "maps",