@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.
package/dist/index.js CHANGED
@@ -749,6 +749,19 @@ var PANEL_POSITION_CLASS = {
749
749
  "bottom-left": "bottom-4 left-4",
750
750
  "bottom-right": "bottom-4 right-4"
751
751
  };
752
+ function getOptimalPanelPosition(markerLat, markerLng, mapCenter) {
753
+ const isNorth = markerLat >= mapCenter.latitude;
754
+ const isEast = markerLng >= mapCenter.longitude;
755
+ if (isNorth && isEast) {
756
+ return "bottom-left";
757
+ } else if (isNorth && !isEast) {
758
+ return "bottom-right";
759
+ } else if (!isNorth && isEast) {
760
+ return "top-left";
761
+ } else {
762
+ return "top-right";
763
+ }
764
+ }
752
765
  var DEFAULT_VIEW_STATE = {
753
766
  latitude: 39.5,
754
767
  longitude: -98.35,
@@ -1154,6 +1167,9 @@ function GeoMap({
1154
1167
  longitude: defaultViewState?.longitude ?? firstCoordinate.longitude,
1155
1168
  zoom: defaultViewState?.zoom ?? calculatedZoom
1156
1169
  });
1170
+ const [dynamicPanelPosition, setDynamicPanelPosition] = React3.useState(
1171
+ panelPosition
1172
+ );
1157
1173
  React3.useEffect(() => {
1158
1174
  if (!viewState && !defaultViewState) {
1159
1175
  setUncontrolledViewState({
@@ -1233,6 +1249,19 @@ function GeoMap({
1233
1249
  markerId: marker.id,
1234
1250
  clusterId: marker.clusterId
1235
1251
  });
1252
+ const currentCenter = resolvedViewState || {
1253
+ latitude: firstCoordinate.latitude,
1254
+ longitude: firstCoordinate.longitude
1255
+ };
1256
+ const optimalPosition = getOptimalPanelPosition(
1257
+ marker.latitude,
1258
+ marker.longitude,
1259
+ {
1260
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1261
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1262
+ }
1263
+ );
1264
+ setDynamicPanelPosition(optimalPosition);
1236
1265
  applyViewState({
1237
1266
  latitude: marker.latitude,
1238
1267
  longitude: marker.longitude,
@@ -1240,7 +1269,7 @@ function GeoMap({
1240
1269
  });
1241
1270
  emitSelectionChange({ type: "marker", marker });
1242
1271
  },
1243
- [applyViewState, emitSelectionChange, markerFocusZoom]
1272
+ [applyViewState, emitSelectionChange, markerFocusZoom, resolvedViewState, firstCoordinate]
1244
1273
  );
1245
1274
  const selectCluster = React3.useCallback(
1246
1275
  (cluster) => {
@@ -1248,6 +1277,19 @@ function GeoMap({
1248
1277
  type: "cluster",
1249
1278
  clusterId: cluster.id
1250
1279
  });
1280
+ const currentCenter = resolvedViewState || {
1281
+ latitude: firstCoordinate.latitude,
1282
+ longitude: firstCoordinate.longitude
1283
+ };
1284
+ const optimalPosition = getOptimalPanelPosition(
1285
+ cluster.latitude,
1286
+ cluster.longitude,
1287
+ {
1288
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1289
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1290
+ }
1291
+ );
1292
+ setDynamicPanelPosition(optimalPosition);
1251
1293
  applyViewState({
1252
1294
  latitude: cluster.latitude,
1253
1295
  longitude: cluster.longitude,
@@ -1255,7 +1297,7 @@ function GeoMap({
1255
1297
  });
1256
1298
  emitSelectionChange({ type: "cluster", cluster });
1257
1299
  },
1258
- [applyViewState, clusterFocusZoom, emitSelectionChange]
1300
+ [applyViewState, clusterFocusZoom, emitSelectionChange, resolvedViewState, firstCoordinate]
1259
1301
  );
1260
1302
  const clearSelection = React3.useCallback(() => {
1261
1303
  setSelection({ type: "none" });
@@ -1542,7 +1584,7 @@ function GeoMap({
1542
1584
  {
1543
1585
  className: cn(
1544
1586
  "pointer-events-none absolute z-30",
1545
- PANEL_POSITION_CLASS[panelPosition]
1587
+ PANEL_POSITION_CLASS[dynamicPanelPosition]
1546
1588
  ),
1547
1589
  style: {
1548
1590
  // Ensure panel can overflow and has higher z-index