@yext/visual-editor 2.0.0-beta.12 → 2.0.0-beta.13

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.
Files changed (69) hide show
  1. package/dist/{SectionLibraryVisualEditorProvider-DJezMczO.js → SectionLibraryVisualEditorProvider-64whofF8.js} +3128 -3105
  2. package/dist/cli/assets/directory-locator-source/components/sections/locator/Filters.test.tsx +1 -0
  3. package/dist/cli/assets/directory-locator-source/components/sections/locator/Filters.tsx +9 -8
  4. package/dist/cli/assets/directory-locator-source/components/sections/locator/Locator.tsx +61 -1
  5. package/dist/cli/assets/directory-locator-source/components/sections/locator/LocatorFields.test.ts +66 -0
  6. package/dist/cli/assets/directory-locator-source/components/sections/locator/LocatorRadius.test.ts +316 -34
  7. package/dist/cli/assets/directory-locator-source/components/sections/locator/LocatorResultCard.tsx +4 -1
  8. package/dist/cli/assets/directory-locator-source/components/sections/locator/LocatorWrapper.test.ts +370 -0
  9. package/dist/cli/assets/directory-locator-source/components/sections/locator/LocatorWrapper.tsx +223 -25
  10. package/dist/cli/assets/directory-locator-source/components/sections/locator/Map.tsx +91 -12
  11. package/dist/cli/assets/directory-locator-source/components/sections/locator/Results.tsx +11 -6
  12. package/dist/cli/assets/directory-locator-source/components/sections/locator/distanceUnits.ts +7 -0
  13. package/dist/cli/yextve.js +1 -1
  14. package/dist/{createYextFieldTransforms-B3f0jVg6.d.ts → createYextFieldTransforms-w-U4b5Zf.d.ts} +5 -2
  15. package/dist/index.d.ts +20 -4
  16. package/dist/locales/platform/cs/visual-editor.json +6 -0
  17. package/dist/locales/platform/da/visual-editor.json +6 -0
  18. package/dist/locales/platform/de/visual-editor.json +6 -0
  19. package/dist/locales/platform/en/visual-editor.json +6 -0
  20. package/dist/locales/platform/en-GB/visual-editor.json +6 -0
  21. package/dist/locales/platform/es/visual-editor.json +6 -0
  22. package/dist/locales/platform/et/visual-editor.json +6 -0
  23. package/dist/locales/platform/fi/visual-editor.json +6 -0
  24. package/dist/locales/platform/fr/visual-editor.json +6 -0
  25. package/dist/locales/platform/hr/visual-editor.json +6 -0
  26. package/dist/locales/platform/hu/visual-editor.json +6 -0
  27. package/dist/locales/platform/it/visual-editor.json +6 -0
  28. package/dist/locales/platform/ja/visual-editor.json +6 -0
  29. package/dist/locales/platform/lt/visual-editor.json +6 -0
  30. package/dist/locales/platform/lv/visual-editor.json +6 -0
  31. package/dist/locales/platform/nb/visual-editor.json +6 -0
  32. package/dist/locales/platform/nl/visual-editor.json +6 -0
  33. package/dist/locales/platform/pl/visual-editor.json +6 -0
  34. package/dist/locales/platform/pt/visual-editor.json +6 -0
  35. package/dist/locales/platform/ro/visual-editor.json +6 -0
  36. package/dist/locales/platform/sk/visual-editor.json +6 -0
  37. package/dist/locales/platform/sv/visual-editor.json +6 -0
  38. package/dist/locales/platform/tr/visual-editor.json +6 -0
  39. package/dist/locales/platform/zh/visual-editor.json +6 -0
  40. package/dist/locales/platform/zh-TW/visual-editor.json +6 -0
  41. package/dist/section-library-support.js +55 -55
  42. package/dist/sectionLibrarySupport.d.ts +2 -2
  43. package/dist/{visual-editor-DuxrpEsM.js → visual-editor-B84T0Hz5.js} +67 -61
  44. package/dist/{visual-editor-bjHpuHcP.js → visual-editor-B8eVGZjZ.js} +6 -0
  45. package/dist/{visual-editor-89bNoCOk.js → visual-editor-BTv1Bren.js} +58 -52
  46. package/dist/{visual-editor-BWDv00Sj.js → visual-editor-BWJvQ6QT.js} +100 -94
  47. package/dist/{visual-editor-BPOG2TRh.js → visual-editor-BWk-Ri1i.js} +69 -63
  48. package/dist/{visual-editor-DuH1uy-p.js → visual-editor-BdM7VUac.js} +57 -51
  49. package/dist/{visual-editor-D_Xqs07E.js → visual-editor-BmM2bYhg.js} +92 -86
  50. package/dist/{visual-editor-CiE_sZgp.js → visual-editor-BuRftlWt.js} +79 -73
  51. package/dist/{visual-editor-BEiQqs_Z.js → visual-editor-BxnbtD_z.js} +59 -53
  52. package/dist/{visual-editor-DVI-AF67.js → visual-editor-C4Nretnd.js} +34 -28
  53. package/dist/{visual-editor-BmfXE4Nt.js → visual-editor-CFE4ijeV.js} +36 -30
  54. package/dist/{visual-editor-CJMynoK2.js → visual-editor-CMfDvLU2.js} +49 -43
  55. package/dist/{visual-editor-DSYNp8sQ.js → visual-editor-CauX7_Ur.js} +23 -17
  56. package/dist/{visual-editor-C5aegP-t.js → visual-editor-D5d9meOo.js} +22 -16
  57. package/dist/{visual-editor-dWLAQOr6.js → visual-editor-D9U8TxD-.js} +47 -41
  58. package/dist/{visual-editor-C_SC4gLo.js → visual-editor-DBZwjDQd.js} +43 -37
  59. package/dist/{visual-editor-TsYZAz_J.js → visual-editor-DI6rS2dP.js} +112 -106
  60. package/dist/{visual-editor-8Okvw03s.js → visual-editor-DOioJ5CW.js} +62 -56
  61. package/dist/{visual-editor-BfzO6vuG.js → visual-editor-D_EvXM08.js} +48 -42
  62. package/dist/{visual-editor-yqgcCbY4.js → visual-editor-DguhAL12.js} +6 -0
  63. package/dist/{visual-editor-C7Ne5NW6.js → visual-editor-DmsRSmAI.js} +54 -48
  64. package/dist/{visual-editor-B-9-8VqT.js → visual-editor-DnNNHiLS.js} +49 -43
  65. package/dist/{visual-editor-Crc50YSl.js → visual-editor-DvksDrGT.js} +36 -30
  66. package/dist/{visual-editor-DcNe0Txh.js → visual-editor-XsBMHUSs.js} +62 -56
  67. package/dist/{visual-editor-d-SdfYpR.js → visual-editor-kiT3ocKi.js} +36 -30
  68. package/dist/visual-editor.js +6727 -6388
  69. package/package.json +1 -1
