@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.cjs CHANGED
@@ -770,6 +770,19 @@ var PANEL_POSITION_CLASS = {
770
770
  "bottom-left": "bottom-4 left-4",
771
771
  "bottom-right": "bottom-4 right-4"
772
772
  };
773
+ function getOptimalPanelPosition(markerLat, markerLng, mapCenter) {
774
+ const isNorth = markerLat >= mapCenter.latitude;
775
+ const isEast = markerLng >= mapCenter.longitude;
776
+ if (isNorth && isEast) {
777
+ return "bottom-left";
778
+ } else if (isNorth && !isEast) {
779
+ return "bottom-right";
780
+ } else if (!isNorth && isEast) {
781
+ return "top-left";
782
+ } else {
783
+ return "top-right";
784
+ }
785
+ }
773
786
  var DEFAULT_VIEW_STATE = {
774
787
  latitude: 39.5,
775
788
  longitude: -98.35,
@@ -1175,6 +1188,9 @@ function GeoMap({
1175
1188
  longitude: defaultViewState?.longitude ?? firstCoordinate.longitude,
1176
1189
  zoom: defaultViewState?.zoom ?? calculatedZoom
1177
1190
  });
1191
+ const [dynamicPanelPosition, setDynamicPanelPosition] = React3__namespace.useState(
1192
+ panelPosition
1193
+ );
1178
1194
  React3__namespace.useEffect(() => {
1179
1195
  if (!viewState && !defaultViewState) {
1180
1196
  setUncontrolledViewState({
@@ -1254,6 +1270,19 @@ function GeoMap({
1254
1270
  markerId: marker.id,
1255
1271
  clusterId: marker.clusterId
1256
1272
  });
1273
+ const currentCenter = resolvedViewState || {
1274
+ latitude: firstCoordinate.latitude,
1275
+ longitude: firstCoordinate.longitude
1276
+ };
1277
+ const optimalPosition = getOptimalPanelPosition(
1278
+ marker.latitude,
1279
+ marker.longitude,
1280
+ {
1281
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1282
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1283
+ }
1284
+ );
1285
+ setDynamicPanelPosition(optimalPosition);
1257
1286
  applyViewState({
1258
1287
  latitude: marker.latitude,
1259
1288
  longitude: marker.longitude,
@@ -1261,7 +1290,7 @@ function GeoMap({
1261
1290
  });
1262
1291
  emitSelectionChange({ type: "marker", marker });
1263
1292
  },
1264
- [applyViewState, emitSelectionChange, markerFocusZoom]
1293
+ [applyViewState, emitSelectionChange, markerFocusZoom, resolvedViewState, firstCoordinate]
1265
1294
  );
1266
1295
  const selectCluster = React3__namespace.useCallback(
1267
1296
  (cluster) => {
@@ -1269,6 +1298,19 @@ function GeoMap({
1269
1298
  type: "cluster",
1270
1299
  clusterId: cluster.id
1271
1300
  });
1301
+ const currentCenter = resolvedViewState || {
1302
+ latitude: firstCoordinate.latitude,
1303
+ longitude: firstCoordinate.longitude
1304
+ };
1305
+ const optimalPosition = getOptimalPanelPosition(
1306
+ cluster.latitude,
1307
+ cluster.longitude,
1308
+ {
1309
+ latitude: currentCenter.latitude ?? firstCoordinate.latitude,
1310
+ longitude: currentCenter.longitude ?? firstCoordinate.longitude
1311
+ }
1312
+ );
1313
+ setDynamicPanelPosition(optimalPosition);
1272
1314
  applyViewState({
1273
1315
  latitude: cluster.latitude,
1274
1316
  longitude: cluster.longitude,
@@ -1276,7 +1318,7 @@ function GeoMap({
1276
1318
  });
1277
1319
  emitSelectionChange({ type: "cluster", cluster });
1278
1320
  },
1279
- [applyViewState, clusterFocusZoom, emitSelectionChange]
1321
+ [applyViewState, clusterFocusZoom, emitSelectionChange, resolvedViewState, firstCoordinate]
1280
1322
  );
1281
1323
  const clearSelection = React3__namespace.useCallback(() => {
1282
1324
  setSelection({ type: "none" });
@@ -1563,7 +1605,7 @@ function GeoMap({
1563
1605
  {
1564
1606
  className: cn(
1565
1607
  "pointer-events-none absolute z-30",
1566
- PANEL_POSITION_CLASS[panelPosition]
1608
+ PANEL_POSITION_CLASS[dynamicPanelPosition]
1567
1609
  ),
1568
1610
  style: {
1569
1611
  // Ensure panel can overflow and has higher z-index