react-zoom-pan-pinch 4.0.7 → 4.1.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/README.md CHANGED
@@ -1,4 +1,9 @@
1
- # 🖼 React Zoom Pan Pinch
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset=".github/assets/logo-lockup-dark.png">
4
+ <img src=".github/assets/logo-lockup-light.png" alt="react-zoom-pan-pinch" width="524" height="96">
5
+ </picture>
6
+ </p>
2
7
 
3
8
  > Super fast and light react npm package for zooming, panning and pinching html
4
9
  > elements in easy way
package/dist/index.cjs.js CHANGED
@@ -104,6 +104,11 @@ var animations = {
104
104
  };
105
105
 
106
106
  /* eslint-disable no-param-reassign */
107
+ var settleAnimation = function (contextInstance) {
108
+ var resolve = contextInstance.animationResolve;
109
+ contextInstance.animationResolve = null;
110
+ resolve === null || resolve === void 0 ? void 0 : resolve();
111
+ };
107
112
  var handleCancelAnimation = function (contextInstance) {
108
113
  if (contextInstance.animationFrame !== null) {
109
114
  cancelAnimationFrame(contextInstance.animationFrame);
@@ -113,10 +118,15 @@ var handleCancelAnimation = function (contextInstance) {
113
118
  contextInstance.isAnimating = false;
114
119
  contextInstance.animation = null;
115
120
  contextInstance.velocity = null;
121
+ // An interrupted animation still settles its promise so callers awaiting
122
+ // `zoomIn()` & co. are never left hanging (#214).
123
+ settleAnimation(contextInstance);
116
124
  };
117
- function handleSetupAnimation(contextInstance, animationName, animationTime, callback) {
118
- if (!contextInstance.mounted)
125
+ function handleSetupAnimation(contextInstance, animationName, animationTime, callback, onFinish) {
126
+ if (!contextInstance.mounted) {
127
+ onFinish === null || onFinish === void 0 ? void 0 : onFinish();
119
128
  return;
129
+ }
120
130
  var startTime = new Date().getTime();
121
131
  var lastStep = 1;
122
132
  // if another animation is active
@@ -138,7 +148,10 @@ function handleSetupAnimation(contextInstance, animationName, animationTime, cal
138
148
  // animation (e.g. from onTransform) is not wiped out afterwards.
139
149
  contextInstance.animation = null;
140
150
  contextInstance.animationFrame = null;
151
+ var resolve = contextInstance.animationResolve;
152
+ contextInstance.animationResolve = null;
141
153
  callback(lastStep);
154
+ resolve === null || resolve === void 0 ? void 0 : resolve();
142
155
  }
143
156
  else {
144
157
  callback(step);
@@ -146,6 +159,7 @@ function handleSetupAnimation(contextInstance, animationName, animationTime, cal
146
159
  }
147
160
  };
148
161
  contextInstance.animation = animation;
162
+ contextInstance.animationResolve = onFinish !== null && onFinish !== void 0 ? onFinish : null;
149
163
  contextInstance.animationFrame = requestAnimationFrame(animation);
150
164
  }
151
165
  function isValidTargetState(targetState) {
@@ -157,10 +171,18 @@ function isValidTargetState(targetState) {
157
171
  }
158
172
  return true;
159
173
  }
174
+ /**
175
+ * Animates the transform to `targetState`.
176
+ *
177
+ * Resolves once the final frame has been applied. It also resolves — never
178
+ * rejects — when the animation is interrupted by another animation or a user
179
+ * gesture, when the instance unmounts, or when the target is invalid, so the
180
+ * returned promise is always safe to await (#214).
181
+ */
160
182
  function animate(contextInstance, targetState, animationTime, animationName) {
161
183
  var isValid = isValidTargetState(targetState);
162
184
  if (!contextInstance.mounted || !isValid)
163
- return;
185
+ return Promise.resolve();
164
186
  var setState = contextInstance.setState;
165
187
  var _a = contextInstance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
166
188
  var scaleDiff = targetState.scale - scale;
@@ -168,8 +190,9 @@ function animate(contextInstance, targetState, animationTime, animationName) {
168
190
  var positionYDiff = targetState.positionY - positionY;
169
191
  if (animationTime === 0) {
170
192
  setState(targetState.scale, targetState.positionX, targetState.positionY);
193
+ return Promise.resolve();
171
194
  }
172
- else {
195
+ return new Promise(function (resolve) {
173
196
  // animation start timestamp
174
197
  handleSetupAnimation(contextInstance, animationName, animationTime, function (step) {
175
198
  if (step !== 1) {
@@ -182,8 +205,8 @@ function animate(contextInstance, targetState, animationTime, animationName) {
182
205
  var newPositionX = positionX + positionXDiff * step;
183
206
  var newPositionY = positionY + positionYDiff * step;
184
207
  setState(newScale, newPositionX, newPositionY);
185
- });
186
- }
208
+ }, resolve);
209
+ });
187
210
  }
188
211
 
189
212
  /* eslint-disable no-param-reassign */
@@ -609,7 +632,14 @@ function handleAlignToBounds(contextInstance, customAnimationTime) {
609
632
  if (isDisabled)
610
633
  return;
611
634
  var targetState = handlePanToBounds(contextInstance);
612
- if (targetState) {
635
+ if (!targetState)
636
+ return;
637
+ // Nothing to align: starting a 200 ms animation towards the current state
638
+ // would only pin the transform and swallow input that arrives meanwhile
639
+ // (#582).
640
+ var _b = contextInstance.state, positionX = _b.positionX, positionY = _b.positionY;
641
+ var isNoop = targetState.positionX === positionX && targetState.positionY === positionY;
642
+ if (!isNoop) {
613
643
  animate(contextInstance, targetState, customAnimationTime !== null && customAnimationTime !== void 0 ? customAnimationTime : animationTime, animationType);
614
644
  }
615
645
  }
@@ -891,46 +921,58 @@ var handleCalculateButtonZoom = function (contextInstance, delta, step) {
891
921
  var newScale = checkZoomBounds(roundNumber(targetScale, 3), minScale, maxScale, size, false);
892
922
  return newScale;
893
923
  };
894
- function handleZoomToViewCenter(contextInstance, delta, step, animationTime, animationType) {
924
+ /**
925
+ * Runs a programmatic zoom: fires `onZoomStart`/`onZoom`, animates to
926
+ * `targetState` and fires `onZoomStop` once the animation time has elapsed.
927
+ * Resolves when the animation has finished (or was interrupted).
928
+ */
929
+ function runZoomAnimation(contextInstance, targetState, animationTime, animationType) {
895
930
  var _a, _b;
896
931
  var wrapperComponent = contextInstance.wrapperComponent;
897
- var _c = contextInstance.state, scale = _c.scale, positionX = _c.positionX, positionY = _c.positionY;
898
- var zoomAnimation = contextInstance.setup.zoomAnimation;
899
- if (!wrapperComponent)
900
- return console.error("No WrapperComponent found");
901
- var effectiveAnimationTime = zoomAnimation.disabled ? 0 : animationTime;
902
- var wrapperWidth = wrapperComponent.offsetWidth;
903
- var wrapperHeight = wrapperComponent.offsetHeight;
904
- var mouseX = (wrapperWidth / 2 - positionX) / scale;
905
- var mouseY = (wrapperHeight / 2 - positionY) / scale;
906
- var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
907
- var targetState = handleZoomToPoint(contextInstance, newScale, mouseX, mouseY);
908
- if (!targetState) {
909
- return console.error("Error during zoom event. New transformation state was not calculated.");
910
- }
911
- var _d = contextInstance.props, onZoomStart = _d.onZoomStart, onZoom = _d.onZoom, onZoomStop = _d.onZoomStop;
932
+ var _c = contextInstance.props, onZoomStart = _c.onZoomStart, onZoom = _c.onZoom, onZoomStop = _c.onZoomStop;
912
933
  var event = new MouseEvent("mousemove", { bubbles: true });
913
934
  var ctx = getContext(contextInstance);
914
935
  handleCallback(ctx, event, onZoomStart);
915
936
  handleCallback(ctx, event, onZoom);
916
- animate(contextInstance, targetState, effectiveAnimationTime, animationType);
917
- var win = (_b = (_a = wrapperComponent.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) !== null && _b !== void 0 ? _b : (typeof window !== "undefined" ? window : null);
937
+ var done = animate(contextInstance, targetState, animationTime, animationType);
938
+ var win = (_b = (_a = wrapperComponent === null || wrapperComponent === void 0 ? void 0 : wrapperComponent.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) !== null && _b !== void 0 ? _b : (typeof window !== "undefined" ? window : null);
918
939
  if (win) {
919
940
  win.setTimeout(function () {
920
941
  if (!contextInstance.mounted)
921
942
  return;
922
943
  handleCallback(getContext(contextInstance), event, onZoomStop);
923
- }, effectiveAnimationTime);
944
+ }, animationTime);
924
945
  }
946
+ return done;
947
+ }
948
+ function handleZoomToViewCenter(contextInstance, delta, step, animationTime, animationType) {
949
+ var wrapperComponent = contextInstance.wrapperComponent;
950
+ var _a = contextInstance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
951
+ var zoomAnimation = contextInstance.setup.zoomAnimation;
952
+ if (!wrapperComponent) {
953
+ console.error("No WrapperComponent found");
954
+ return Promise.resolve();
955
+ }
956
+ var effectiveAnimationTime = zoomAnimation.disabled ? 0 : animationTime;
957
+ var wrapperWidth = wrapperComponent.offsetWidth;
958
+ var wrapperHeight = wrapperComponent.offsetHeight;
959
+ var mouseX = (wrapperWidth / 2 - positionX) / scale;
960
+ var mouseY = (wrapperHeight / 2 - positionY) / scale;
961
+ var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
962
+ var targetState = handleZoomToPoint(contextInstance, newScale, mouseX, mouseY);
963
+ if (!targetState) {
964
+ console.error("Error during zoom event. New transformation state was not calculated.");
965
+ return Promise.resolve();
966
+ }
967
+ return runZoomAnimation(contextInstance, targetState, effectiveAnimationTime, animationType);
925
968
  }
926
969
  function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
927
- var _a, _b;
928
970
  var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent;
929
971
  var limitToBounds = setup.limitToBounds, centerOnInit = setup.centerOnInit;
930
972
  var initialTransformation = createState(contextInstance.props);
931
- var _c = contextInstance.state, scale = _c.scale, positionX = _c.positionX, positionY = _c.positionY;
973
+ var _a = contextInstance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
932
974
  if (!wrapperComponent)
933
- return;
975
+ return Promise.resolve();
934
976
  var targetPositionX = initialTransformation.positionX;
935
977
  var targetPositionY = initialTransformation.positionY;
936
978
  if (centerOnInit && contentComponent) {
@@ -948,26 +990,36 @@ function resetTransformations(contextInstance, animationTime, animationType, onR
948
990
  if (scale === initialTransformation.scale &&
949
991
  positionX === initialTransformation.positionX &&
950
992
  positionY === initialTransformation.positionY) {
951
- return;
993
+ return Promise.resolve();
952
994
  }
953
995
  onResetTransformation === null || onResetTransformation === void 0 ? void 0 : onResetTransformation();
954
- var _d = contextInstance.props, onZoomStart = _d.onZoomStart, onZoom = _d.onZoom, onZoomStop = _d.onZoomStop;
955
- var event = new MouseEvent("mousemove", { bubbles: true });
956
- var ctx = getContext(contextInstance);
957
- handleCallback(ctx, event, onZoomStart);
958
- handleCallback(ctx, event, onZoom);
959
- animate(contextInstance, newState, animationTime, animationType);
960
- var win = (_b = (_a = wrapperComponent.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) !== null && _b !== void 0 ? _b : (typeof window !== "undefined" ? window : null);
961
- if (win) {
962
- win.setTimeout(function () {
963
- if (!contextInstance.mounted)
964
- return;
965
- handleCallback(getContext(contextInstance), event, onZoomStop);
966
- }, animationTime);
996
+ return runZoomAnimation(contextInstance, newState, animationTime, animationType);
997
+ }
998
+ var toRect = function (target) {
999
+ if ("getBoundingClientRect" in target) {
1000
+ var rect = target.getBoundingClientRect();
1001
+ return {
1002
+ x: rect.left,
1003
+ y: rect.top,
1004
+ width: rect.width,
1005
+ height: rect.height,
1006
+ };
967
1007
  }
1008
+ return target;
1009
+ };
1010
+ /**
1011
+ * Smallest client rect containing every node (#388).
1012
+ */
1013
+ function getUnionRect(nodes) {
1014
+ var rects = nodes.map(toRect);
1015
+ var left = Math.min.apply(Math, rects.map(function (r) { return r.x; }));
1016
+ var top = Math.min.apply(Math, rects.map(function (r) { return r.y; }));
1017
+ var right = Math.max.apply(Math, rects.map(function (r) { return r.x + r.width; }));
1018
+ var bottom = Math.max.apply(Math, rects.map(function (r) { return r.y + r.height; }));
1019
+ return { x: left, y: top, width: right - left, height: bottom - top };
968
1020
  }
969
1021
  function getOffset(element, wrapper, content, state) {
970
- var offset = element.getBoundingClientRect();
1022
+ var offset = toRect(element);
971
1023
  var wrapperOffset = wrapper.getBoundingClientRect();
972
1024
  var contentOffset = content.getBoundingClientRect();
973
1025
  var xOff = wrapperOffset.x * state.scale;
@@ -977,7 +1029,14 @@ function getOffset(element, wrapper, content, state) {
977
1029
  y: (offset.y - contentOffset.y + yOff) / state.scale,
978
1030
  };
979
1031
  }
980
- function calculateZoomToNode(contextInstance, node, customZoom, customOffsetX, customOffsetY) {
1032
+ /**
1033
+ * Transform that frames the client-space `rect` in the viewport.
1034
+ *
1035
+ * `customZoom` forces the scale; otherwise the rect is fitted and the fit
1036
+ * scale is capped by `minFitScale`/`maxFitScale` (#515) before the wrapper's
1037
+ * own `minScale`/`maxScale` apply.
1038
+ */
1039
+ function calculateZoomToRect(contextInstance, rect, customZoom, customOffsetX, customOffsetY, minFitScale, maxFitScale) {
981
1040
  if (customOffsetX === void 0) { customOffsetX = 0; }
982
1041
  if (customOffsetY === void 0) { customOffsetY = 0; }
983
1042
  var wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent, state = contextInstance.state;
@@ -985,15 +1044,21 @@ function calculateZoomToNode(contextInstance, node, customZoom, customOffsetX, c
985
1044
  if (!wrapperComponent || !contentComponent)
986
1045
  return state;
987
1046
  var wrapperRect = wrapperComponent.getBoundingClientRect();
988
- var nodeRect = node.getBoundingClientRect();
989
- var nodeOffset = getOffset(node, wrapperComponent, contentComponent, state);
1047
+ var nodeOffset = getOffset(rect, wrapperComponent, contentComponent, state);
990
1048
  var nodeLeft = nodeOffset.x;
991
1049
  var nodeTop = nodeOffset.y;
992
- var nodeWidth = nodeRect.width / state.scale;
993
- var nodeHeight = nodeRect.height / state.scale;
1050
+ var nodeWidth = rect.width / state.scale;
1051
+ var nodeHeight = rect.height / state.scale;
994
1052
  var scaleX = wrapperComponent.offsetWidth / nodeWidth;
995
1053
  var scaleY = wrapperComponent.offsetHeight / nodeHeight;
996
- var newScale = checkZoomBounds(customZoom || Math.min(scaleX, scaleY), minScale, maxScale, 0, false);
1054
+ var targetScale = customZoom || Math.min(scaleX, scaleY);
1055
+ if (maxFitScale !== undefined) {
1056
+ targetScale = Math.min(targetScale, maxFitScale);
1057
+ }
1058
+ if (minFitScale !== undefined) {
1059
+ targetScale = Math.max(targetScale, minFitScale);
1060
+ }
1061
+ var newScale = checkZoomBounds(targetScale, minScale, maxScale, 0, false);
997
1062
  var offsetX = (wrapperRect.width - nodeWidth * newScale) / 2;
998
1063
  var offsetY = (wrapperRect.height - nodeHeight * newScale) / 2;
999
1064
  var newPositionX = (wrapperRect.left - nodeLeft) * newScale + offsetX + customOffsetX;
@@ -1002,13 +1067,41 @@ function calculateZoomToNode(contextInstance, node, customZoom, customOffsetX, c
1002
1067
  var _b = getMouseBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, 0, 0, wrapperComponent), x = _b.x, y = _b.y;
1003
1068
  return { positionX: x, positionY: y, scale: newScale };
1004
1069
  }
1070
+ /**
1071
+ * Client (viewport) coordinates → unscaled content coordinates (#378).
1072
+ * Returns `{ x: 0, y: 0 }` before the wrapper is mounted.
1073
+ */
1074
+ function clientToContentPoint(contextInstance, clientX, clientY) {
1075
+ var wrapperComponent = contextInstance.wrapperComponent, state = contextInstance.state;
1076
+ if (!wrapperComponent)
1077
+ return { x: 0, y: 0 };
1078
+ var rect = wrapperComponent.getBoundingClientRect();
1079
+ return {
1080
+ x: (clientX - rect.left - state.positionX) / state.scale,
1081
+ y: (clientY - rect.top - state.positionY) / state.scale,
1082
+ };
1083
+ }
1084
+ /**
1085
+ * Unscaled content coordinates → client (viewport) coordinates.
1086
+ * Returns `{ x: 0, y: 0 }` before the wrapper is mounted.
1087
+ */
1088
+ function contentToClientPoint(contextInstance, x, y) {
1089
+ var wrapperComponent = contextInstance.wrapperComponent, state = contextInstance.state;
1090
+ if (!wrapperComponent)
1091
+ return { x: 0, y: 0 };
1092
+ var rect = wrapperComponent.getBoundingClientRect();
1093
+ return {
1094
+ x: rect.left + state.positionX + x * state.scale,
1095
+ y: rect.top + state.positionY + y * state.scale,
1096
+ };
1097
+ }
1005
1098
 
1006
1099
  var zoomIn = function (contextInstance) {
1007
1100
  return function (step, animationTime, animationType) {
1008
1101
  if (step === void 0) { step = 0.5; }
1009
1102
  if (animationTime === void 0) { animationTime = 300; }
1010
1103
  if (animationType === void 0) { animationType = "easeOut"; }
1011
- handleZoomToViewCenter(contextInstance, 1, step, animationTime, animationType);
1104
+ return handleZoomToViewCenter(contextInstance, 1, step, animationTime, animationType);
1012
1105
  };
1013
1106
  };
1014
1107
  var zoomOut = function (contextInstance) {
@@ -1016,7 +1109,7 @@ var zoomOut = function (contextInstance) {
1016
1109
  if (step === void 0) { step = 0.5; }
1017
1110
  if (animationTime === void 0) { animationTime = 300; }
1018
1111
  if (animationType === void 0) { animationType = "easeOut"; }
1019
- handleZoomToViewCenter(contextInstance, -1, step, animationTime, animationType);
1112
+ return handleZoomToViewCenter(contextInstance, -1, step, animationTime, animationType);
1020
1113
  };
1021
1114
  };
1022
1115
  var setTransform = function (contextInstance) {
@@ -1026,21 +1119,22 @@ var setTransform = function (contextInstance) {
1026
1119
  var _a = contextInstance.state, positionX = _a.positionX, positionY = _a.positionY, scale = _a.scale;
1027
1120
  var wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent;
1028
1121
  var disabled = contextInstance.setup.disabled;
1029
- if (disabled || !wrapperComponent || !contentComponent)
1030
- return;
1122
+ if (disabled || !wrapperComponent || !contentComponent) {
1123
+ return Promise.resolve();
1124
+ }
1031
1125
  var targetState = {
1032
1126
  positionX: Number.isNaN(newPositionX) ? positionX : newPositionX,
1033
1127
  positionY: Number.isNaN(newPositionY) ? positionY : newPositionY,
1034
1128
  scale: Number.isNaN(newScale) ? scale : newScale,
1035
1129
  };
1036
- animate(contextInstance, targetState, animationTime, animationType);
1130
+ return animate(contextInstance, targetState, animationTime, animationType);
1037
1131
  };
1038
1132
  };
1039
1133
  var resetTransform = function (contextInstance) {
1040
1134
  return function (animationTime, animationType) {
1041
1135
  if (animationTime === void 0) { animationTime = 200; }
1042
1136
  if (animationType === void 0) { animationType = "easeOut"; }
1043
- resetTransformations(contextInstance, animationTime, animationType);
1137
+ return resetTransformations(contextInstance, animationTime, animationType);
1044
1138
  };
1045
1139
  };
1046
1140
  var centerView = function (contextInstance) {
@@ -1050,29 +1144,102 @@ var centerView = function (contextInstance) {
1050
1144
  var state = contextInstance.state, wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent;
1051
1145
  if (wrapperComponent && contentComponent) {
1052
1146
  var targetState = getCenterPosition(scale || state.scale, wrapperComponent, contentComponent);
1053
- animate(contextInstance, targetState, animationTime, animationType);
1147
+ return animate(contextInstance, targetState, animationTime, animationType);
1148
+ }
1149
+ return Promise.resolve();
1150
+ };
1151
+ };
1152
+ /**
1153
+ * Zooms so that the content point currently under the client (viewport)
1154
+ * coordinates `clientX`/`clientY` stays fixed on screen — the same anchoring a
1155
+ * wheel zoom does at the cursor. Useful for click-to-zoom and focal zooms
1156
+ * (#353).
1157
+ */
1158
+ var zoomToPoint = function (contextInstance) {
1159
+ return function (scale, clientX, clientY, animationTime, animationType) {
1160
+ if (animationTime === void 0) { animationTime = 300; }
1161
+ if (animationType === void 0) { animationType = "easeOut"; }
1162
+ var wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent, setup = contextInstance.setup;
1163
+ if (setup.disabled || !wrapperComponent || !contentComponent) {
1164
+ return Promise.resolve();
1054
1165
  }
1166
+ var point = clientToContentPoint(contextInstance, clientX, clientY);
1167
+ var targetState = handleZoomToPoint(contextInstance, scale, point.x, point.y);
1168
+ if (!targetState)
1169
+ return Promise.resolve();
1170
+ var effectiveAnimationTime = setup.zoomAnimation.disabled
1171
+ ? 0
1172
+ : animationTime;
1173
+ return runZoomAnimation(contextInstance, targetState, effectiveAnimationTime, animationType);
1174
+ };
1175
+ };
1176
+ /**
1177
+ * Converts client (viewport) coordinates — e.g. `event.clientX/Y` — into
1178
+ * content coordinates: the unscaled position inside `TransformComponent`
1179
+ * regardless of the current pan and zoom (#378).
1180
+ */
1181
+ var clientToContent = function (contextInstance) {
1182
+ return function (clientX, clientY) {
1183
+ return clientToContentPoint(contextInstance, clientX, clientY);
1184
+ };
1185
+ };
1186
+ /**
1187
+ * Inverse of `clientToContent`: converts unscaled content coordinates into
1188
+ * client (viewport) coordinates for the current transform.
1189
+ */
1190
+ var contentToClient = function (contextInstance) {
1191
+ return function (x, y) {
1192
+ return contentToClientPoint(contextInstance, x, y);
1055
1193
  };
1056
1194
  };
1195
+ var resolveTargets = function (contextInstance, node) {
1196
+ var _a;
1197
+ var wrapperComponent = contextInstance.wrapperComponent;
1198
+ if (!wrapperComponent)
1199
+ return [];
1200
+ // Ids are resolved in the wrapper's own root so this works inside portal
1201
+ // windows and iframes (#290) as well as shadow roots (#371).
1202
+ var rootNode = wrapperComponent.getRootNode();
1203
+ var root = rootNode && "getElementById" in rootNode
1204
+ ? rootNode
1205
+ : (_a = wrapperComponent.ownerDocument) !== null && _a !== void 0 ? _a : (typeof document !== "undefined" ? document : null);
1206
+ var list = Array.isArray(node) ? node : [node];
1207
+ return list
1208
+ .map(function (item) {
1209
+ return typeof item === "string"
1210
+ ? root === null || root === void 0 ? void 0 : root.getElementById(item)
1211
+ : item;
1212
+ })
1213
+ .filter(function (element) {
1214
+ return !!element && wrapperComponent.contains(element);
1215
+ });
1216
+ };
1217
+ /**
1218
+ * Zooms and pans so the target element(s) fill the viewport.
1219
+ *
1220
+ * - `node` may be an element, an element id, or an array of either; with
1221
+ * several targets the union of their boxes is framed (#388).
1222
+ * - The second argument is either the target `scale` (legacy positional
1223
+ * form) or an options object. `minScale`/`maxScale` cap the automatically
1224
+ * computed fit scale (#515).
1225
+ */
1057
1226
  var zoomToElement = function (contextInstance) {
1058
- return function (node, scale, animationTime, animationType, offsetX, offsetY) {
1059
- var _a, _b;
1227
+ return function (node, scaleOrOptions, animationTime, animationType, offsetX, offsetY) {
1228
+ var _a, _b, _c, _d;
1060
1229
  if (animationTime === void 0) { animationTime = 600; }
1061
1230
  if (animationType === void 0) { animationType = "easeOut"; }
1062
1231
  if (offsetX === void 0) { offsetX = 0; }
1063
1232
  if (offsetY === void 0) { offsetY = 0; }
1064
1233
  handleCancelAnimation(contextInstance);
1065
- var wrapperComponent = contextInstance.wrapperComponent;
1066
- // Resolve ids in the wrapper's own document so it works inside portal
1067
- // windows and iframes, not only the top-level document (#290).
1068
- var ownerDocument = (_a = wrapperComponent === null || wrapperComponent === void 0 ? void 0 : wrapperComponent.ownerDocument) !== null && _a !== void 0 ? _a : (typeof document !== "undefined" ? document : null);
1069
- var target = typeof node === "string"
1070
- ? (_b = ownerDocument === null || ownerDocument === void 0 ? void 0 : ownerDocument.getElementById(node)) !== null && _b !== void 0 ? _b : null
1071
- : node;
1072
- if (wrapperComponent && target && wrapperComponent.contains(target)) {
1073
- var targetState = calculateZoomToNode(contextInstance, target, scale, offsetX, offsetY);
1074
- animate(contextInstance, targetState, animationTime, animationType);
1234
+ var options = typeof scaleOrOptions === "object" && scaleOrOptions !== null
1235
+ ? scaleOrOptions
1236
+ : { scale: scaleOrOptions };
1237
+ var targets = resolveTargets(contextInstance, node);
1238
+ if (!contextInstance.wrapperComponent || targets.length === 0) {
1239
+ return Promise.resolve();
1075
1240
  }
1241
+ var targetState = calculateZoomToRect(contextInstance, getUnionRect(targets), options.scale, (_a = options.offsetX) !== null && _a !== void 0 ? _a : offsetX, (_b = options.offsetY) !== null && _b !== void 0 ? _b : offsetY, options.minScale, options.maxScale);
1242
+ return animate(contextInstance, targetState, (_c = options.animationTime) !== null && _c !== void 0 ? _c : animationTime, (_d = options.animationType) !== null && _d !== void 0 ? _d : animationType);
1076
1243
  };
1077
1244
  };
1078
1245
 
@@ -1086,6 +1253,9 @@ var getControls = function (contextInstance) {
1086
1253
  resetTransform: resetTransform(contextInstance),
1087
1254
  centerView: centerView(contextInstance),
1088
1255
  zoomToElement: zoomToElement(contextInstance),
1256
+ zoomToPoint: zoomToPoint(contextInstance),
1257
+ clientToContent: clientToContent(contextInstance),
1258
+ contentToClient: contentToClient(contextInstance),
1089
1259
  };
1090
1260
  };
1091
1261
  var getState = function (contextInstance) {
@@ -1410,8 +1580,13 @@ var wheelStopEventTime = 160;
1410
1580
  var wheelAnimationTime = 100;
1411
1581
  var handleWheelStart = function (contextInstance, event) {
1412
1582
  var _a = contextInstance.props, onWheelStart = _a.onWheelStart, onZoomStart = _a.onZoomStart;
1583
+ // Every wheel event must stop a running animation, not only the first one
1584
+ // of a gesture. The alignment animation scheduled by `handleWheelStop`
1585
+ // starts after `wheelAnimationTime` (100 ms) while `wheelStopEventTimer`
1586
+ // (160 ms) is still pending; guarding the cancel on the stop timer let that
1587
+ // animation overwrite the zoom of any wheel event arriving in between (#582).
1588
+ handleCancelAnimation(contextInstance);
1413
1589
  if (!contextInstance.wheelStopEventTimer) {
1414
- handleCancelAnimation(contextInstance);
1415
1590
  handleCallback(getContext(contextInstance), event, onWheelStart);
1416
1591
  handleCallback(getContext(contextInstance), event, onZoomStart);
1417
1592
  }
@@ -1471,8 +1646,9 @@ var handleWheelStop = function (contextInstance, event) {
1471
1646
  };
1472
1647
  var handleWheelPanningStart = function (contextInstance, event) {
1473
1648
  var _a = contextInstance.props, onWheelStart = _a.onWheelStart, onPanningStart = _a.onPanningStart;
1649
+ // See `handleWheelStart` (#582): cancel on every event, not only the first.
1650
+ handleCancelAnimation(contextInstance);
1474
1651
  if (!contextInstance.wheelStopEventTimer) {
1475
- handleCancelAnimation(contextInstance);
1476
1652
  // Same as a mouse pan start: bounds must reflect the current scale and
1477
1653
  // content size, not the ones measured at mount (#396, #433).
1478
1654
  handleCalculateBounds(contextInstance, contextInstance.state.scale);
@@ -1693,6 +1869,8 @@ var ZoomPanPinch = /** @class */ (function () {
1693
1869
  this.isAnimating = false;
1694
1870
  this.animation = null;
1695
1871
  this.animationFrame = null;
1872
+ // Settles the promise returned by the running programmatic animation.
1873
+ this.animationResolve = null;
1696
1874
  // init helpers
1697
1875
  this.initObserverTimer = null;
1698
1876
  // key press
@@ -2544,7 +2722,10 @@ var TransformComponent = function (_a) {
2544
2722
  sync();
2545
2723
  return instance.onChange(sync);
2546
2724
  }, [infinite, instance]);
2547
- return (jsxRuntime.jsxs("div", __assign({}, wrapperProps, { ref: wrapperRef, className: "".concat(baseClasses.wrapperClass, " ").concat(styles.wrapper, " ").concat(wrapperClass), style: wrapperStyle }, { children: [infinite && (jsxRuntime.jsx("div", { ref: gridRef, className: styles.infiniteGrid, "aria-hidden": true })), jsxRuntime.jsx("div", __assign({}, contentProps, { ref: contentRef, className: "".concat(baseClasses.contentClass, " ").concat(styles.content, " ").concat(contentClass), style: __assign(__assign({}, contentStyle), { transform: getTransformStyles(instance.state.positionX, instance.state.positionY, instance.state.scale) }) }, { children: children }))] })));
2725
+ return (jsxRuntime.jsxs("div", __assign({}, wrapperProps, { ref: wrapperRef, className: "".concat(baseClasses.wrapperClass, " ").concat(styles.wrapper, " ").concat(wrapperClass), style: wrapperStyle }, { children: [infinite && (jsxRuntime.jsx("div", { ref: gridRef, className: styles.infiniteGrid, "aria-hidden": true })), jsxRuntime.jsx("div", __assign({}, contentProps, { ref: contentRef, className: "".concat(baseClasses.contentClass, " ").concat(styles.content, " ").concat(contentClass), style: __assign(__assign({
2726
+ // Inline so the zoom anchor survives environments where the
2727
+ // stylesheet is not applied, e.g. a shadow root (#371, #444).
2728
+ transformOrigin: "0 0" }, contentStyle), { transform: getTransformStyles(instance.state.positionX, instance.state.positionY, instance.state.scale) }) }, { children: children }))] })));
2548
2729
  };
2549
2730
 
2550
2731
  /**
@@ -2723,6 +2904,42 @@ function useTransformComponent(callback) {
2723
2904
  return transformRender;
2724
2905
  }
2725
2906
 
2907
+ var useZoomPanPinch = function (props) {
2908
+ var contentRef = React.useRef(null);
2909
+ var wrapperRef = React.useRef(null);
2910
+ // Lazy so a fresh (immediately discarded) instance is not built on every
2911
+ // render.
2912
+ var instanceRef = React.useRef(null);
2913
+ if (instanceRef.current === null) {
2914
+ instanceRef.current = new ZoomPanPinch(__assign(__assign({}, initialSetup), props));
2915
+ }
2916
+ var instance = instanceRef;
2917
+ var useTransformCallback = React.useRef(function () { });
2918
+ React.useLayoutEffect(function () {
2919
+ var inst = instance.current;
2920
+ if (contentRef.current && wrapperRef.current) {
2921
+ inst.init(wrapperRef.current, contentRef.current);
2922
+ }
2923
+ var unmount = inst.onTransform(function (data) {
2924
+ useTransformCallback.current(data);
2925
+ });
2926
+ return function () {
2927
+ inst.unmount();
2928
+ unmount();
2929
+ };
2930
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2931
+ }, []);
2932
+ var useTransform = function (callback) {
2933
+ useTransformCallback.current = callback;
2934
+ };
2935
+ return {
2936
+ contentRef: contentRef,
2937
+ wrapperRef: wrapperRef,
2938
+ instance: instance,
2939
+ useTransform: useTransform,
2940
+ };
2941
+ };
2942
+
2726
2943
  exports.Context = Context;
2727
2944
  exports.KeepScale = KeepScale;
2728
2945
  exports.MiniMap = MiniMap;
@@ -2738,4 +2955,5 @@ exports.useTransformComponent = useTransformComponent;
2738
2955
  exports.useTransformContext = useTransformContext;
2739
2956
  exports.useTransformEffect = useTransformEffect;
2740
2957
  exports.useTransformInit = useTransformInit;
2958
+ exports.useZoomPanPinch = useZoomPanPinch;
2741
2959
  //# sourceMappingURL=index.cjs.js.map