@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.
@@ -732,6 +732,19 @@ var PANEL_POSITION_CLASS = {
732
732
  "bottom-left": "bottom-4 left-4",
733
733
  "bottom-right": "bottom-4 right-4"
734
734
  };
735
+ function getOptimalPanelPosition(markerLat, markerLng, mapCenter) {
736
+ const isNorth = markerLat >= mapCenter.latitude;
737
+ const isEast = markerLng >= mapCenter.longitude;
738
+ if (isNorth && isEast) {
739
+ return "bottom-left";
740
+ } else if (isNorth && !isEast) {
741
+ return "bottom-right";
742
+ } else if (!isNorth && isEast) {
743
+ return "top-left";
744
+ } else {
745
+ return "top-right";
746
+ }
747
+ }
735
748
  var DEFAULT_VIEW_STATE = {
736
749
  latitude: 39.5,
737
750
  longitude: -98.35,
@@ -1137,6 +1150,9 @@ function GeoMap({
1137
1150
  longitude: defaultViewState?.longitude ?? firstCoordinate.longitude,
1138
1151
  zoom: defaultViewState?.zoom ?? calculatedZoom
1139
1152
  });
1153
+ const [dynamicPanelPosition, setDynamicPanelPosition] = React3__namespace.useState(
1154
+ panelPosition
1155
+ );
1140
1156
  React3__namespace.useEffect(() => {
1141
1157
  if (!viewState && !defaultViewState) {
1142
1158
  setUncontrolledViewState({
@@ -1216,6 +1232,19 @@ function GeoMap({
1216
1232
  markerId: marker.id,
1217
1233
  clusterId: marker.clusterId
1218
1234
  });
1235
+ const currentCenter = resolvedViewState || {
1236
+ latitude: firstCoordinate.latitude,
1237
+ longitude: firstCoordinate.longitude
1238
+ };
1239
+ const optimalPosition = getOptimalPanelPosition(
1240
+ marker.latitude,
1241
+ marker.longitude,
1242
+ {
1243
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1244
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1245
+ }
1246
+ );
1247
+ setDynamicPanelPosition(optimalPosition);
1219
1248
  applyViewState({
1220
1249
  latitude: marker.latitude,
1221
1250
  longitude: marker.longitude,
@@ -1223,7 +1252,7 @@ function GeoMap({
1223
1252
  });
1224
1253
  emitSelectionChange({ type: "marker", marker });
1225
1254
  },
1226
- [applyViewState, emitSelectionChange, markerFocusZoom]
1255
+ [applyViewState, emitSelectionChange, markerFocusZoom, resolvedViewState, firstCoordinate]
1227
1256
  );
1228
1257
  const selectCluster = React3__namespace.useCallback(
1229
1258
  (cluster) => {
@@ -1231,6 +1260,19 @@ function GeoMap({
1231
1260
  type: "cluster",
1232
1261
  clusterId: cluster.id
1233
1262
  });
1263
+ const currentCenter = resolvedViewState || {
1264
+ latitude: firstCoordinate.latitude,
1265
+ longitude: firstCoordinate.longitude
1266
+ };
1267
+ const optimalPosition = getOptimalPanelPosition(
1268
+ cluster.latitude,
1269
+ cluster.longitude,
1270
+ {
1271
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1272
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1273
+ }
1274
+ );
1275
+ setDynamicPanelPosition(optimalPosition);
1234
1276
  applyViewState({
1235
1277
  latitude: cluster.latitude,
1236
1278
  longitude: cluster.longitude,
@@ -1238,7 +1280,7 @@ function GeoMap({
1238
1280
  });
1239
1281
  emitSelectionChange({ type: "cluster", cluster });
1240
1282
  },
1241
- [applyViewState, clusterFocusZoom, emitSelectionChange]
1283
+ [applyViewState, clusterFocusZoom, emitSelectionChange, resolvedViewState, firstCoordinate]
1242
1284
  );
1243
1285
  const clearSelection = React3__namespace.useCallback(() => {
1244
1286
  setSelection({ type: "none" });
@@ -1525,7 +1567,7 @@ function GeoMap({
1525
1567
  {
1526
1568
  className: cn(
1527
1569
  "pointer-events-none absolute z-30",
1528
- PANEL_POSITION_CLASS[panelPosition]
1570
+ PANEL_POSITION_CLASS[dynamicPanelPosition]
1529
1571
  ),
1530
1572
  style: {
1531
1573
  // Ensure panel can overflow and has higher z-index