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