@page-speed/maps 0.2.0 → 0.2.1

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.
@@ -613,6 +613,19 @@ var PANEL_POSITION_CLASS = {
613
613
  "bottom-left": "bottom-4 left-4",
614
614
  "bottom-right": "bottom-4 right-4"
615
615
  };
616
+ function getOptimalPanelPosition(markerLat, markerLng, mapCenter) {
617
+ const isNorth = markerLat >= mapCenter.latitude;
618
+ const isEast = markerLng >= mapCenter.longitude;
619
+ if (isNorth && isEast) {
620
+ return "bottom-left";
621
+ } else if (isNorth && !isEast) {
622
+ return "bottom-right";
623
+ } else if (!isNorth && isEast) {
624
+ return "top-left";
625
+ } else {
626
+ return "top-right";
627
+ }
628
+ }
616
629
  var DEFAULT_VIEW_STATE = {
617
630
  latitude: 39.5,
618
631
  longitude: -98.35,
@@ -1018,6 +1031,9 @@ function GeoMap({
1018
1031
  longitude: defaultViewState?.longitude ?? firstCoordinate.longitude,
1019
1032
  zoom: defaultViewState?.zoom ?? calculatedZoom
1020
1033
  });
1034
+ const [dynamicPanelPosition, setDynamicPanelPosition] = React3__namespace.useState(
1035
+ panelPosition
1036
+ );
1021
1037
  React3__namespace.useEffect(() => {
1022
1038
  if (!viewState && !defaultViewState) {
1023
1039
  setUncontrolledViewState({
@@ -1097,6 +1113,19 @@ function GeoMap({
1097
1113
  markerId: marker.id,
1098
1114
  clusterId: marker.clusterId
1099
1115
  });
1116
+ const currentCenter = resolvedViewState || {
1117
+ latitude: firstCoordinate.latitude,
1118
+ longitude: firstCoordinate.longitude
1119
+ };
1120
+ const optimalPosition = getOptimalPanelPosition(
1121
+ marker.latitude,
1122
+ marker.longitude,
1123
+ {
1124
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1125
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1126
+ }
1127
+ );
1128
+ setDynamicPanelPosition(optimalPosition);
1100
1129
  applyViewState({
1101
1130
  latitude: marker.latitude,
1102
1131
  longitude: marker.longitude,
@@ -1104,7 +1133,7 @@ function GeoMap({
1104
1133
  });
1105
1134
  emitSelectionChange({ type: "marker", marker });
1106
1135
  },
1107
- [applyViewState, emitSelectionChange, markerFocusZoom]
1136
+ [applyViewState, emitSelectionChange, markerFocusZoom, resolvedViewState, firstCoordinate]
1108
1137
  );
1109
1138
  const selectCluster = React3__namespace.useCallback(
1110
1139
  (cluster) => {
@@ -1112,6 +1141,19 @@ function GeoMap({
1112
1141
  type: "cluster",
1113
1142
  clusterId: cluster.id
1114
1143
  });
1144
+ const currentCenter = resolvedViewState || {
1145
+ latitude: firstCoordinate.latitude,
1146
+ longitude: firstCoordinate.longitude
1147
+ };
1148
+ const optimalPosition = getOptimalPanelPosition(
1149
+ cluster.latitude,
1150
+ cluster.longitude,
1151
+ {
1152
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1153
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1154
+ }
1155
+ );
1156
+ setDynamicPanelPosition(optimalPosition);
1115
1157
  applyViewState({
1116
1158
  latitude: cluster.latitude,
1117
1159
  longitude: cluster.longitude,
@@ -1119,7 +1161,7 @@ function GeoMap({
1119
1161
  });
1120
1162
  emitSelectionChange({ type: "cluster", cluster });
1121
1163
  },
1122
- [applyViewState, clusterFocusZoom, emitSelectionChange]
1164
+ [applyViewState, clusterFocusZoom, emitSelectionChange, resolvedViewState, firstCoordinate]
1123
1165
  );
1124
1166
  const clearSelection = React3__namespace.useCallback(() => {
1125
1167
  setSelection({ type: "none" });
@@ -1406,7 +1448,7 @@ function GeoMap({
1406
1448
  {
1407
1449
  className: cn(
1408
1450
  "pointer-events-none absolute z-30",
1409
- PANEL_POSITION_CLASS[panelPosition]
1451
+ PANEL_POSITION_CLASS[dynamicPanelPosition]
1410
1452
  ),
1411
1453
  style: {
1412
1454
  // Ensure panel can overflow and has higher z-index