@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.
@@ -711,6 +711,19 @@ var PANEL_POSITION_CLASS = {
711
711
  "bottom-left": "bottom-4 left-4",
712
712
  "bottom-right": "bottom-4 right-4"
713
713
  };
714
+ function getOptimalPanelPosition(markerLat, markerLng, mapCenter) {
715
+ const isNorth = markerLat >= mapCenter.latitude;
716
+ const isEast = markerLng >= mapCenter.longitude;
717
+ if (isNorth && isEast) {
718
+ return "bottom-left";
719
+ } else if (isNorth && !isEast) {
720
+ return "bottom-right";
721
+ } else if (!isNorth && isEast) {
722
+ return "top-left";
723
+ } else {
724
+ return "top-right";
725
+ }
726
+ }
714
727
  var DEFAULT_VIEW_STATE = {
715
728
  latitude: 39.5,
716
729
  longitude: -98.35,
@@ -980,9 +993,16 @@ function GeoMap({
980
993
  const updateDimensions = () => {
981
994
  if (containerRef.current) {
982
995
  const rect = containerRef.current.getBoundingClientRect();
983
- setContainerDimensions({
984
- width: rect.width || 800,
985
- height: rect.height || calculatedHeight
996
+ setContainerDimensions((prev) => {
997
+ const newWidth = rect.width || 800;
998
+ const newHeight = rect.height || calculatedHeight;
999
+ if (prev.width === newWidth && prev.height === newHeight) {
1000
+ return prev;
1001
+ }
1002
+ return {
1003
+ width: newWidth,
1004
+ height: newHeight
1005
+ };
986
1006
  });
987
1007
  }
988
1008
  };
@@ -1116,6 +1136,10 @@ function GeoMap({
1116
1136
  longitude: defaultViewState?.longitude ?? firstCoordinate.longitude,
1117
1137
  zoom: defaultViewState?.zoom ?? calculatedZoom
1118
1138
  });
1139
+ const [dynamicPanelPosition, setDynamicPanelPosition] = React3.useState(
1140
+ panelPosition
1141
+ );
1142
+ const viewStateRef = React3.useRef(uncontrolledViewState);
1119
1143
  React3.useEffect(() => {
1120
1144
  if (!viewState && !defaultViewState) {
1121
1145
  setUncontrolledViewState({
@@ -1127,6 +1151,9 @@ function GeoMap({
1127
1151
  }, [firstCoordinate, calculatedZoom, viewState, defaultViewState]);
1128
1152
  const isControlledViewState = viewState !== void 0;
1129
1153
  const resolvedViewState = isControlledViewState ? viewState : uncontrolledViewState;
1154
+ React3.useEffect(() => {
1155
+ viewStateRef.current = resolvedViewState || uncontrolledViewState;
1156
+ }, [resolvedViewState, uncontrolledViewState]);
1130
1157
  const applyViewState = React3.useCallback(
1131
1158
  (nextState) => {
1132
1159
  if (!isControlledViewState) {
@@ -1195,6 +1222,17 @@ function GeoMap({
1195
1222
  markerId: marker.id,
1196
1223
  clusterId: marker.clusterId
1197
1224
  });
1225
+ const currentState = viewStateRef.current;
1226
+ const center = {
1227
+ latitude: currentState.latitude ?? 0,
1228
+ longitude: currentState.longitude ?? 0
1229
+ };
1230
+ const optimalPosition = getOptimalPanelPosition(
1231
+ marker.latitude,
1232
+ marker.longitude,
1233
+ center
1234
+ );
1235
+ setDynamicPanelPosition(optimalPosition);
1198
1236
  applyViewState({
1199
1237
  latitude: marker.latitude,
1200
1238
  longitude: marker.longitude,
@@ -1210,6 +1248,17 @@ function GeoMap({
1210
1248
  type: "cluster",
1211
1249
  clusterId: cluster.id
1212
1250
  });
1251
+ const currentState = viewStateRef.current;
1252
+ const center = {
1253
+ latitude: currentState.latitude ?? 0,
1254
+ longitude: currentState.longitude ?? 0
1255
+ };
1256
+ const optimalPosition = getOptimalPanelPosition(
1257
+ cluster.latitude,
1258
+ cluster.longitude,
1259
+ center
1260
+ );
1261
+ setDynamicPanelPosition(optimalPosition);
1213
1262
  applyViewState({
1214
1263
  latitude: cluster.latitude,
1215
1264
  longitude: cluster.longitude,
@@ -1504,7 +1553,7 @@ function GeoMap({
1504
1553
  {
1505
1554
  className: cn(
1506
1555
  "pointer-events-none absolute z-30",
1507
- PANEL_POSITION_CLASS[panelPosition]
1556
+ PANEL_POSITION_CLASS[dynamicPanelPosition]
1508
1557
  ),
1509
1558
  style: {
1510
1559
  // Ensure panel can overflow and has higher z-index