@page-speed/maps 0.2.0 → 0.2.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.
@@ -592,6 +592,19 @@ var PANEL_POSITION_CLASS = {
592
592
  "bottom-left": "bottom-4 left-4",
593
593
  "bottom-right": "bottom-4 right-4"
594
594
  };
595
+ function getOptimalPanelPosition(markerLat, markerLng, mapCenter) {
596
+ const isNorth = markerLat >= mapCenter.latitude;
597
+ const isEast = markerLng >= mapCenter.longitude;
598
+ if (isNorth && isEast) {
599
+ return "bottom-left";
600
+ } else if (isNorth && !isEast) {
601
+ return "bottom-right";
602
+ } else if (!isNorth && isEast) {
603
+ return "top-left";
604
+ } else {
605
+ return "top-right";
606
+ }
607
+ }
595
608
  var DEFAULT_VIEW_STATE = {
596
609
  latitude: 39.5,
597
610
  longitude: -98.35,
@@ -861,9 +874,16 @@ function GeoMap({
861
874
  const updateDimensions = () => {
862
875
  if (containerRef.current) {
863
876
  const rect = containerRef.current.getBoundingClientRect();
864
- setContainerDimensions({
865
- width: rect.width || 800,
866
- height: rect.height || calculatedHeight
877
+ setContainerDimensions((prev) => {
878
+ const newWidth = rect.width || 800;
879
+ const newHeight = rect.height || calculatedHeight;
880
+ if (prev.width === newWidth && prev.height === newHeight) {
881
+ return prev;
882
+ }
883
+ return {
884
+ width: newWidth,
885
+ height: newHeight
886
+ };
867
887
  });
868
888
  }
869
889
  };
@@ -997,6 +1017,10 @@ function GeoMap({
997
1017
  longitude: defaultViewState?.longitude ?? firstCoordinate.longitude,
998
1018
  zoom: defaultViewState?.zoom ?? calculatedZoom
999
1019
  });
1020
+ const [dynamicPanelPosition, setDynamicPanelPosition] = React3.useState(
1021
+ panelPosition
1022
+ );
1023
+ const viewStateRef = React3.useRef(uncontrolledViewState);
1000
1024
  React3.useEffect(() => {
1001
1025
  if (!viewState && !defaultViewState) {
1002
1026
  setUncontrolledViewState({
@@ -1008,6 +1032,9 @@ function GeoMap({
1008
1032
  }, [firstCoordinate, calculatedZoom, viewState, defaultViewState]);
1009
1033
  const isControlledViewState = viewState !== void 0;
1010
1034
  const resolvedViewState = isControlledViewState ? viewState : uncontrolledViewState;
1035
+ React3.useEffect(() => {
1036
+ viewStateRef.current = resolvedViewState || uncontrolledViewState;
1037
+ }, [resolvedViewState, uncontrolledViewState]);
1011
1038
  const applyViewState = React3.useCallback(
1012
1039
  (nextState) => {
1013
1040
  if (!isControlledViewState) {
@@ -1076,6 +1103,17 @@ function GeoMap({
1076
1103
  markerId: marker.id,
1077
1104
  clusterId: marker.clusterId
1078
1105
  });
1106
+ const currentState = viewStateRef.current;
1107
+ const center = {
1108
+ latitude: currentState.latitude ?? 0,
1109
+ longitude: currentState.longitude ?? 0
1110
+ };
1111
+ const optimalPosition = getOptimalPanelPosition(
1112
+ marker.latitude,
1113
+ marker.longitude,
1114
+ center
1115
+ );
1116
+ setDynamicPanelPosition(optimalPosition);
1079
1117
  applyViewState({
1080
1118
  latitude: marker.latitude,
1081
1119
  longitude: marker.longitude,
@@ -1091,6 +1129,17 @@ function GeoMap({
1091
1129
  type: "cluster",
1092
1130
  clusterId: cluster.id
1093
1131
  });
1132
+ const currentState = viewStateRef.current;
1133
+ const center = {
1134
+ latitude: currentState.latitude ?? 0,
1135
+ longitude: currentState.longitude ?? 0
1136
+ };
1137
+ const optimalPosition = getOptimalPanelPosition(
1138
+ cluster.latitude,
1139
+ cluster.longitude,
1140
+ center
1141
+ );
1142
+ setDynamicPanelPosition(optimalPosition);
1094
1143
  applyViewState({
1095
1144
  latitude: cluster.latitude,
1096
1145
  longitude: cluster.longitude,
@@ -1385,7 +1434,7 @@ function GeoMap({
1385
1434
  {
1386
1435
  className: cn(
1387
1436
  "pointer-events-none absolute z-30",
1388
- PANEL_POSITION_CLASS[panelPosition]
1437
+ PANEL_POSITION_CLASS[dynamicPanelPosition]
1389
1438
  ),
1390
1439
  style: {
1391
1440
  // Ensure panel can overflow and has higher z-index