@page-speed/maps 0.1.9 → 0.2.0

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
@@ -1012,6 +1012,13 @@ function GeoMap({
1012
1012
  IconComponent = FallbackIcon,
1013
1013
  ImgComponent = FallbackImg
1014
1014
  }) {
1015
+ const containerRef = React3__namespace.useRef(null);
1016
+ const [containerDimensions, setContainerDimensions] = React3__namespace.useState({
1017
+ width: 800,
1018
+ // Default width
1019
+ height: 520
1020
+ // Default height
1021
+ });
1015
1022
  const [isMobile, setIsMobile] = React3__namespace.useState(false);
1016
1023
  React3__namespace.useEffect(() => {
1017
1024
  const checkMobile = () => {
@@ -1027,6 +1034,24 @@ function GeoMap({
1027
1034
  }
1028
1035
  return isMobile ? 420 : 520;
1029
1036
  }, [mapSize, isMobile]);
1037
+ React3__namespace.useEffect(() => {
1038
+ if (!containerRef.current) return;
1039
+ const updateDimensions = () => {
1040
+ if (containerRef.current) {
1041
+ const rect = containerRef.current.getBoundingClientRect();
1042
+ setContainerDimensions({
1043
+ width: rect.width || 800,
1044
+ height: rect.height || calculatedHeight
1045
+ });
1046
+ }
1047
+ };
1048
+ updateDimensions();
1049
+ if (typeof ResizeObserver !== "undefined") {
1050
+ const resizeObserver = new ResizeObserver(updateDimensions);
1051
+ resizeObserver.observe(containerRef.current);
1052
+ return () => resizeObserver.disconnect();
1053
+ }
1054
+ }, [calculatedHeight]);
1030
1055
  const normalizedStandaloneMarkers = React3__namespace.useMemo(
1031
1056
  () => markers.map((marker, index) => ({
1032
1057
  ...marker,
@@ -1110,42 +1135,41 @@ function GeoMap({
1110
1135
  longitude: DEFAULT_VIEW_STATE.longitude
1111
1136
  };
1112
1137
  }, [normalizedClusters, normalizedStandaloneMarkers]);
1113
- const calculatedZoom = React3__namespace.useMemo(() => {
1114
- if (normalizedStandaloneMarkers.length + normalizedClusters.length <= 1) {
1115
- return markerFocusZoom;
1116
- }
1117
- const allCoords = [];
1138
+ const zoomCoordinates = React3__namespace.useMemo(() => {
1139
+ const coords = [];
1118
1140
  normalizedStandaloneMarkers.forEach((marker) => {
1119
- allCoords.push({
1120
- latitude: marker.latitude,
1121
- longitude: marker.longitude
1141
+ coords.push({
1142
+ lat: marker.latitude,
1143
+ lng: marker.longitude
1122
1144
  });
1123
1145
  });
1124
1146
  normalizedClusters.forEach((cluster) => {
1125
- allCoords.push({
1126
- latitude: cluster.latitude,
1127
- longitude: cluster.longitude
1147
+ coords.push({
1148
+ lat: cluster.latitude,
1149
+ lng: cluster.longitude
1128
1150
  });
1129
1151
  });
1130
- if (allCoords.length === 0) {
1152
+ return coords;
1153
+ }, [normalizedStandaloneMarkers, normalizedClusters]);
1154
+ const properZoom = useDefaultZoom({
1155
+ coordinates: zoomCoordinates,
1156
+ mapWidth: containerDimensions.width,
1157
+ mapHeight: containerDimensions.height,
1158
+ padding: 80,
1159
+ // Increased padding for better framing
1160
+ maxZoom: 18,
1161
+ minZoom: 1
1162
+ });
1163
+ const calculatedZoom = React3__namespace.useMemo(() => {
1164
+ if (zoomCoordinates.length === 1) {
1165
+ return markerFocusZoom;
1166
+ }
1167
+ if (zoomCoordinates.length === 0) {
1131
1168
  return DEFAULT_VIEW_STATE.zoom;
1132
1169
  }
1133
- const lats = allCoords.map((c) => c.latitude);
1134
- const lngs = allCoords.map((c) => c.longitude);
1135
- const latDiff = Math.max(...lats) - Math.min(...lats);
1136
- const lngDiff = Math.max(...lngs) - Math.min(...lngs);
1137
- const maxDiff = Math.max(latDiff, lngDiff);
1138
- const heightFactor = calculatedHeight / 520;
1139
- const paddingFactor = 0.85;
1140
- if (maxDiff > 10) return Math.max(2, 3 * heightFactor * paddingFactor);
1141
- if (maxDiff > 5) return Math.max(4, 5 * heightFactor * paddingFactor);
1142
- if (maxDiff > 2) return Math.max(6, 7 * heightFactor * paddingFactor);
1143
- if (maxDiff > 1) return Math.max(8, 9 * heightFactor * paddingFactor);
1144
- if (maxDiff > 0.5) return Math.max(9, 10 * heightFactor * paddingFactor);
1145
- if (maxDiff > 0.1) return Math.max(11, 12 * heightFactor * paddingFactor);
1146
- if (maxDiff > 0.01) return Math.max(12, 13 * heightFactor * paddingFactor);
1147
- return Math.max(11, 12 * heightFactor * paddingFactor);
1148
- }, [normalizedClusters, normalizedStandaloneMarkers, markerFocusZoom, calculatedHeight]);
1170
+ const adjustedZoom = properZoom ? properZoom - 0.5 : DEFAULT_VIEW_STATE.zoom;
1171
+ return Math.min(adjustedZoom, 15);
1172
+ }, [properZoom, zoomCoordinates.length, markerFocusZoom]);
1149
1173
  const [uncontrolledViewState, setUncontrolledViewState] = React3__namespace.useState({
1150
1174
  latitude: defaultViewState?.latitude ?? firstCoordinate.latitude,
1151
1175
  longitude: defaultViewState?.longitude ?? firstCoordinate.longitude,
@@ -1471,6 +1495,7 @@ function GeoMap({
1471
1495
  return /* @__PURE__ */ jsxRuntime.jsxs(
1472
1496
  "div",
1473
1497
  {
1498
+ ref: containerRef,
1474
1499
  className: cn(
1475
1500
  "relative rounded-2xl border border-border bg-background",
1476
1501
  // Remove overflow-hidden from outer container to allow panel to overflow