react-zoom-pan-pinch 4.3.0 → 4.4.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
@@ -451,6 +451,34 @@ var isPanningAllowed = function (contextInstance) {
451
451
  return false;
452
452
  return true;
453
453
  };
454
+ /**
455
+ * Pointer travel in px up to which a press still counts as a click, not a
456
+ * drag: a pan ending within it aligns instead of flinging, and only a pan
457
+ * beyond it swallows the click that follows (#452).
458
+ */
459
+ var CLICK_DISTANCE = 5;
460
+ /**
461
+ * Whether a pan moving by (`dx`, `dy`) would push the content further into a
462
+ * bound it already rests on, judged on the dominant axis. With
463
+ * `panning.releaseAtBounds` such a gesture is handed back to the page or the
464
+ * parent (a carousel, page scroll) instead of being claimed (#509).
465
+ */
466
+ function isPanBlockedAtBounds(contextInstance, dx, dy) {
467
+ var bounds = contextInstance.bounds, setup = contextInstance.setup, state = contextInstance.state;
468
+ if (!bounds || !setup.limitToBounds || (!dx && !dy))
469
+ return false;
470
+ var isHorizontal = Math.abs(dx) >= Math.abs(dy);
471
+ if (isHorizontal ? setup.panning.lockAxisX : setup.panning.lockAxisY) {
472
+ return true;
473
+ }
474
+ var delta = isHorizontal ? dx : dy;
475
+ var position = isHorizontal ? state.positionX : state.positionY;
476
+ var min = isHorizontal ? bounds.minPositionX : bounds.minPositionY;
477
+ var max = isHorizontal ? bounds.maxPositionX : bounds.maxPositionY;
478
+ // Moving the pointer right/down moves the content towards its max bound.
479
+ // Half a pixel of slack: alignment animations settle on float values.
480
+ return delta > 0 ? position >= max - 0.5 : position <= min + 0.5;
481
+ }
454
482
  var getPaddingValue = function (contextInstance, size, explicitScale) {
455
483
  var setup = contextInstance.setup, state = contextInstance.state;
456
484
  var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
@@ -789,7 +817,7 @@ function handlePanningEnd(contextInstance, velocityDisabled) {
789
817
  // A click is not a fling, but it can still land on content left past
790
818
  // the bounds — e.g. grabbed mid return animation, which the press
791
819
  // cancelled. Finish the return (a no-op inside the bounds).
792
- if (dx * dx + dy * dy <= 25) {
820
+ if (dx * dx + dy * dy <= Math.pow(CLICK_DISTANCE, 2)) {
793
821
  handleAlignToBounds(contextInstance);
794
822
  return;
795
823
  }
@@ -936,6 +964,7 @@ var initialSetup = {
936
964
  trackPadPanning: {
937
965
  disabled: true,
938
966
  velocityDisabled: false,
967
+ speed: 1,
939
968
  lockAxisX: false,
940
969
  lockAxisY: false,
941
970
  activationKeys: [],
@@ -949,6 +978,8 @@ var initialSetup = {
949
978
  allowLeftClickPan: true,
950
979
  allowMiddleClickPan: true,
951
980
  allowRightClickPan: true,
981
+ suppressClickAfterPan: true,
982
+ releaseAtBounds: false,
952
983
  activationKeys: [],
953
984
  excluded: [],
954
985
  },
@@ -1006,13 +1037,19 @@ var baseClasses = {
1006
1037
  };
1007
1038
 
1008
1039
  var createState = function (props) {
1009
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1040
+ var _a, _b, _c, _d, _e, _f, _g;
1010
1041
  var minScale = Math.max((_a = props.minScale) !== null && _a !== void 0 ? _a : initialSetup.minScale, 1e-7);
1011
1042
  var maxScale = (_b = props.maxScale) !== null && _b !== void 0 ? _b : initialSetup.maxScale;
1012
1043
  var rawScale = (_c = props.initialScale) !== null && _c !== void 0 ? _c : initialState.scale;
1013
1044
  var scale = Math.min(Math.max(rawScale, minScale), maxScale);
1014
- var positionX = boundLimiter((_d = props.initialPositionX) !== null && _d !== void 0 ? _d : initialState.positionX, (_e = props.minPositionX) !== null && _e !== void 0 ? _e : -Infinity, (_f = props.maxPositionX) !== null && _f !== void 0 ? _f : Infinity, props.minPositionX != null || props.maxPositionX != null);
1015
- var positionY = boundLimiter((_g = props.initialPositionY) !== null && _g !== void 0 ? _g : initialState.positionY, (_h = props.minPositionY) !== null && _h !== void 0 ? _h : -Infinity, (_j = props.maxPositionY) !== null && _j !== void 0 ? _j : Infinity, props.minPositionY != null || props.maxPositionY != null);
1045
+ // A named initial position ("center", "right", …) needs the sizes, so it
1046
+ // starts at 0 and `applyNamedInitialPosition` places it once laid out.
1047
+ var positionX = boundLimiter(typeof props.initialPositionX === "number"
1048
+ ? props.initialPositionX
1049
+ : initialState.positionX, (_d = props.minPositionX) !== null && _d !== void 0 ? _d : -Infinity, (_e = props.maxPositionX) !== null && _e !== void 0 ? _e : Infinity, props.minPositionX != null || props.maxPositionX != null);
1050
+ var positionY = boundLimiter(typeof props.initialPositionY === "number"
1051
+ ? props.initialPositionY
1052
+ : initialState.positionY, (_f = props.minPositionY) !== null && _f !== void 0 ? _f : -Infinity, (_g = props.maxPositionY) !== null && _g !== void 0 ? _g : Infinity, props.minPositionY != null || props.maxPositionY != null);
1016
1053
  return {
1017
1054
  previousScale: scale,
1018
1055
  scale: scale,
@@ -1162,6 +1199,53 @@ function calculateFitToView(contextInstance, mode, minFitScale, maxFitScale) {
1162
1199
  scale = checkZoomBounds(scale, setup.minScale, setup.maxScale, 0, false);
1163
1200
  return getCenterPosition(scale, wrapperComponent, contentComponent);
1164
1201
  }
1202
+ // Share of the free space (wrapper minus scaled content) left before the
1203
+ // content for each named initial position (#349).
1204
+ var NAMED_POSITION_SHARE = {
1205
+ left: 0,
1206
+ top: 0,
1207
+ center: 0.5,
1208
+ right: 1,
1209
+ bottom: 1,
1210
+ };
1211
+ function hasNamedInitialPosition(contextInstance) {
1212
+ var _a = contextInstance.props, initialPositionX = _a.initialPositionX, initialPositionY = _a.initialPositionY;
1213
+ return (typeof initialPositionX === "string" || typeof initialPositionY === "string");
1214
+ }
1215
+ /**
1216
+ * Replaces each axis of `target` whose `initialPositionX/Y` is named
1217
+ * (`"right"`, `"center"`, `"top"`…) with the position that puts the content
1218
+ * there at `target.scale`. Numeric axes are left as they are.
1219
+ */
1220
+ function applyNamedInitialPosition(contextInstance, target) {
1221
+ var wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent, props = contextInstance.props;
1222
+ var initialPositionX = props.initialPositionX, initialPositionY = props.initialPositionY;
1223
+ if (!wrapperComponent || !contentComponent)
1224
+ return target;
1225
+ var place = function (name, wrapperSize, contentSize) { return (wrapperSize - contentSize * target.scale) * NAMED_POSITION_SHARE[name]; };
1226
+ return {
1227
+ scale: target.scale,
1228
+ positionX: typeof initialPositionX === "string"
1229
+ ? place(initialPositionX, wrapperComponent.clientWidth, contentComponent.offsetWidth)
1230
+ : target.positionX,
1231
+ positionY: typeof initialPositionY === "string"
1232
+ ? place(initialPositionY, wrapperComponent.clientHeight, contentComponent.offsetHeight)
1233
+ : target.positionY,
1234
+ };
1235
+ }
1236
+ /**
1237
+ * The scale `resetTransform` returns to: the fitted one with `fitOnInit`,
1238
+ * otherwise `initialScale` (clamped to min/max).
1239
+ */
1240
+ function getResetScale(contextInstance) {
1241
+ var fitOnInit = contextInstance.setup.fitOnInit;
1242
+ if (fitOnInit) {
1243
+ var fitted = calculateFitToView(contextInstance, fitOnInit === "cover" ? "cover" : "contain");
1244
+ if (fitted)
1245
+ return fitted.scale;
1246
+ }
1247
+ return createState(contextInstance.props).scale;
1248
+ }
1165
1249
  function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
1166
1250
  var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent;
1167
1251
  var limitToBounds = setup.limitToBounds, centerOnInit = setup.centerOnInit, fitOnInit = setup.fitOnInit;
@@ -1172,8 +1256,9 @@ function resetTransformations(contextInstance, animationTime, animationType, onR
1172
1256
  // With `fitOnInit` the initial layout is the fitted one, so that is what a
1173
1257
  // reset returns to (#252).
1174
1258
  if (fitOnInit) {
1175
- var fitted = calculateFitToView(contextInstance, fitOnInit === "cover" ? "cover" : "contain");
1176
- if (fitted) {
1259
+ var fit_1 = calculateFitToView(contextInstance, fitOnInit === "cover" ? "cover" : "contain");
1260
+ if (fit_1) {
1261
+ var fitted = applyNamedInitialPosition(contextInstance, fit_1);
1177
1262
  if (scale === fitted.scale &&
1178
1263
  positionX === fitted.positionX &&
1179
1264
  positionY === fitted.positionY) {
@@ -1190,6 +1275,14 @@ function resetTransformations(contextInstance, animationTime, animationType, onR
1190
1275
  targetPositionX = centered.positionX;
1191
1276
  targetPositionY = centered.positionY;
1192
1277
  }
1278
+ // Named initial positions win over the centring on their own axis (#349).
1279
+ var named = applyNamedInitialPosition(contextInstance, {
1280
+ scale: initialTransformation.scale,
1281
+ positionX: targetPositionX,
1282
+ positionY: targetPositionY,
1283
+ });
1284
+ targetPositionX = named.positionX;
1285
+ targetPositionY = named.positionY;
1193
1286
  var newBounds = calculateBounds(contextInstance, initialTransformation.scale);
1194
1287
  var boundedPositions = getMouseBoundedPosition(targetPositionX, targetPositionY, newBounds, limitToBounds, 0, 0, wrapperComponent);
1195
1288
  var newState = {
@@ -1603,6 +1696,39 @@ var cancelTimeout = function (timeout) {
1603
1696
  var roundScaleForTransform = function (scale) {
1604
1697
  return Number.parseFloat(scale.toFixed(8));
1605
1698
  };
1699
+ var STYLE_MARKER = "data-react-zoom-pan-pinch";
1700
+ /**
1701
+ * Adds the library stylesheet once per root the wrapper renders into: the
1702
+ * document `<head>`, or the shadow root itself (#371), where a stylesheet in
1703
+ * the head never applies. `nonce` is set on the `<style>` so a CSP with
1704
+ * `style-src 'nonce-…'` accepts it (#403). A `<style>` already holding the
1705
+ * same CSS is reused, so several viewers (or a re-mount) add one copy.
1706
+ */
1707
+ function injectStylesheet(wrapper, css, nonce) {
1708
+ if (!css || typeof wrapper.getRootNode !== "function")
1709
+ return;
1710
+ var doc = wrapper.ownerDocument;
1711
+ var root = wrapper.getRootNode();
1712
+ // Duck-typed: a ShadowRoot from another window fails `instanceof`.
1713
+ var isShadowRoot = root.nodeType === 11 && "host" in root;
1714
+ var container = isShadowRoot
1715
+ ? root
1716
+ : doc.head;
1717
+ if (!container)
1718
+ return;
1719
+ var existing = container.querySelectorAll("style[".concat(STYLE_MARKER, "]"));
1720
+ for (var index = 0; index < existing.length; index += 1) {
1721
+ if (existing[index].textContent === css)
1722
+ return;
1723
+ }
1724
+ var style = doc.createElement("style");
1725
+ style.setAttribute(STYLE_MARKER, "");
1726
+ if (nonce)
1727
+ style.setAttribute("nonce", nonce);
1728
+ style.textContent = css;
1729
+ // Appended, like style-inject did before (the bundle used to add it at load).
1730
+ container.appendChild(style);
1731
+ }
1606
1732
  var getTransformStyles = function (x, y, scale) {
1607
1733
  // Standard translate prevents blurry svg on the safari
1608
1734
  var s = roundScaleForTransform(scale);
@@ -1919,9 +2045,10 @@ function handleSizeChange(contextInstance) {
1919
2045
 
1920
2046
  var isWheelAllowed = function (contextInstance, event) {
1921
2047
  var _a = contextInstance.setup.wheel, disabled = _a.disabled, wheelDisabled = _a.wheelDisabled, touchPadDisabled = _a.touchPadDisabled, excluded = _a.excluded;
1922
- var isInitialized = contextInstance.isInitialized, isPanning = contextInstance.isPanning;
2048
+ var isInitialized = contextInstance.isInitialized;
1923
2049
  var target = event.target;
1924
- var isAllowed = isInitialized && !isPanning && !disabled && target;
2050
+ // Zooming mid-drag is allowed (#543): `handleWheelZoom` re-anchors the pan.
2051
+ var isAllowed = isInitialized && !disabled && target;
1925
2052
  if (!isAllowed)
1926
2053
  return false;
1927
2054
  // A purely horizontal wheel/swipe (Magic Mouse, trackpad) carries no zoom
@@ -1951,6 +2078,10 @@ var isWheelPanningAllowed = function (contextInstance, event) {
1951
2078
  if (disabled || trackPadPanning.disabled || event.ctrlKey) {
1952
2079
  return false;
1953
2080
  }
2081
+ // A drag owns the position; a wheel pan on top of it would be undone by
2082
+ // the next pointer move.
2083
+ if (contextInstance.isPanning)
2084
+ return false;
1954
2085
  var isAllowed = isWheelAllowed(contextInstance, event);
1955
2086
  // Cannot execute at the same time as wheel zoom
1956
2087
  if (isAllowed)
@@ -1964,6 +2095,36 @@ var isWheelPanningAllowed = function (contextInstance, event) {
1964
2095
  return false;
1965
2096
  return true;
1966
2097
  };
2098
+ // px per line for `deltaMode === 1` (Firefox with a mouse wheel), close to
2099
+ // what browsers scroll a page per line.
2100
+ var WHEEL_LINE_HEIGHT = 16;
2101
+ /**
2102
+ * The distance in px a wheel event pans by (#500):
2103
+ * - line and page deltas (`deltaMode` 1/2) become pixels, so a wheel that
2104
+ * reports "3 lines" moves 48px instead of 3px;
2105
+ * - Shift turns a vertical wheel horizontal where the browser does not do it
2106
+ * already (a browser that does reports it as `deltaX`);
2107
+ * - `speed` scales the result.
2108
+ */
2109
+ function getWheelPanDelta(event, wrapperComponent, speed) {
2110
+ var unitX = 1;
2111
+ var unitY = 1;
2112
+ if (event.deltaMode === 1) {
2113
+ unitX = WHEEL_LINE_HEIGHT;
2114
+ unitY = WHEEL_LINE_HEIGHT;
2115
+ }
2116
+ else if (event.deltaMode === 2) {
2117
+ unitX = wrapperComponent.clientWidth;
2118
+ unitY = wrapperComponent.clientHeight;
2119
+ }
2120
+ var x = event.deltaX * unitX;
2121
+ var y = event.deltaY * unitY;
2122
+ if (event.shiftKey && !x) {
2123
+ x = y;
2124
+ y = 0;
2125
+ }
2126
+ return { x: x * speed, y: y * speed };
2127
+ }
1967
2128
  var getDeltaY = function (event) {
1968
2129
  if (event) {
1969
2130
  return event.deltaY < 0 ? 1 : -1;
@@ -2117,9 +2278,12 @@ var handleWheelZoom = function (contextInstance, event) {
2117
2278
  event.preventDefault();
2118
2279
  }
2119
2280
  event.stopPropagation();
2281
+ var isPanning = contextInstance.isPanning;
2120
2282
  var delta = getDelta(event, null);
2121
2283
  var zoomStep = smooth ? step * Math.abs(event.deltaY) : step;
2122
- var newScale = handleCalculateWheelZoom(contextInstance, delta, zoomStep, !event.ctrlKey);
2284
+ // Mid-drag the scale stays inside min/max: the overshoot would need the
2285
+ // settle animation, which cannot run while the pointer owns the position.
2286
+ var newScale = handleCalculateWheelZoom(contextInstance, delta, zoomStep, !event.ctrlKey || isPanning);
2123
2287
  // if scale not change
2124
2288
  if (scale === newScale)
2125
2289
  return;
@@ -2130,6 +2294,15 @@ var handleWheelZoom = function (contextInstance, event) {
2130
2294
  var _b = handleCalculateZoomPositions(contextInstance, mousePosition.x, mousePosition.y, newScale, bounds, isLimitedToBounds), x = _b.x, y = _b.y;
2131
2295
  contextInstance.previousWheelEvent = event;
2132
2296
  contextInstance.setState(newScale, x, y);
2297
+ // Zoom during a drag (#543): the pan maps pointer → position through
2298
+ // `startCoords`, taken at the old scale. Re-anchor it at the zoomed state,
2299
+ // or the next move snaps the content back. The velocity sample from before
2300
+ // the zoom would read the jump as a fling, so drop it too.
2301
+ if (isPanning) {
2302
+ handlePanningSetup(contextInstance, event);
2303
+ contextInstance.lastMousePosition = null;
2304
+ contextInstance.velocity = null;
2305
+ }
2133
2306
  handleCallback(getContext(contextInstance), event, onWheel);
2134
2307
  handleCallback(getContext(contextInstance), event, onZoom);
2135
2308
  };
@@ -2140,8 +2313,12 @@ var handleWheelStop = function (contextInstance, event) {
2140
2313
  contextInstance.wheelAnimationTimer = setTimeout(function () {
2141
2314
  if (!contextInstance.mounted)
2142
2315
  return;
2143
- handleAlignToScaleBounds(contextInstance, event.x, event.y);
2144
2316
  contextInstance.wheelAnimationTimer = null;
2317
+ // Mid-drag the pan end aligns (and flushes a pending resize) instead:
2318
+ // an animation now would fight the pointer (#543).
2319
+ if (contextInstance.isPanning)
2320
+ return;
2321
+ handleAlignToScaleBounds(contextInstance, event.x, event.y);
2145
2322
  contextInstance.flushResizeAlignment();
2146
2323
  }, wheelAnimationTime);
2147
2324
  // Wheel stop event
@@ -2297,12 +2474,12 @@ var handleDoubleClickResetMode = function (contextInstance, event) {
2297
2474
  });
2298
2475
  handleDoubleClickStop(contextInstance, event);
2299
2476
  };
2300
- function getDoubleClickScale(mode, scale) {
2301
- if (mode === "toggle") {
2302
- return scale === 1 ? 1 : -1;
2303
- }
2477
+ function getDoubleClickScale(mode) {
2304
2478
  return mode === "zoomOut" ? -1 : 1;
2305
2479
  }
2480
+ // Scales closer than this count as "at the reset scale": animations and fits
2481
+ // land on float values.
2482
+ var RESET_SCALE_TOLERANCE = 1e-3;
2306
2483
  function handleDoubleClick(contextInstance, event) {
2307
2484
  var setup = contextInstance.setup, doubleClickStopEventTimer = contextInstance.doubleClickStopEventTimer, state = contextInstance.state, contentComponent = contextInstance.contentComponent;
2308
2485
  var scale = state.scale;
@@ -2315,9 +2492,19 @@ function handleDoubleClick(contextInstance, event) {
2315
2492
  if (mode === "reset") {
2316
2493
  return handleDoubleClickResetMode(contextInstance, event);
2317
2494
  }
2495
+ // `toggle` (#125): at the reset scale (`initialScale`, or the fitted one
2496
+ // with `fitOnInit`) zoom in by `step`; anywhere else go back to the reset
2497
+ // state. It used to compare against 1, so with another initial scale the
2498
+ // first double click zoomed out, and the way back only stepped out.
2499
+ if (mode === "toggle") {
2500
+ var resetScale = getResetScale(contextInstance);
2501
+ if (Math.abs(scale - resetScale) > RESET_SCALE_TOLERANCE) {
2502
+ return handleDoubleClickResetMode(contextInstance, event);
2503
+ }
2504
+ }
2318
2505
  if (!contentComponent)
2319
2506
  return console.error("No ContentComponent found");
2320
- var delta = getDoubleClickScale(mode, contextInstance.state.scale);
2507
+ var delta = getDoubleClickScale(mode);
2321
2508
  var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
2322
2509
  // stop execution when scale didn't change
2323
2510
  if (scale === newScale)
@@ -2371,6 +2558,10 @@ var ZoomPanPinch = /** @class */ (function () {
2371
2558
  this.isWheelPanning = false;
2372
2559
  this.startCoords = null;
2373
2560
  this.panStartPosition = null;
2561
+ // Pointer travel of the running mouse/touch pan: whether it moved at all
2562
+ // (the first move decides `releaseAtBounds`, #509) and whether it went
2563
+ // past the click distance (the click after it is swallowed, #452).
2564
+ this.panGesture = null;
2374
2565
  this.lastTouch = null;
2375
2566
  // pinch helpers
2376
2567
  this.isPinching = false;
@@ -2500,6 +2691,7 @@ var ZoomPanPinch = /** @class */ (function () {
2500
2691
  wrapper.removeEventListener("touchstart", _this.onTouchPanningStart, passive);
2501
2692
  wrapper.removeEventListener("touchmove", _this.onTouchPanning, passive);
2502
2693
  wrapper.removeEventListener("touchend", _this.onTouchPanningStop, passive);
2694
+ wrapper.removeEventListener("scroll", _this.onWrapperScroll, passive);
2503
2695
  };
2504
2696
  this.handleInitializeWrapperEvents = function (wrapper) {
2505
2697
  // Zooming events on wrapper
@@ -2510,12 +2702,31 @@ var ZoomPanPinch = /** @class */ (function () {
2510
2702
  wrapper.addEventListener("touchstart", _this.onTouchPanningStart, passive);
2511
2703
  wrapper.addEventListener("touchmove", _this.onTouchPanning, passive);
2512
2704
  wrapper.addEventListener("touchend", _this.onTouchPanningStop, passive);
2705
+ wrapper.addEventListener("scroll", _this.onWrapperScroll, passive);
2706
+ };
2707
+ // The wrapper clips with `overflow: hidden`, but the browser still scrolls
2708
+ // it to reveal a focused input (the virtual keyboard on mobile) or for
2709
+ // `scrollIntoView`. Nothing in the transform knows about that offset, so
2710
+ // the content stayed shifted and could not be panned back (#280, #477).
2711
+ // Fold the scroll into the position instead: the target stays in view and
2712
+ // the state matches what is on screen.
2713
+ this.onWrapperScroll = function () {
2714
+ var wrapper = _this.wrapperComponent;
2715
+ if (!wrapper)
2716
+ return;
2717
+ var scrollLeft = wrapper.scrollLeft, scrollTop = wrapper.scrollTop;
2718
+ if (!scrollLeft && !scrollTop)
2719
+ return;
2720
+ wrapper.scrollLeft = 0;
2721
+ wrapper.scrollTop = 0;
2722
+ var _a = _this.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
2723
+ _this.setState(scale, positionX - scrollLeft, positionY - scrollTop);
2513
2724
  };
2514
2725
  this.handleInitialize = function () {
2515
2726
  var _a = _this.setup, centerOnInit = _a.centerOnInit, fitOnInit = _a.fitOnInit;
2516
2727
  _this.applyTransformation();
2517
2728
  _this.onInitCallbacks.forEach(function (callback) { return callback(getContext(_this)); });
2518
- if (centerOnInit || fitOnInit) {
2729
+ if (centerOnInit || fitOnInit || hasNamedInitialPosition(_this)) {
2519
2730
  var applied = _this.applyInitialLayout();
2520
2731
  // Content without a size yet (an image still loading) gets its layout
2521
2732
  // applied again by `onSizeChange` once it has one. Once a real layout
@@ -2608,17 +2819,19 @@ var ZoomPanPinch = /** @class */ (function () {
2608
2819
  this.onWheelPanning = function (event) {
2609
2820
  var onPanning = _this.props.onPanning;
2610
2821
  var trackPadPanning = _this.setup.trackPadPanning;
2611
- var lockAxisX = trackPadPanning.lockAxisX, lockAxisY = trackPadPanning.lockAxisY;
2822
+ var lockAxisX = trackPadPanning.lockAxisX, lockAxisY = trackPadPanning.lockAxisY, speed = trackPadPanning.speed;
2823
+ var wrapper = _this.wrapperComponent;
2612
2824
  _this.syncModifierKeys(event);
2613
2825
  var isAllowed = isWheelPanningAllowed(_this, event);
2614
- if (!isAllowed)
2826
+ if (!isAllowed || !wrapper)
2615
2827
  return;
2616
2828
  if (event.cancelable) {
2617
2829
  event.preventDefault();
2618
2830
  }
2619
2831
  event.stopPropagation();
2620
- var deltaX = lockAxisX ? 0 : event.deltaX;
2621
- var deltaY = lockAxisY ? 0 : event.deltaY;
2832
+ var delta = getWheelPanDelta(event, wrapper, speed);
2833
+ var deltaX = lockAxisX ? 0 : delta.x;
2834
+ var deltaY = lockAxisY ? 0 : delta.y;
2622
2835
  if (!deltaX && !deltaY)
2623
2836
  return;
2624
2837
  var _a = _this.setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
@@ -2663,6 +2876,7 @@ var ZoomPanPinch = /** @class */ (function () {
2663
2876
  event.stopPropagation();
2664
2877
  handleCancelAnimation(_this);
2665
2878
  handlePanningStart(_this, event);
2879
+ _this.startPanGesture(event.clientX, event.clientY);
2666
2880
  _this.lockTextSelection();
2667
2881
  _this.focusForKeyboard();
2668
2882
  handleCallback(getContext(_this), event, onPanningStart);
@@ -2685,6 +2899,10 @@ var ZoomPanPinch = /** @class */ (function () {
2685
2899
  var keysPressed = _this.isPressingKeys(_this.setup.panning.activationKeys);
2686
2900
  if (!keysPressed)
2687
2901
  return;
2902
+ if (_this.trackPanGesture(event.clientX, event.clientY)) {
2903
+ _this.onPanningStop(event);
2904
+ return;
2905
+ }
2688
2906
  if (event.cancelable) {
2689
2907
  event.preventDefault();
2690
2908
  }
@@ -2693,15 +2911,73 @@ var ZoomPanPinch = /** @class */ (function () {
2693
2911
  handleCallback(getContext(_this), event, onPanning);
2694
2912
  };
2695
2913
  this.onPanningStop = function (event) {
2696
- var velocityDisabled = _this.setup.panning.velocityDisabled;
2914
+ var _a;
2915
+ var _b = _this.setup.panning, velocityDisabled = _b.velocityDisabled, suppressClickAfterPan = _b.suppressClickAfterPan;
2697
2916
  var onPanningStop = _this.props.onPanningStop;
2698
2917
  if (_this.isPanning) {
2918
+ var isDrag = !!((_a = _this.panGesture) === null || _a === void 0 ? void 0 : _a.isDrag);
2919
+ _this.panGesture = null;
2699
2920
  _this.unlockTextSelection();
2700
2921
  handlePanningEnd(_this, velocityDisabled);
2701
2922
  handleCallback(getContext(_this), event, onPanningStop);
2702
2923
  _this.flushResizeAlignment();
2924
+ // Only a mouseup is followed by a click on the content; a released or
2925
+ // cancelled gesture (mousemove, mouseleave, touch) is not.
2926
+ if (isDrag && suppressClickAfterPan && event.type === "mouseup") {
2927
+ _this.suppressNextClick();
2928
+ }
2703
2929
  }
2704
2930
  };
2931
+ this.startPanGesture = function (clientX, clientY) {
2932
+ _this.panGesture = {
2933
+ startX: clientX,
2934
+ startY: clientY,
2935
+ hasMoved: false,
2936
+ isDrag: false,
2937
+ };
2938
+ };
2939
+ // Records the pointer travel of the running pan. Returns true when this is
2940
+ // the first move, it heads into a bound the content already rests on and
2941
+ // `releaseAtBounds` is on: the caller then ends the pan without claiming
2942
+ // the event, so the page scrolls or a parent takes the drag (#509).
2943
+ this.trackPanGesture = function (clientX, clientY) {
2944
+ var gesture = _this.panGesture;
2945
+ if (!gesture)
2946
+ return false;
2947
+ var dx = clientX - gesture.startX;
2948
+ var dy = clientY - gesture.startY;
2949
+ if (!dx && !dy)
2950
+ return false;
2951
+ var isFirstMove = !gesture.hasMoved;
2952
+ gesture.hasMoved = true;
2953
+ if (dx * dx + dy * dy > Math.pow(CLICK_DISTANCE, 2))
2954
+ gesture.isDrag = true;
2955
+ return (isFirstMove &&
2956
+ _this.setup.panning.releaseAtBounds &&
2957
+ isPanBlockedAtBounds(_this, dx, dy));
2958
+ };
2959
+ // The browser fires `click` right after `mouseup`, in the same task, so a
2960
+ // capture listener on the window that removes itself on the next tick
2961
+ // swallows exactly that click and nothing later (#452).
2962
+ this.suppressNextClick = function () {
2963
+ var _a;
2964
+ var wrapper = _this.wrapperComponent;
2965
+ var win = (_a = wrapper === null || wrapper === void 0 ? void 0 : wrapper.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView;
2966
+ if (!wrapper || !win)
2967
+ return;
2968
+ var swallow = function (event) {
2969
+ var path = typeof event.composedPath === "function" ? event.composedPath() : [];
2970
+ var isInside = path.includes(wrapper) || wrapper.contains(event.target);
2971
+ if (!isInside)
2972
+ return;
2973
+ event.preventDefault();
2974
+ event.stopPropagation();
2975
+ };
2976
+ win.addEventListener("click", swallow, { capture: true, once: true });
2977
+ win.setTimeout(function () {
2978
+ win.removeEventListener("click", swallow, { capture: true });
2979
+ }, 0);
2980
+ };
2705
2981
  /// ///////
2706
2982
  // Pinch
2707
2983
  /// ///////
@@ -2768,6 +3044,7 @@ var ZoomPanPinch = /** @class */ (function () {
2768
3044
  return;
2769
3045
  handleCancelAnimation(_this);
2770
3046
  handlePanningStart(_this, event);
3047
+ _this.startPanGesture(touches[0].clientX, touches[0].clientY);
2771
3048
  _this.lockTextSelection();
2772
3049
  _this.focusForKeyboard();
2773
3050
  handleCallback(getContext(_this), event, onPanningStart);
@@ -2786,11 +3063,16 @@ var ZoomPanPinch = /** @class */ (function () {
2786
3063
  var isAllowed = isPanningAllowed(_this);
2787
3064
  if (!isAllowed)
2788
3065
  return;
3066
+ var touch = event.touches[0];
3067
+ // Not cancelling the first touchmove is what lets the browser scroll.
3068
+ if (_this.trackPanGesture(touch.clientX, touch.clientY)) {
3069
+ _this.onPanningStop(event);
3070
+ return;
3071
+ }
2789
3072
  if (event.cancelable) {
2790
3073
  event.preventDefault();
2791
3074
  }
2792
3075
  event.stopPropagation();
2793
- var touch = event.touches[0];
2794
3076
  handlePanning(_this, touch.clientX, touch.clientY, DeviceType.TOUCH);
2795
3077
  handleCallback(getContext(_this), event, onPanning);
2796
3078
  }
@@ -2830,6 +3112,7 @@ var ZoomPanPinch = /** @class */ (function () {
2830
3112
  if (_this.isPanning) {
2831
3113
  _this.isPanning = false;
2832
3114
  _this.startCoords = null;
3115
+ _this.panGesture = null;
2833
3116
  }
2834
3117
  _this.unlockTextSelection();
2835
3118
  _this.flushResizeAlignment();
@@ -2937,24 +3220,30 @@ var ZoomPanPinch = /** @class */ (function () {
2937
3220
  /// ///////
2938
3221
  // `fitOnInit` wins over `centerOnInit` (a fit is always centred). Falls
2939
3222
  // back to centring while the content has no size yet; `onSizeChange`
2940
- // re-runs this once it does. Returns whether both boxes had a size, i.e.
2941
- // whether this was a real initial layout.
3223
+ // re-runs this once it does. A named `initialPositionX/Y` then places its
3224
+ // own axis (#349). Returns whether both boxes had a size, i.e. whether
3225
+ // this was a real initial layout.
2942
3226
  this.applyInitialLayout = function () {
2943
- var fitOnInit = _this.setup.fitOnInit;
2944
- var _a = _this, wrapperComponent = _a.wrapperComponent, contentComponent = _a.contentComponent;
2945
- var hasSizes = !!wrapperComponent &&
2946
- !!contentComponent &&
2947
- wrapperComponent.clientWidth > 0 &&
3227
+ var _a = _this.setup, fitOnInit = _a.fitOnInit, centerOnInit = _a.centerOnInit;
3228
+ var _b = _this, wrapperComponent = _b.wrapperComponent, contentComponent = _b.contentComponent;
3229
+ if (!wrapperComponent || !contentComponent)
3230
+ return false;
3231
+ var hasSizes = wrapperComponent.clientWidth > 0 &&
2948
3232
  wrapperComponent.clientHeight > 0 &&
2949
3233
  (contentComponent.offsetWidth > 0 || contentComponent.offsetHeight > 0);
2950
- if (fitOnInit) {
2951
- var fitted = calculateFitToView(_this, fitOnInit === "cover" ? "cover" : "contain");
2952
- if (fitted) {
2953
- _this.setState(fitted.scale, fitted.positionX, fitted.positionY);
2954
- return hasSizes;
2955
- }
3234
+ var _c = _this.state, scale = _c.scale, positionX = _c.positionX, positionY = _c.positionY;
3235
+ var target = { scale: scale, positionX: positionX, positionY: positionY };
3236
+ var fitted = fitOnInit
3237
+ ? calculateFitToView(_this, fitOnInit === "cover" ? "cover" : "contain")
3238
+ : null;
3239
+ if (fitted) {
3240
+ target = fitted;
2956
3241
  }
2957
- _this.setCenter();
3242
+ else if (fitOnInit || centerOnInit) {
3243
+ target = getCenterPosition(scale, wrapperComponent, contentComponent);
3244
+ }
3245
+ var placed = applyNamedInitialPosition(_this, target);
3246
+ _this.setState(placed.scale, placed.positionX, placed.positionY);
2958
3247
  return hasSizes;
2959
3248
  };
2960
3249
  this.setCenter = function () {
@@ -3463,40 +3752,14 @@ var MiniMap = function (_a) {
3463
3752
  return (jsxs("div", __assign({}, rest, { ref: mainRef, style: containerStyle, className: "rzpp-mini-map ".concat(rest.className || "").trim() }, { children: [jsx("div", __assign({ style: wrapperStyles, ref: wrapperRef, className: "rzpp-minimap-wrapper ".concat(wrapperClassName || "").trim() }, { children: children })), jsx("svg", __assign({ className: "rzpp-minimap-mask", style: maskStyles, width: width, height: height, viewBox: "0 0 ".concat(width, " ").concat(height), "aria-hidden": "true" }, { children: jsx("path", { ref: maskPathRef, fill: maskColor, fillRule: "evenodd", d: "M0 0H".concat(width, "V").concat(height, "H0Z") }) })), jsx("div", { className: "rzpp-minimap-preview ".concat(previewClassName || "").trim(), ref: previewRef, style: __assign(__assign(__assign({}, previewStyles), { borderColor: borderColor }), previewStyle) })] })));
3464
3753
  };
3465
3754
 
3466
- function styleInject(css, ref) {
3467
- if ( ref === void 0 ) ref = {};
3468
- var insertAt = ref.insertAt;
3469
-
3470
- if (!css || typeof document === 'undefined') { return; }
3471
-
3472
- var head = document.head || document.getElementsByTagName('head')[0];
3473
- var style = document.createElement('style');
3474
- style.type = 'text/css';
3475
-
3476
- if (insertAt === 'top') {
3477
- if (head.firstChild) {
3478
- head.insertBefore(style, head.firstChild);
3479
- } else {
3480
- head.appendChild(style);
3481
- }
3482
- } else {
3483
- head.appendChild(style);
3484
- }
3485
-
3486
- if (style.styleSheet) {
3487
- style.styleSheet.cssText = css;
3488
- } else {
3489
- style.appendChild(document.createTextNode(css));
3490
- }
3491
- }
3492
-
3493
- var css_248z = ".transform-component-module_wrapper__SPB86 {\n position: relative;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n overflow: hidden;\n -webkit-touch-callout: none; /* iOS Safari */\n /* Text stays selectable (#467); the instance applies an inline lock\n only while a pan gesture is active. */\n margin: 0;\n padding: 0;\n transform: translate3d(0, 0, 0);\n}\n.transform-component-module_content__FBWxo {\n display: flex;\n flex-wrap: wrap;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n margin: 0;\n padding: 0;\n transform-origin: 0% 0%;\n}\n.transform-component-module_content__FBWxo img {\n pointer-events: none;\n}\n.transform-component-module_infiniteGrid__Z-aP3 {\n position: absolute;\n inset: 0;\n pointer-events: none;\n background-image: radial-gradient(\n circle,\n rgba(0, 0, 0, 0.12) 1px,\n transparent 1px\n );\n background-size: 20px 20px;\n background-position: 0 0;\n}\n";
3494
- var styles = {"wrapper":"transform-component-module_wrapper__SPB86","content":"transform-component-module_content__FBWxo","infiniteGrid":"transform-component-module_infiniteGrid__Z-aP3"};
3495
- styleInject(css_248z);
3755
+ var transformComponent_module = {"wrapper":"transform-component-module_wrapper__SPB86","content":"transform-component-module_content__FBWxo","infiniteGrid":"transform-component-module_infiniteGrid__Z-aP3"};
3756
+ var stylesheet$1=".transform-component-module_wrapper__SPB86 {\n position: relative;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n overflow: hidden;\n -webkit-touch-callout: none; /* iOS Safari */\n /* Text stays selectable (#467); the instance applies an inline lock\n only while a pan gesture is active. */\n margin: 0;\n padding: 0;\n transform: translate3d(0, 0, 0);\n}\n.transform-component-module_content__FBWxo {\n display: flex;\n flex-wrap: wrap;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n margin: 0;\n padding: 0;\n transform-origin: 0% 0%;\n}\n.transform-component-module_content__FBWxo img {\n pointer-events: none;\n}\n.transform-component-module_infiniteGrid__Z-aP3 {\n position: absolute;\n inset: 0;\n pointer-events: none;\n background-image: radial-gradient(\n circle,\n rgba(0, 0, 0, 0.12) 1px,\n transparent 1px\n );\n background-size: 20px 20px;\n background-position: 0 0;\n}\n";
3496
3757
 
3758
+ var styles = transformComponent_module;
3759
+ var stylesheet = stylesheet$1;
3497
3760
  var TransformComponent = function (_a) {
3498
3761
  var _b;
3499
- var children = _a.children, _c = _a.wrapperClass, wrapperClass = _c === void 0 ? "" : _c, _d = _a.contentClass, contentClass = _d === void 0 ? "" : _d, wrapperStyle = _a.wrapperStyle, contentStyle = _a.contentStyle, _e = _a.wrapperProps, wrapperProps = _e === void 0 ? {} : _e, _f = _a.contentProps, contentProps = _f === void 0 ? {} : _f, _g = _a.infinite, infinite = _g === void 0 ? false : _g;
3762
+ var children = _a.children, _c = _a.wrapperClass, wrapperClass = _c === void 0 ? "" : _c, _d = _a.contentClass, contentClass = _d === void 0 ? "" : _d, wrapperStyle = _a.wrapperStyle, contentStyle = _a.contentStyle, _e = _a.wrapperProps, wrapperProps = _e === void 0 ? {} : _e, _f = _a.contentProps, contentProps = _f === void 0 ? {} : _f, _g = _a.infinite, infinite = _g === void 0 ? false : _g, nonce = _a.nonce, _h = _a.disableStyleInjection, disableStyleInjection = _h === void 0 ? false : _h;
3500
3763
  var instance = useContext(Context);
3501
3764
  var init = instance.init, cleanupWindowEvents = instance.cleanupWindowEvents;
3502
3765
  var wrapperRef = useRef(null);
@@ -3508,6 +3771,10 @@ var TransformComponent = function (_a) {
3508
3771
  useIsomorphicLayoutEffect(function () {
3509
3772
  var wrapper = wrapperRef.current;
3510
3773
  var content = contentRef.current;
3774
+ // Before `init` measures anything: the sizes depend on these rules.
3775
+ if (wrapper !== null && !disableStyleInjection) {
3776
+ injectStylesheet(wrapper, stylesheet, nonce);
3777
+ }
3511
3778
  if (wrapper !== null && content !== null && init) {
3512
3779
  init === null || init === void 0 ? void 0 : init(wrapper, content);
3513
3780
  }
@@ -3788,5 +4055,5 @@ var useZoomPanPinch = function (props) {
3788
4055
  };
3789
4056
  };
3790
4057
 
3791
- export { Context, DeviceType, KeepScale, MiniMap, TransformComponent, TransformWrapper, Virtualize, getCenterPosition, getMatrixTransformStyles, getTransformStyles, roundScaleForTransform, useControls, useTransformComponent, useTransformContext, useTransformEffect, useTransformInit, useZoomPanPinch };
4058
+ export { Context, DeviceType, KeepScale, MiniMap, TransformComponent, TransformWrapper, Virtualize, getCenterPosition, getMatrixTransformStyles, getTransformStyles, injectStylesheet, roundScaleForTransform, useControls, useTransformComponent, useTransformContext, useTransformEffect, useTransformInit, useZoomPanPinch };
3792
4059
  //# sourceMappingURL=index.esm.js.map