@@ -48,6 +48,12 @@ import { getThemeColorCssValue } from "../../../utils/colors.ts";
48
48
  import { Button } from "../../atoms/button.tsx";
49
49
  import { Body } from "../../atoms/body.tsx";
50
50
  import { Heading } from "../../atoms/heading.tsx";
51
+ import {
52
+ BROWSER_LANGUAGE,
53
+ DISTANCE_UNITS,
54
+ MILE,
55
+ type DistanceUnit,
56
+ } from "./distanceUnits.ts";
51
57
  import {
52
58
  DEFAULT_ENTITY_TYPE,
53
59
  LocatorEntityType,
@@ -85,6 +91,7 @@ import {
85
91
  ResultsCountSummary,
86
92
  RESULTS_LIMIT,
87
93
  SearchState,
94
+ translateDistanceUnit,
88
95
  } from "./Results.tsx";
89
96
 
90
97
  export const INITIAL_LOCATION_KEY = "initialLocation";
@@ -158,6 +165,7 @@ export const LocatorWrapper = (props: WithPuckProps<LocatorProps>) => {
158
165
  };
159
166
 
160
167
  const LocatorInternal = ({
168
+ autoSearchOnMapMove = false,
161
169
  mapStyle,
162
170
  locationStyles,
163
171
  filters: {
@@ -171,6 +179,8 @@ const LocatorInternal = ({
171
179
  defaultSearchRadius,
172
180
  resultCard: resultCardConfigs,
173
181
  distanceDisplay,
182
+ showDistanceUnitToggle,
183
+ defaultDistanceUnit,
174
184
  pageHeading,
175
185
  }: LocatorProps) => {
176
186
  // Adds unified [enable|disable]YextAnalytics to the window for both Pages and Search
@@ -198,13 +208,23 @@ const LocatorInternal = ({
198
208
  const windowWidth = useWindowWidth(previewWindow);
199
209
  const { isMobile } = getViewport(windowWidth);
200
210
  const preferredUnit = getPreferredDistanceUnit(i18n.language);
211
+ const [distanceUnitOverride, setDistanceUnitOverride] =
212
+ React.useState<DistanceUnit | null>(null);
213
+ const configuredDistanceUnit =
214
+ defaultDistanceUnit && defaultDistanceUnit !== BROWSER_LANGUAGE
215
+ ? defaultDistanceUnit
216
+ : preferredUnit;
217
+ const distanceUnit = distanceUnitOverride ?? configuredDistanceUnit;
218
+ React.useEffect(() => setDistanceUnitOverride(null), [defaultDistanceUnit]);
201
219
  // Puck emits 0 when its number field is cleared; only positive radii override Search.
202
220
  const configuredRadiusMeters =
203
221
  defaultSearchRadius?.value && defaultSearchRadius.value > 0
204
222
  ? toMeters(defaultSearchRadius.value, defaultSearchRadius.unit)
205
223
  : undefined;
206
224
  const defaultRadiusMeters =
207
- configuredRadiusMeters ?? toMeters(DEFAULT_RADIUS, preferredUnit);
225
+ configuredRadiusMeters ?? toMeters(DEFAULT_RADIUS, distanceUnit);
226
+ const initialDefaultRadiusMeters =
227
+ configuredRadiusMeters ?? toMeters(DEFAULT_RADIUS, configuredDistanceUnit);
208
228
  const streamDocument = useDocument();
209
229
  const entityTypeSourceMap = getLocatorEntityTypeSourceMap(streamDocument);
210
230
  const entityTypes =
@@ -251,6 +271,11 @@ const LocatorInternal = ({
251
271
  const [showSearchAreaButton, setShowSearchAreaButton] = React.useState(false);
252
272
  const [mapCenter, setMapCenter] = React.useState<Coordinate | undefined>();
253
273
  const [mapRadius, setMapRadius] = React.useState<number | undefined>();
274
+ const mapSearchTimeout = React.useRef<ReturnType<typeof setTimeout>>();
275
+ const cancelMapSearch = React.useCallback((): void => {
276
+ clearTimeout(mapSearchTimeout.current);
277
+ mapSearchTimeout.current = undefined;
278
+ }, []);
254
279
  /** Explicit filter radius selected by the user, in meters */
255
280
  const [selectedDistanceMeters, setSelectedDistanceMeters] = React.useState<
256
281
  number | null
@@ -258,8 +283,16 @@ const LocatorInternal = ({
258
283
  const [selectedDistanceOption, setSelectedDistanceOption] = React.useState<
259
284
  number | null
260
285
  >(null);
286
+ const currentDistanceSelection = React.useRef({
287
+ selectedDistanceOption,
288
+ distanceUnit,
289
+ });
290
+ currentDistanceSelection.current = { selectedDistanceOption, distanceUnit };
291
+ const previousDistanceUnit = React.useRef(distanceUnit);
261
292
  /** Radius of the current location filter before a distance option is applied */
262
293
  const baseFilterRadius = React.useRef<number | null>(null);
294
+ /** Minimum accuracy for a base radius derived from the default unit; null for independent radii. */
295
+ const automaticBaseRadiusAccuracy = React.useRef<number | null>(null);
263
296
 
264
297
  const handleDrag: OnDragHandler = (center, bounds) => {
265
298
  setMapCenter({
@@ -268,6 +301,13 @@ const LocatorInternal = ({
268
301
  });
269
302
  setMapRadius(center.distanceTo(bounds.getNorthEast()));
270
303
  setShowSearchAreaButton(true);
304
+ cancelMapSearch();
305
+ if (autoSearchOnMapMove && mapEnabled) {
306
+ mapSearchTimeout.current = setTimeout((): void => {
307
+ mapSearchTimeout.current = undefined;
308
+ searchMapArea.current();
309
+ }, 500);
310
+ }
271
311
  };
272
312
 
273
313
  const [isOpenNowSelected, setIsOpenNowSelected] = React.useState(false);
@@ -288,7 +328,10 @@ const LocatorInternal = ({
288
328
  const searchActions = useSearchActions();
289
329
  const searchFilters = useSearchState((state) => state.filters);
290
330
 
291
- const handleSearchAreaClick = () => {
331
+ const preserveMapView = React.useRef(false);
332
+
333
+ const handleSearchAreaClick = (preserveView = false): void => {
334
+ cancelMapSearch();
292
335
  if (mapCenter && mapRadius) {
293
336
  searchActions.setOffset(0);
294
337
  const locationFilter: SelectableStaticFilter = {
@@ -307,9 +350,11 @@ const LocatorInternal = ({
307
350
  },
308
351
  };
309
352
  baseFilterRadius.current = mapRadius;
353
+ automaticBaseRadiusAccuracy.current = null;
310
354
  setSelectedDistanceOption(null);
311
355
  setSelectedDistanceMeters(null);
312
356
  searchActions.setStaticFilters([locationFilter, openNowFilter]);
357
+ preserveMapView.current = preserveView;
313
358
  searchActions.executeVerticalQuery();
314
359
  setSearchState("loading");
315
360
  setShowSearchAreaButton(false);
@@ -324,9 +369,18 @@ const LocatorInternal = ({
324
369
  }
325
370
  };
326
371
 
372
+ const searchMapArea = React.useRef((): void => handleSearchAreaClick(true));
373
+ React.useEffect((): void => {
374
+ // Use the current map area and filters when the movement delay ends.
375
+ searchMapArea.current = (): void => handleSearchAreaClick(true);
376
+ });
377
+
327
378
  const handleCurrentLocationClick = (position: GeolocationPosition) => {
379
+ cancelMapSearch();
380
+ setShowSearchAreaButton(false);
328
381
  const { latitude, longitude, accuracy } = position.coords;
329
382
  baseFilterRadius.current = Math.max(accuracy, defaultRadiusMeters);
383
+ automaticBaseRadiusAccuracy.current = accuracy;
330
384
  const radius = Math.max(
331
385
  accuracy,
332
386
  showDistanceOptions && selectedDistanceMeters
@@ -348,6 +402,7 @@ const LocatorInternal = ({
348
402
  );
349
403
  searchActions.setOffset(0);
350
404
  searchActions.setStaticFilters([...nonLocationFilters, locationFilter]);
405
+ preserveMapView.current = false;
351
406
  searchActions.executeVerticalQuery();
352
407
  setSearchState("loading");
353
408
  updateLocationQuery(formatCoordinateQuery(latitude, longitude, radius));
@@ -376,6 +431,8 @@ const LocatorInternal = ({
376
431
  );
377
432
 
378
433
  const handleFilterSelect = (params: OnSelectParams) => {
434
+ cancelMapSearch();
435
+ setShowSearchAreaButton(false);
379
436
  const newDisplayName = params.newDisplayName;
380
437
  const filter = params.newFilter;
381
438
 
@@ -386,6 +443,7 @@ const LocatorInternal = ({
386
443
  nearFilterValue = filter.value as NearFilterValue;
387
444
  baseFilterRadius.current =
388
445
  configuredRadiusMeters ?? nearFilterValue.radius;
446
+ automaticBaseRadiusAccuracy.current = null;
389
447
  const radius =
390
448
  showDistanceOptions && selectedDistanceMeters
391
449
  ? selectedDistanceMeters
@@ -399,6 +457,7 @@ const LocatorInternal = ({
399
457
  }
400
458
  case Matcher.Equals: {
401
459
  baseFilterRadius.current = null;
460
+ automaticBaseRadiusAccuracy.current = null;
402
461
  locationFilter = buildEqualsLocationFilter(filter, newDisplayName);
403
462
  break;
404
463
  }
@@ -409,6 +468,7 @@ const LocatorInternal = ({
409
468
 
410
469
  searchActions.setOffset(0);
411
470
  searchActions.setStaticFilters([locationFilter, openNowFilter]);
471
+ preserveMapView.current = false;
412
472
  searchActions.executeVerticalQuery();
413
473
  setSearchState("loading");
414
474
  updateLocationQuery(newDisplayName);
@@ -443,13 +503,39 @@ const LocatorInternal = ({
443
503
  }, [searchLoading, searchState]);
444
504
 
445
505
  React.useEffect(() => {
506
+ const unitChanged = previousDistanceUnit.current !== distanceUnit;
507
+ previousDistanceUnit.current = distanceUnit;
508
+ if (unitChanged && automaticBaseRadiusAccuracy.current !== null) {
509
+ baseFilterRadius.current = Math.max(
510
+ automaticBaseRadiusAccuracy.current,
511
+ defaultRadiusMeters
512
+ );
513
+ }
446
514
  if (selectedDistanceOption === null) {
447
515
  setSelectedDistanceMeters(null);
448
516
  return;
449
517
  }
450
518
 
451
- setSelectedDistanceMeters(toMeters(selectedDistanceOption, preferredUnit));
452
- }, [preferredUnit, selectedDistanceOption]);
519
+ const radius = toMeters(selectedDistanceOption, distanceUnit);
520
+ setSelectedDistanceMeters(radius);
521
+ if (unitChanged) {
522
+ const updatedFilters = updateRadiusInNearFiltersOnLocationField(
523
+ searchFilters.static || [],
524
+ radius
525
+ );
526
+ searchActions.setStaticFilters(updatedFilters);
527
+ searchActions.setOffset(0);
528
+ preserveMapView.current = false;
529
+ executeSearch(searchActions);
530
+ setSearchState("loading");
531
+ }
532
+ }, [
533
+ distanceUnit,
534
+ defaultRadiusMeters,
535
+ selectedDistanceOption,
536
+ searchActions,
537
+ searchFilters.static,
538
+ ]);
453
539
 
454
540
  const resultsRef = React.useRef<Array<HTMLDivElement | null>>([]);
455
541
  const resultsContainer = React.useRef<HTMLDivElement>(null);
@@ -537,12 +623,14 @@ const LocatorInternal = ({
537
623
  resultCardProps={resultCardProps}
538
624
  isSelected={result.result.index === selectedResultIndex}
539
625
  distanceDisplay={distanceDisplay}
626
+ distanceUnit={distanceUnit}
540
627
  searchLocationName={filterDisplayName}
541
628
  />
542
629
  );
543
630
  },
544
631
  [
545
632
  distanceDisplay,
633
+ distanceUnit,
546
634
  filterDisplayName,
547
635
  getResultCardProps,
548
636
  selectedResultIndex,
@@ -596,6 +684,7 @@ const LocatorInternal = ({
596
684
  const mapProps = React.useMemo(
597
685
  () => ({
598
686
  mapStyle,
687
+ preserveMapView,
599
688
  centerCoords,
600
689
  onDragHandler: handleDrag,
601
690
  scrollToResult,
@@ -612,8 +701,46 @@ const LocatorInternal = ({
612
701
  ]
613
702
  );
614
703
 
704
+ const initializationInputsRef = React.useRef({
705
+ urlNavigationVersion,
706
+ initialMapCenter,
707
+ searchActions,
708
+ configuredRadiusMeters,
709
+ initialDefaultRadiusMeters,
710
+ });
711
+ const initializationCompleted = React.useRef(false);
615
712
  React.useEffect(() => {
713
+ const previousInputs = initializationInputsRef.current;
714
+ initializationInputsRef.current = {
715
+ urlNavigationVersion,
716
+ initialMapCenter,
717
+ searchActions,
718
+ configuredRadiusMeters,
719
+ initialDefaultRadiusMeters,
720
+ };
721
+ // Changing only the default unit should update the active distance filter
722
+ // without restarting location initialization.
723
+ if (
724
+ initializationCompleted.current &&
725
+ selectedDistanceOption !== null &&
726
+ previousInputs.urlNavigationVersion === urlNavigationVersion &&
727
+ previousInputs.initialMapCenter === initialMapCenter &&
728
+ previousInputs.searchActions === searchActions &&
729
+ previousInputs.configuredRadiusMeters === configuredRadiusMeters &&
730
+ previousInputs.initialDefaultRadiusMeters !== initialDefaultRadiusMeters
731
+ ) {
732
+ return;
733
+ }
734
+
735
+ initializationCompleted.current = false;
616
736
  let isCancelled = false;
737
+ const getActiveDistanceMeters = () => {
738
+ const { selectedDistanceOption, distanceUnit } =
739
+ currentDistanceSelection.current;
740
+ return selectedDistanceOption === null
741
+ ? null
742
+ : toMeters(selectedDistanceOption, distanceUnit);
743
+ };
617
744
  const queryParams = new URLSearchParams(window.location.search);
618
745
  const initialLocationParam = queryParams.has(LOCATION_QUERY_KEY)
619
746
  ? queryParams.get(LOCATION_QUERY_KEY)
@@ -624,11 +751,13 @@ const LocatorInternal = ({
624
751
  setCenterCoords(initialMapCenter);
625
752
  setMapCenter(initialMapCenter);
626
753
 
754
+ const activeDistanceMeters = getActiveDistanceMeters();
627
755
  const radius =
628
- showDistanceOptions && selectedDistanceMeters
629
- ? selectedDistanceMeters
630
- : defaultRadiusMeters;
631
- baseFilterRadius.current = defaultRadiusMeters;
756
+ showDistanceOptions && activeDistanceMeters
757
+ ? activeDistanceMeters
758
+ : initialDefaultRadiusMeters;
759
+ baseFilterRadius.current = initialDefaultRadiusMeters;
760
+ automaticBaseRadiusAccuracy.current = 0;
632
761
  // default location filter to configured starting location or NYC
633
762
  let initialLocationFilter = buildNearLocationFilterFromCoords(
634
763
  initialMapCenter.latitude,
@@ -646,6 +775,7 @@ const LocatorInternal = ({
646
775
  ? [initialLocationFilter]
647
776
  : [initialLocationFilter, openNowFilter]
648
777
  );
778
+ preserveMapView.current = false;
649
779
  searchActions.executeVerticalQuery();
650
780
  setSearchState("loading");
651
781
  if (
@@ -672,6 +802,7 @@ const LocatorInternal = ({
672
802
  }
673
803
  if (!isCancelled) {
674
804
  setIsInitialMapLocationResolved(true);
805
+ initializationCompleted.current = true;
675
806
  }
676
807
  };
677
808
 
@@ -701,11 +832,13 @@ const LocatorInternal = ({
701
832
  const filterFromResult = resultFilter.value as NearFilterValue;
702
833
  baseFilterRadius.current =
703
834
  configuredRadiusMeters ?? filterFromResult.radius;
835
+ automaticBaseRadiusAccuracy.current = null;
836
+ const activeDistanceMeters = getActiveDistanceMeters();
704
837
  initialLocationFilter = buildNearLocationFilterFromPrevious(
705
838
  filterFromResult,
706
839
  firstResult.value,
707
- showDistanceOptions && selectedDistanceMeters
708
- ? selectedDistanceMeters
840
+ showDistanceOptions && activeDistanceMeters
841
+ ? activeDistanceMeters
709
842
  : baseFilterRadius.current
710
843
  );
711
844
  return true;
@@ -716,6 +849,7 @@ const LocatorInternal = ({
716
849
  firstResult.value
717
850
  );
718
851
  baseFilterRadius.current = null;
852
+ automaticBaseRadiusAccuracy.current = null;
719
853
  return true;
720
854
  }
721
855
  default: {
@@ -736,6 +870,7 @@ const LocatorInternal = ({
736
870
  : undefined;
737
871
  if (coordinates) {
738
872
  baseFilterRadius.current = coordinates.radius;
873
+ automaticBaseRadiusAccuracy.current = null;
739
874
  setSelectedDistanceOption(null);
740
875
  setSelectedDistanceMeters(null);
741
876
  initialLocationFilter = buildNearLocationFilterFromCoords(
@@ -767,10 +902,6 @@ const LocatorInternal = ({
767
902
  }
768
903
  const lat = location.coords.latitude;
769
904
  const lng = location.coords.longitude;
770
- baseFilterRadius.current = Math.max(
771
- location.coords.accuracy,
772
- defaultRadiusMeters
773
- );
774
905
  setShowCurrentLocationButton(true);
775
906
 
776
907
  // Try to reverse-geocode the coordinates to a human-readable place name using Mapbox
@@ -796,14 +927,33 @@ const LocatorInternal = ({
796
927
  }
797
928
  } catch (e) {
798
929
  console.warn("Reverse geocoding failed:", e);
799
- } finally {
800
- initialLocationFilter = buildNearLocationFilterFromCoords(
801
- lat,
802
- lng,
803
- Math.max(location.coords.accuracy, radius),
804
- displayName
805
- );
806
930
  }
931
+ if (isCancelled) {
932
+ return;
933
+ }
934
+ const activeDistanceMeters = getActiveDistanceMeters();
935
+ const currentDefaultRadiusMeters =
936
+ configuredRadiusMeters ??
937
+ toMeters(
938
+ DEFAULT_RADIUS,
939
+ currentDistanceSelection.current.distanceUnit
940
+ );
941
+ baseFilterRadius.current = Math.max(
942
+ location.coords.accuracy,
943
+ currentDefaultRadiusMeters
944
+ );
945
+ automaticBaseRadiusAccuracy.current = location.coords.accuracy;
946
+ initialLocationFilter = buildNearLocationFilterFromCoords(
947
+ lat,
948
+ lng,
949
+ Math.max(
950
+ location.coords.accuracy,
951
+ showDistanceOptions && activeDistanceMeters
952
+ ? activeDistanceMeters
953
+ : currentDefaultRadiusMeters
954
+ ),
955
+ displayName
956
+ );
807
957
  } catch {
808
958
  // Fall back to configured starting location or default center.
809
959
  }
@@ -825,7 +975,7 @@ const LocatorInternal = ({
825
975
  initialMapCenter,
826
976
  searchActions,
827
977
  configuredRadiusMeters,
828
- defaultRadiusMeters,
978
+ initialDefaultRadiusMeters,
829
979
  ]);
830
980
 
831
981
  const handleOpenNowClick = (selected: boolean) => {
@@ -845,6 +995,7 @@ const LocatorInternal = ({
845
995
  });
846
996
  setIsOpenNowSelected(selected);
847
997
  searchActions.setOffset(0);
998
+ preserveMapView.current = false;
848
999
  executeSearch(searchActions);
849
1000
  };
850
1001
 
@@ -883,6 +1034,7 @@ const LocatorInternal = ({
883
1034
  // Always reload from offset 0 if switching to mobile
884
1035
  setMobileResults([]);
885
1036
  searchActions.setOffset(0);
1037
+ preserveMapView.current = false;
886
1038
  executeSearch(searchActions);
887
1039
  setSearchState("loading");
888
1040
  }, [currentOffset, isMobile, searchActions, searchLoading]);
@@ -903,7 +1055,7 @@ const LocatorInternal = ({
903
1055
 
904
1056
  const handleDistanceClick = (
905
1057
  distance: number,
906
- distanceUnit: "mile" | "kilometer"
1058
+ distanceUnit: DistanceUnit
907
1059
  ) => {
908
1060
  const existingFilters = searchFilters.static || [];
909
1061
  let updatedFilters: SelectableStaticFilter[];
@@ -925,9 +1077,15 @@ const LocatorInternal = ({
925
1077
  }
926
1078
  searchActions.setStaticFilters(updatedFilters);
927
1079
  searchActions.setOffset(0);
1080
+ preserveMapView.current = false;
928
1081
  executeSearch(searchActions);
929
1082
  };
930
1083
 
1084
+ const handleDistanceUnitChange = (nextUnit: DistanceUnit) => {
1085
+ if (nextUnit === distanceUnit) return;
1086
+ setDistanceUnitOverride(nextUnit);
1087
+ };
1088
+
931
1089
  const handleClearFiltersClick = () => {
932
1090
  const existingFilters = searchFilters.static || [];
933
1091
  // Restore the current location's base radius.
@@ -943,6 +1101,7 @@ const LocatorInternal = ({
943
1101
  searchActions.resetFacets();
944
1102
  // Execute search to update AppliedFilters components
945
1103
  searchActions.setOffset(0);
1104
+ preserveMapView.current = false;
946
1105
  executeSearch(searchActions);
947
1106
  setSelectedDistanceMeters(null);
948
1107
  setSelectedDistanceOption(null);
@@ -989,6 +1148,15 @@ const LocatorInternal = ({
989
1148
  : false;
990
1149
  // If no opt-in is required, the map is already enabled.
991
1150
  const [mapEnabled, setMapEnabled] = React.useState(!requireMapOptIn);
1151
+ // Cancel pending searches when the prop or map is disabled, and on unmount.
1152
+ React.useEffect(
1153
+ () => cancelMapSearch,
1154
+ [autoSearchOnMapMove, mapEnabled, cancelMapSearch]
1155
+ );
1156
+ React.useEffect((): void => {
1157
+ cancelMapSearch();
1158
+ setShowSearchAreaButton(false);
1159
+ }, [urlNavigationVersion, cancelMapSearch]);
992
1160
  // Adds unified [enable|disable]Map functions to the window.
993
1161
  useEffect(() => {
994
1162
  (window as any).enableMap = () => setMapEnabled(true);
@@ -1050,6 +1218,33 @@ const LocatorInternal = ({
1050
1218
  radius: fromMeters(defaultRadiusMeters, "mile"),
1051
1219
  }}
1052
1220
  />
1221
+ {showDistanceUnitToggle && (
1222
+ <div className="flex items-center justify-end gap-3 font-body-fontFamily text-body-sm-fontSize">
1223
+ <span>{t("distance", "Distance")}</span>
1224
+ <div
1225
+ role="group"
1226
+ aria-label={t("distance", "Distance")}
1227
+ className="inline-flex rounded-md border border-gray-300 overflow-hidden"
1228
+ >
1229
+ {DISTANCE_UNITS.map((unit) => (
1230
+ <button
1231
+ key={unit}
1232
+ type="button"
1233
+ aria-label={translateDistanceUnit(t, unit, 2)}
1234
+ aria-pressed={distanceUnit === unit}
1235
+ onClick={() => handleDistanceUnitChange(unit)}
1236
+ className={`px-3 py-1 font-bold ${
1237
+ distanceUnit === unit
1238
+ ? "bg-palette-primary text-palette-primary-contrast"
1239
+ : "bg-white text-palette-primary-dark"
1240
+ }`}
1241
+ >
1242
+ {unit === MILE ? "mi" : "km"}
1243
+ </button>
1244
+ ))}
1245
+ </div>
1246
+ </div>
1247
+ )}
1053
1248
  </div>
1054
1249
  <div className="relative flex-1 flex flex-col min-h-0">
1055
1250
  <div className="px-8 py-4 text-body-fontSize border-y border-gray-300 inline-block">
@@ -1058,6 +1253,7 @@ const LocatorInternal = ({
1058
1253
  searchState={searchState}
1059
1254
  resultCount={resultCount}
1060
1255
  selectedDistanceOption={selectedDistanceOption}
1256
+ distanceUnit={distanceUnit}
1061
1257
  filterDisplayName={filterDisplayName}
1062
1258
  />
1063
1259
  {hasFilterModalToggle && (
@@ -1104,6 +1300,7 @@ const LocatorInternal = ({
1104
1300
  }
1105
1301
 
1106
1302
  searchActions.setOffset(mobileResults.length);
1303
+ preserveMapView.current = false;
1107
1304
  executeSearch(searchActions);
1108
1305
  setSearchState("loading");
1109
1306
  }}
@@ -1133,6 +1330,7 @@ const LocatorInternal = ({
1133
1330
  handleOpenNowClick={handleOpenNowClick}
1134
1331
  showDistanceOptions={showDistanceOptions}
1135
1332
  selectedDistanceOption={selectedDistanceOption}
1333
+ distanceUnit={distanceUnit}
1136
1334
  handleDistanceClick={handleDistanceClick}
1137
1335
  handleCloseModalClick={() => setShowFilterModal(false)}
1138
1336
  handleClearFiltersClick={handleClearFiltersClick}
@@ -1174,10 +1372,10 @@ const LocatorInternal = ({
1174
1372
  </div>
1175
1373
  </div>
1176
1374
  )}
1177
- {showSearchAreaButton && (
1375
+ {showSearchAreaButton && !autoSearchOnMapMove && (
1178
1376
  <div className="absolute top-10 left-0 right-0 flex justify-center">
1179
1377
  <Button
1180
- onClick={handleSearchAreaClick}
1378
+ onClick={() => handleSearchAreaClick()}
1181
1379
  className="py-2 px-4 shadow-xl"
1182
1380
  >
1183
1381
  <Body>{t("searchThisArea", "Search This Area")}</Body>
@@ -7,6 +7,9 @@ import {
7
7
  } from "@yext/search-ui-react";
8
8
  import { Result } from "@yext/search-headless-react";
9
9
  import React from "react";
10
+ import type mapboxgl from "mapbox-gl";
11
+ import ReactDOM from "react-dom";
12
+ import { createRoot, type Root } from "react-dom/client";
10
13
  import { useTranslation } from "react-i18next";
11
14
  import { ImageField } from "../../../fields/ImageField.tsx";
12
15
  import { useDocument } from "../../../hooks/useDocument.tsx";
@@ -135,6 +138,7 @@ const LocatorTestMap = () => {
135
138
 
136
139
  interface MapProps {
137
140
  mapStyle?: string;
141
+ preserveMapView?: React.MutableRefObject<boolean>;
138
142
  centerCoords?: Coordinate;
139
143
  onDragHandler?: OnDragHandler;
140
144
  scrollToResult?: (result: Result<Location> | undefined) => void;
@@ -144,6 +148,7 @@ interface MapProps {
144
148
 
145
149
  export const LocatorMap: React.FC<MapProps> = ({
146
150
  mapStyle,
151
+ preserveMapView,
147
152
  centerCoords,
148
153
  onDragHandler,
149
154
  scrollToResult,
@@ -175,17 +180,91 @@ export const LocatorMap: React.FC<MapProps> = ({
175
180
  }),
176
181
  [centerCoords, mapPadding, mapStyle]
177
182
  );
178
- const PinComponent = React.useMemo(
179
- () =>
180
- function PinComponent<T>(pinProps: PinComponentProps<T>) {
181
- return (
182
- <LocatorMapPin
183
- {...pinProps}
184
- locationStyleConfig={locationStyleConfig}
185
- />
186
- );
187
- },
188
- [locationStyleConfig]
183
+ const markerRoots = React.useRef(new Map<HTMLElement, Root>());
184
+ const restoreCamera = React.useRef<(() => void) | null>(null);
185
+
186
+ /**
187
+ * 1. Guard this map's camera before Search UI fits the new markers.
188
+ * 2. Render each pin and remove its React root when its marker is removed.
189
+ */
190
+ const renderPin = React.useCallback(
191
+ (
192
+ pinProps: PinComponentProps<Location> & { container: HTMLElement }
193
+ ): void => {
194
+ if (!restoreCamera.current) {
195
+ // Search UI can provide the native map directly or through a facade.
196
+ const nativeMap = (
197
+ "getNativeInstance" in pinProps.mapbox &&
198
+ typeof pinProps.mapbox.getNativeInstance === "function"
199
+ ? pinProps.mapbox.getNativeInstance()
200
+ : pinProps.mapbox
201
+ ) as mapboxgl.Map;
202
+ const fitBounds = nativeMap.fitBounds;
203
+ const flyTo = nativeMap.flyTo;
204
+ nativeMap.fitBounds = (
205
+ ...args: Parameters<mapboxgl.Map["fitBounds"]>
206
+ ): mapboxgl.Map =>
207
+ preserveMapView?.current
208
+ ? nativeMap
209
+ : fitBounds.apply(nativeMap, args);
210
+ nativeMap.flyTo = (
211
+ ...args: Parameters<mapboxgl.Map["flyTo"]>
212
+ ): mapboxgl.Map =>
213
+ preserveMapView?.current ? nativeMap : flyTo.apply(nativeMap, args);
214
+
215
+ const observer = new MutationObserver((): void => {
216
+ for (const [container, root] of markerRoots.current) {
217
+ if (!nativeMap.getContainer().contains(container)) {
218
+ markerRoots.current.delete(container);
219
+ queueMicrotask((): void => root.unmount());
220
+ }
221
+ }
222
+ });
223
+ observer.observe(nativeMap.getContainer(), {
224
+ childList: true,
225
+ subtree: true,
226
+ });
227
+ restoreCamera.current = (): void => {
228
+ observer.disconnect();
229
+ nativeMap.fitBounds = fitBounds;
230
+ nativeMap.flyTo = flyTo;
231
+ };
232
+ }
233
+ let root = markerRoots.current.get(pinProps.container);
234
+ if (!root) {
235
+ // Keep the React 17 rendering path supported by this package.
236
+ root =
237
+ Number(React.version.split(".")[0]) >= 18
238
+ ? createRoot(pinProps.container)
239
+ : {
240
+ render: (children: React.ReactNode): void => {
241
+ ReactDOM.render(<>{children}</>, pinProps.container);
242
+ },
243
+ unmount: (): void => {
244
+ ReactDOM.unmountComponentAtNode(pinProps.container);
245
+ },
246
+ };
247
+ markerRoots.current.set(pinProps.container, root);
248
+ }
249
+ root.render(
250
+ <LocatorMapPin
251
+ {...pinProps}
252
+ locationStyleConfig={locationStyleConfig}
253
+ />
254
+ );
255
+ },
256
+ [locationStyleConfig, preserveMapView]
257
+ );
258
+ React.useEffect(
259
+ () => (): void => {
260
+ restoreCamera.current?.();
261
+ restoreCamera.current = null;
262
+ for (const root of markerRoots.current.values()) {
263
+ queueMicrotask((): void => root.unmount());
264
+ }
265
+ markerRoots.current.clear();
266
+ },
267
+ []
189
268
  );
190
269
 
191
270
  if (isVisualEditorTestEnv()) {
@@ -213,7 +292,7 @@ export const LocatorMap: React.FC<MapProps> = ({
213
292
  mapboxAccessToken={mapboxApiKey || ""}
214
293
  mapboxOptions={mapboxOptions}
215
294
  onDrag={onDragHandler}
216
- PinComponent={PinComponent}
295
+ renderPin={renderPin}
217
296
  iframeWindow={iframe?.contentWindow ?? undefined}
218
297
  allowUpdates={!!iframe?.contentDocument}
219
298
  onPinClick={scrollToResult}