react-zoom-pan-pinch 4.2.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
@@ -1,4 +1,4 @@
1
- import React, { useLayoutEffect, useEffect, useRef, useImperativeHandle, useContext, useState, useCallback, useMemo } from 'react';
1
+ import React, { useLayoutEffect, useEffect, useRef, useImperativeHandle, useContext, useState, useCallback } from 'react';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
3
 
4
4
  /**
@@ -280,6 +280,61 @@ var calculateBounds = function (contextInstance, newScale) {
280
280
  }
281
281
  return bounds;
282
282
  };
283
+ function clamp(v, min, max) {
284
+ return Math.max(min, Math.min(v, max));
285
+ }
286
+ // Based on @aholachek ;)
287
+ // https://twitter.com/chpwn/status/285540192096497664
288
+ // iOS constant = 0.55
289
+ // https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
290
+ function rubberband(distance, dimension, constant) {
291
+ if (dimension === 0 || Math.abs(dimension) === Infinity)
292
+ return Math.pow(distance, (constant * 5));
293
+ return (distance * dimension * constant) / (dimension + constant * distance);
294
+ }
295
+ /**
296
+ * Furthest a grabbed position is traced back along the rubber band curve, as
297
+ * a fraction of the padding. The curve never reaches the padding itself, so
298
+ * a position at (or past) it has no pointer distance to map back to.
299
+ */
300
+ var MAX_RUBBER_BAND_RATIO = 0.9;
301
+ /**
302
+ * Resistance of the rubber band, the constant UIScrollView uses: past a bound
303
+ * the content moves at 0.55× the pointer and slows down from there.
304
+ */
305
+ var RUBBER_BAND_CONSTANT = 0.55;
306
+ /**
307
+ * Where a pan that was dragged past a bound ends up: `(1 - 1 / (x * c / d + 1)) * d`
308
+ * with `d` = `padding`. It trails the pointer from the first pixel past the
309
+ * bound and approaches — never reaches — `padding` px past it.
310
+ */
311
+ function getRubberBandPosition(position, min, max, padding) {
312
+ if (padding <= 0)
313
+ return clamp(position, min, max);
314
+ if (position < min)
315
+ return min - rubberband(min - position, padding, RUBBER_BAND_CONSTANT);
316
+ if (position > max)
317
+ return max + rubberband(position - max, padding, RUBBER_BAND_CONSTANT);
318
+ return position;
319
+ }
320
+ /**
321
+ * Inverse of `getRubberBandPosition`: the pointer position that puts the
322
+ * content at `position`. Lets a pan that starts inside the padding (grabbed
323
+ * mid return animation, trackpad deltas) continue from where the content is.
324
+ */
325
+ function getRubberBandSource(position, min, max, padding) {
326
+ if (padding <= 0)
327
+ return position;
328
+ var stretch = function (offset) {
329
+ var limited = Math.min(offset, padding * MAX_RUBBER_BAND_RATIO);
330
+ return (limited * padding) / (RUBBER_BAND_CONSTANT * (padding - limited));
331
+ };
332
+ if (position < min)
333
+ return min - stretch(min - position);
334
+ if (position > max)
335
+ return max + stretch(position - max);
336
+ return position;
337
+ }
283
338
  /**
284
339
  * Keeps value between given bounds, used for limiting view to given boundaries
285
340
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -314,6 +369,23 @@ function getMouseBoundedPosition(positionX, positionY, bounds, limitToBounds, pa
314
369
  var y = boundLimiter(positionY, minPositionY - paddingY, maxPositionY + paddingY, limitToBounds);
315
370
  return { x: x, y: y };
316
371
  }
372
+ /**
373
+ * Bounded position for a pan gesture: free inside the bounds and, inside the
374
+ * padding around them, rubber banded (`rubberBand`) or 1:1 up to its edge.
375
+ */
376
+ function getPanBoundedPosition(positionX, positionY, bounds, limitToBounds, paddingX, paddingY, rubberBand) {
377
+ var minPositionX = bounds.minPositionX, minPositionY = bounds.minPositionY, maxPositionX = bounds.maxPositionX, maxPositionY = bounds.maxPositionY;
378
+ var limit = function (position, min, max, padding) {
379
+ if (limitToBounds && rubberBand && padding > 0) {
380
+ return roundNumber(getRubberBandPosition(position, min, max, padding), 2);
381
+ }
382
+ return boundLimiter(position, min - padding, max + padding, limitToBounds);
383
+ };
384
+ return {
385
+ x: limit(positionX, minPositionX, maxPositionX, paddingX),
386
+ y: limit(positionY, minPositionY, maxPositionY, paddingY),
387
+ };
388
+ }
317
389
 
318
390
  function handleCalculateZoomPositions(contextInstance, mouseX, mouseY, newScale, bounds, limitToBounds) {
319
391
  var _a = contextInstance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
@@ -379,8 +451,73 @@ var isPanningAllowed = function (contextInstance) {
379
451
  return false;
380
452
  return true;
381
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
+ }
482
+ var getPaddingValue = function (contextInstance, size, explicitScale) {
483
+ var setup = contextInstance.setup, state = contextInstance.state;
484
+ var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
485
+ var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
486
+ if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
487
+ return size;
488
+ }
489
+ return 0;
490
+ };
491
+ /**
492
+ * Dragging past the bounds stretches like a rubber band only when the content
493
+ * is going to return to them on release (`autoAlignment`). Without that return
494
+ * the padding is a plain area the content can be left in, so it follows the
495
+ * pointer 1:1 up to its edge.
496
+ */
497
+ function isRubberBandEnabled(contextInstance) {
498
+ var _a = contextInstance.setup, limitToBounds = _a.limitToBounds, autoAlignment = _a.autoAlignment;
499
+ return limitToBounds && !autoAlignment.disabled;
500
+ }
501
+ /**
502
+ * The position a pan continues from. Inside the bounds that is the content
503
+ * position; inside the padding the content trails the pointer (rubber band),
504
+ * so the pointer's own position is traced back from it — otherwise the first
505
+ * move of a pan started there would make the content jump.
506
+ */
507
+ function getPanSourcePosition(contextInstance) {
508
+ var bounds = contextInstance.bounds, setup = contextInstance.setup, state = contextInstance.state;
509
+ var positionX = state.positionX, positionY = state.positionY;
510
+ if (!bounds || !isRubberBandEnabled(contextInstance)) {
511
+ return { x: positionX, y: positionY };
512
+ }
513
+ var _a = setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
514
+ return {
515
+ x: getRubberBandSource(positionX, bounds.minPositionX, bounds.maxPositionX, getPaddingValue(contextInstance, sizeX)),
516
+ y: getRubberBandSource(positionY, bounds.minPositionY, bounds.maxPositionY, getPaddingValue(contextInstance, sizeY)),
517
+ };
518
+ }
382
519
  var handlePanningSetup = function (contextInstance, event) {
383
- var _a = contextInstance.state, positionX = _a.positionX, positionY = _a.positionY;
520
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
384
521
  contextInstance.isPanning = true;
385
522
  // Panning with mouse
386
523
  var x = event.clientX;
@@ -389,7 +526,7 @@ var handlePanningSetup = function (contextInstance, event) {
389
526
  };
390
527
  var handleTouchPanningSetup = function (contextInstance, event) {
391
528
  var touches = event.touches;
392
- var _a = contextInstance.state, positionX = _a.positionX, positionY = _a.positionY;
529
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
393
530
  contextInstance.isPanning = true;
394
531
  // Panning with touch
395
532
  var oneFingerTouch = touches.length === 1;
@@ -430,14 +567,16 @@ function handleNewPosition(contextInstance, newPositionX, newPositionY, paddingV
430
567
  (newPositionX === positionX && newPositionY === positionY)) {
431
568
  return;
432
569
  }
433
- var _b = getMouseBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, paddingValueX, paddingValueY, wrapperComponent), x = _b.x, y = _b.y;
570
+ var _b = getPanBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, paddingValueX, paddingValueY, isRubberBandEnabled(contextInstance)), x = _b.x, y = _b.y;
434
571
  contextInstance.setState(scale, x, y);
435
572
  }
436
573
  var getPanningClientPosition = function (contextInstance, clientX, clientY) {
437
- var startCoords = contextInstance.startCoords, state = contextInstance.state;
574
+ var startCoords = contextInstance.startCoords;
438
575
  var panning = contextInstance.setup.panning;
439
576
  var lockAxisX = panning.lockAxisX, lockAxisY = panning.lockAxisY;
440
- var positionX = state.positionX, positionY = state.positionY;
577
+ // A locked axis keeps its place: the source maps back onto the position
578
+ // it has now, also when it rests inside the padding.
579
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
441
580
  if (!startCoords) {
442
581
  return { x: positionX, y: positionY };
443
582
  }
@@ -447,15 +586,6 @@ var getPanningClientPosition = function (contextInstance, clientX, clientY) {
447
586
  var newPositionY = lockAxisY ? positionY : mouseY;
448
587
  return { x: newPositionX, y: newPositionY };
449
588
  };
450
- var getPaddingValue = function (contextInstance, size, explicitScale) {
451
- var setup = contextInstance.setup, state = contextInstance.state;
452
- var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
453
- var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
454
- if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
455
- return size;
456
- }
457
- return 0;
458
- };
459
589
 
460
590
  var DeviceType;
461
591
  (function (DeviceType) {
@@ -589,8 +719,15 @@ function handleVelocityPanning(contextInstance) {
589
719
  var finalAnimationTime = Math.max(moveAnimationTime, alignAnimationTime);
590
720
  var paddingValueX = getPaddingValue(contextInstance, sizeX);
591
721
  var paddingValueY = getPaddingValue(contextInstance, sizeY);
592
- var paddingX = (paddingValueX * wrapperComponent.clientWidth) / 100;
593
- var paddingY = (paddingValueY * wrapperComponent.clientHeight) / 100;
722
+ // A fling released inside the rubber band stays within the same stretch a
723
+ // drag could reach (`padding` px), not a share of the wrapper size.
724
+ var isRubberBand = isRubberBandEnabled(contextInstance);
725
+ var paddingX = isRubberBand
726
+ ? paddingValueX
727
+ : (paddingValueX * wrapperComponent.clientWidth) / 100;
728
+ var paddingY = isRubberBand
729
+ ? paddingValueY
730
+ : (paddingValueY * wrapperComponent.clientHeight) / 100;
594
731
  var maxTargetX = maxPositionX + paddingX;
595
732
  var minTargetX = minPositionX - paddingX;
596
733
  var maxTargetY = maxPositionY + paddingY;
@@ -677,8 +814,13 @@ function handlePanningEnd(contextInstance, velocityDisabled) {
677
814
  if (start) {
678
815
  var dx = positionX - start.x;
679
816
  var dy = positionY - start.y;
680
- if (dx * dx + dy * dy <= 25)
817
+ // A click is not a fling, but it can still land on content left past
818
+ // the bounds — e.g. grabbed mid return animation, which the press
819
+ // cancelled. Finish the return (a no-op inside the bounds).
820
+ if (dx * dx + dy * dy <= Math.pow(CLICK_DISTANCE, 2)) {
821
+ handleAlignToBounds(contextInstance);
681
822
  return;
823
+ }
682
824
  }
683
825
  contextInstance.isAnimating = false;
684
826
  contextInstance.animation = null;
@@ -822,6 +964,7 @@ var initialSetup = {
822
964
  trackPadPanning: {
823
965
  disabled: true,
824
966
  velocityDisabled: false,
967
+ speed: 1,
825
968
  lockAxisX: false,
826
969
  lockAxisY: false,
827
970
  activationKeys: [],
@@ -835,6 +978,8 @@ var initialSetup = {
835
978
  allowLeftClickPan: true,
836
979
  allowMiddleClickPan: true,
837
980
  allowRightClickPan: true,
981
+ suppressClickAfterPan: true,
982
+ releaseAtBounds: false,
838
983
  activationKeys: [],
839
984
  excluded: [],
840
985
  },
@@ -892,13 +1037,19 @@ var baseClasses = {
892
1037
  };
893
1038
 
894
1039
  var createState = function (props) {
895
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1040
+ var _a, _b, _c, _d, _e, _f, _g;
896
1041
  var minScale = Math.max((_a = props.minScale) !== null && _a !== void 0 ? _a : initialSetup.minScale, 1e-7);
897
1042
  var maxScale = (_b = props.maxScale) !== null && _b !== void 0 ? _b : initialSetup.maxScale;
898
1043
  var rawScale = (_c = props.initialScale) !== null && _c !== void 0 ? _c : initialState.scale;
899
1044
  var scale = Math.min(Math.max(rawScale, minScale), maxScale);
900
- 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);
901
- 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);
902
1053
  return {
903
1054
  previousScale: scale,
904
1055
  scale: scale,
@@ -1048,6 +1199,53 @@ function calculateFitToView(contextInstance, mode, minFitScale, maxFitScale) {
1048
1199
  scale = checkZoomBounds(scale, setup.minScale, setup.maxScale, 0, false);
1049
1200
  return getCenterPosition(scale, wrapperComponent, contentComponent);
1050
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
+ }
1051
1249
  function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
1052
1250
  var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent;
1053
1251
  var limitToBounds = setup.limitToBounds, centerOnInit = setup.centerOnInit, fitOnInit = setup.fitOnInit;
@@ -1058,8 +1256,9 @@ function resetTransformations(contextInstance, animationTime, animationType, onR
1058
1256
  // With `fitOnInit` the initial layout is the fitted one, so that is what a
1059
1257
  // reset returns to (#252).
1060
1258
  if (fitOnInit) {
1061
- var fitted = calculateFitToView(contextInstance, fitOnInit === "cover" ? "cover" : "contain");
1062
- if (fitted) {
1259
+ var fit_1 = calculateFitToView(contextInstance, fitOnInit === "cover" ? "cover" : "contain");
1260
+ if (fit_1) {
1261
+ var fitted = applyNamedInitialPosition(contextInstance, fit_1);
1063
1262
  if (scale === fitted.scale &&
1064
1263
  positionX === fitted.positionX &&
1065
1264
  positionY === fitted.positionY) {
@@ -1076,6 +1275,14 @@ function resetTransformations(contextInstance, animationTime, animationType, onR
1076
1275
  targetPositionX = centered.positionX;
1077
1276
  targetPositionY = centered.positionY;
1078
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;
1079
1286
  var newBounds = calculateBounds(contextInstance, initialTransformation.scale);
1080
1287
  var boundedPositions = getMouseBoundedPosition(targetPositionX, targetPositionY, newBounds, limitToBounds, 0, 0, wrapperComponent);
1081
1288
  var newState = {
@@ -1489,6 +1696,39 @@ var cancelTimeout = function (timeout) {
1489
1696
  var roundScaleForTransform = function (scale) {
1490
1697
  return Number.parseFloat(scale.toFixed(8));
1491
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
+ }
1492
1732
  var getTransformStyles = function (x, y, scale) {
1493
1733
  // Standard translate prevents blurry svg on the safari
1494
1734
  var s = roundScaleForTransform(scale);
@@ -1517,16 +1757,6 @@ var getCenterPosition = function (scale, wrapperComponent, contentComponent) {
1517
1757
  };
1518
1758
  };
1519
1759
 
1520
- function assignRef(ref, value) {
1521
- if (ref == null)
1522
- return;
1523
- if (typeof ref === "function") {
1524
- ref(value);
1525
- }
1526
- else {
1527
- ref.current = value;
1528
- }
1529
- }
1530
1760
  function mergeRefs(refs) {
1531
1761
  return function (value) {
1532
1762
  refs.forEach(function (ref) {
@@ -1815,9 +2045,10 @@ function handleSizeChange(contextInstance) {
1815
2045
 
1816
2046
  var isWheelAllowed = function (contextInstance, event) {
1817
2047
  var _a = contextInstance.setup.wheel, disabled = _a.disabled, wheelDisabled = _a.wheelDisabled, touchPadDisabled = _a.touchPadDisabled, excluded = _a.excluded;
1818
- var isInitialized = contextInstance.isInitialized, isPanning = contextInstance.isPanning;
2048
+ var isInitialized = contextInstance.isInitialized;
1819
2049
  var target = event.target;
1820
- var isAllowed = isInitialized && !isPanning && !disabled && target;
2050
+ // Zooming mid-drag is allowed (#543): `handleWheelZoom` re-anchors the pan.
2051
+ var isAllowed = isInitialized && !disabled && target;
1821
2052
  if (!isAllowed)
1822
2053
  return false;
1823
2054
  // A purely horizontal wheel/swipe (Magic Mouse, trackpad) carries no zoom
@@ -1847,6 +2078,10 @@ var isWheelPanningAllowed = function (contextInstance, event) {
1847
2078
  if (disabled || trackPadPanning.disabled || event.ctrlKey) {
1848
2079
  return false;
1849
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;
1850
2085
  var isAllowed = isWheelAllowed(contextInstance, event);
1851
2086
  // Cannot execute at the same time as wheel zoom
1852
2087
  if (isAllowed)
@@ -1860,6 +2095,36 @@ var isWheelPanningAllowed = function (contextInstance, event) {
1860
2095
  return false;
1861
2096
  return true;
1862
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
+ }
1863
2128
  var getDeltaY = function (event) {
1864
2129
  if (event) {
1865
2130
  return event.deltaY < 0 ? 1 : -1;
@@ -2013,9 +2278,12 @@ var handleWheelZoom = function (contextInstance, event) {
2013
2278
  event.preventDefault();
2014
2279
  }
2015
2280
  event.stopPropagation();
2281
+ var isPanning = contextInstance.isPanning;
2016
2282
  var delta = getDelta(event, null);
2017
2283
  var zoomStep = smooth ? step * Math.abs(event.deltaY) : step;
2018
- 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);
2019
2287
  // if scale not change
2020
2288
  if (scale === newScale)
2021
2289
  return;
@@ -2026,6 +2294,15 @@ var handleWheelZoom = function (contextInstance, event) {
2026
2294
  var _b = handleCalculateZoomPositions(contextInstance, mousePosition.x, mousePosition.y, newScale, bounds, isLimitedToBounds), x = _b.x, y = _b.y;
2027
2295
  contextInstance.previousWheelEvent = event;
2028
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
+ }
2029
2306
  handleCallback(getContext(contextInstance), event, onWheel);
2030
2307
  handleCallback(getContext(contextInstance), event, onZoom);
2031
2308
  };
@@ -2036,8 +2313,12 @@ var handleWheelStop = function (contextInstance, event) {
2036
2313
  contextInstance.wheelAnimationTimer = setTimeout(function () {
2037
2314
  if (!contextInstance.mounted)
2038
2315
  return;
2039
- handleAlignToScaleBounds(contextInstance, event.x, event.y);
2040
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);
2041
2322
  contextInstance.flushResizeAlignment();
2042
2323
  }, wheelAnimationTime);
2043
2324
  // Wheel stop event
@@ -2193,12 +2474,12 @@ var handleDoubleClickResetMode = function (contextInstance, event) {
2193
2474
  });
2194
2475
  handleDoubleClickStop(contextInstance, event);
2195
2476
  };
2196
- function getDoubleClickScale(mode, scale) {
2197
- if (mode === "toggle") {
2198
- return scale === 1 ? 1 : -1;
2199
- }
2477
+ function getDoubleClickScale(mode) {
2200
2478
  return mode === "zoomOut" ? -1 : 1;
2201
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;
2202
2483
  function handleDoubleClick(contextInstance, event) {
2203
2484
  var setup = contextInstance.setup, doubleClickStopEventTimer = contextInstance.doubleClickStopEventTimer, state = contextInstance.state, contentComponent = contextInstance.contentComponent;
2204
2485
  var scale = state.scale;
@@ -2211,9 +2492,19 @@ function handleDoubleClick(contextInstance, event) {
2211
2492
  if (mode === "reset") {
2212
2493
  return handleDoubleClickResetMode(contextInstance, event);
2213
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
+ }
2214
2505
  if (!contentComponent)
2215
2506
  return console.error("No ContentComponent found");
2216
- var delta = getDoubleClickScale(mode, contextInstance.state.scale);
2507
+ var delta = getDoubleClickScale(mode);
2217
2508
  var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
2218
2509
  // stop execution when scale didn't change
2219
2510
  if (scale === newScale)
@@ -2267,6 +2558,10 @@ var ZoomPanPinch = /** @class */ (function () {
2267
2558
  this.isWheelPanning = false;
2268
2559
  this.startCoords = null;
2269
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;
2270
2565
  this.lastTouch = null;
2271
2566
  // pinch helpers
2272
2567
  this.isPinching = false;
@@ -2396,6 +2691,7 @@ var ZoomPanPinch = /** @class */ (function () {
2396
2691
  wrapper.removeEventListener("touchstart", _this.onTouchPanningStart, passive);
2397
2692
  wrapper.removeEventListener("touchmove", _this.onTouchPanning, passive);
2398
2693
  wrapper.removeEventListener("touchend", _this.onTouchPanningStop, passive);
2694
+ wrapper.removeEventListener("scroll", _this.onWrapperScroll, passive);
2399
2695
  };
2400
2696
  this.handleInitializeWrapperEvents = function (wrapper) {
2401
2697
  // Zooming events on wrapper
@@ -2406,12 +2702,31 @@ var ZoomPanPinch = /** @class */ (function () {
2406
2702
  wrapper.addEventListener("touchstart", _this.onTouchPanningStart, passive);
2407
2703
  wrapper.addEventListener("touchmove", _this.onTouchPanning, passive);
2408
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);
2409
2724
  };
2410
2725
  this.handleInitialize = function () {
2411
2726
  var _a = _this.setup, centerOnInit = _a.centerOnInit, fitOnInit = _a.fitOnInit;
2412
2727
  _this.applyTransformation();
2413
2728
  _this.onInitCallbacks.forEach(function (callback) { return callback(getContext(_this)); });
2414
- if (centerOnInit || fitOnInit) {
2729
+ if (centerOnInit || fitOnInit || hasNamedInitialPosition(_this)) {
2415
2730
  var applied = _this.applyInitialLayout();
2416
2731
  // Content without a size yet (an image still loading) gets its layout
2417
2732
  // applied again by `onSizeChange` once it has one. Once a real layout
@@ -2504,26 +2819,30 @@ var ZoomPanPinch = /** @class */ (function () {
2504
2819
  this.onWheelPanning = function (event) {
2505
2820
  var onPanning = _this.props.onPanning;
2506
2821
  var trackPadPanning = _this.setup.trackPadPanning;
2507
- var lockAxisX = trackPadPanning.lockAxisX, lockAxisY = trackPadPanning.lockAxisY;
2822
+ var lockAxisX = trackPadPanning.lockAxisX, lockAxisY = trackPadPanning.lockAxisY, speed = trackPadPanning.speed;
2823
+ var wrapper = _this.wrapperComponent;
2508
2824
  _this.syncModifierKeys(event);
2509
2825
  var isAllowed = isWheelPanningAllowed(_this, event);
2510
- if (!isAllowed)
2826
+ if (!isAllowed || !wrapper)
2511
2827
  return;
2512
2828
  if (event.cancelable) {
2513
2829
  event.preventDefault();
2514
2830
  }
2515
2831
  event.stopPropagation();
2516
- var _a = _this.state, positionX = _a.positionX, positionY = _a.positionY;
2517
- var mouseX = positionX - event.deltaX;
2518
- var mouseY = positionY - event.deltaY;
2519
- var newPositionX = lockAxisX ? positionX : mouseX;
2520
- var newPositionY = lockAxisY ? positionY : mouseY;
2521
- var _b = _this.setup.autoAlignment, sizeX = _b.sizeX, sizeY = _b.sizeY;
2832
+ var delta = getWheelPanDelta(event, wrapper, speed);
2833
+ var deltaX = lockAxisX ? 0 : delta.x;
2834
+ var deltaY = lockAxisY ? 0 : delta.y;
2835
+ if (!deltaX && !deltaY)
2836
+ return;
2837
+ var _a = _this.setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
2522
2838
  var paddingValueX = getPaddingValue(_this, sizeX);
2523
2839
  var paddingValueY = getPaddingValue(_this, sizeY);
2524
- if (newPositionX === positionX && newPositionY === positionY)
2525
- return;
2526
2840
  handleWheelPanningStart(_this, event);
2841
+ // Deltas add up on the pointer's side of the rubber band, the same way
2842
+ // a drag does, so the content slows down towards the padding limit.
2843
+ var source = getPanSourcePosition(_this);
2844
+ var newPositionX = source.x - deltaX;
2845
+ var newPositionY = source.y - deltaY;
2527
2846
  handleNewPosition(_this, newPositionX, newPositionY, paddingValueX, paddingValueY);
2528
2847
  handleCallback(getContext(_this), event, onPanning);
2529
2848
  handleWheelPanningStop(_this, event);
@@ -2557,6 +2876,7 @@ var ZoomPanPinch = /** @class */ (function () {
2557
2876
  event.stopPropagation();
2558
2877
  handleCancelAnimation(_this);
2559
2878
  handlePanningStart(_this, event);
2879
+ _this.startPanGesture(event.clientX, event.clientY);
2560
2880
  _this.lockTextSelection();
2561
2881
  _this.focusForKeyboard();
2562
2882
  handleCallback(getContext(_this), event, onPanningStart);
@@ -2579,6 +2899,10 @@ var ZoomPanPinch = /** @class */ (function () {
2579
2899
  var keysPressed = _this.isPressingKeys(_this.setup.panning.activationKeys);
2580
2900
  if (!keysPressed)
2581
2901
  return;
2902
+ if (_this.trackPanGesture(event.clientX, event.clientY)) {
2903
+ _this.onPanningStop(event);
2904
+ return;
2905
+ }
2582
2906
  if (event.cancelable) {
2583
2907
  event.preventDefault();
2584
2908
  }
@@ -2587,15 +2911,73 @@ var ZoomPanPinch = /** @class */ (function () {
2587
2911
  handleCallback(getContext(_this), event, onPanning);
2588
2912
  };
2589
2913
  this.onPanningStop = function (event) {
2590
- var velocityDisabled = _this.setup.panning.velocityDisabled;
2914
+ var _a;
2915
+ var _b = _this.setup.panning, velocityDisabled = _b.velocityDisabled, suppressClickAfterPan = _b.suppressClickAfterPan;
2591
2916
  var onPanningStop = _this.props.onPanningStop;
2592
2917
  if (_this.isPanning) {
2918
+ var isDrag = !!((_a = _this.panGesture) === null || _a === void 0 ? void 0 : _a.isDrag);
2919
+ _this.panGesture = null;
2593
2920
  _this.unlockTextSelection();
2594
2921
  handlePanningEnd(_this, velocityDisabled);
2595
2922
  handleCallback(getContext(_this), event, onPanningStop);
2596
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
+ }
2597
2929
  }
2598
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
+ };
2599
2981
  /// ///////
2600
2982
  // Pinch
2601
2983
  /// ///////
@@ -2662,6 +3044,7 @@ var ZoomPanPinch = /** @class */ (function () {
2662
3044
  return;
2663
3045
  handleCancelAnimation(_this);
2664
3046
  handlePanningStart(_this, event);
3047
+ _this.startPanGesture(touches[0].clientX, touches[0].clientY);
2665
3048
  _this.lockTextSelection();
2666
3049
  _this.focusForKeyboard();
2667
3050
  handleCallback(getContext(_this), event, onPanningStart);
@@ -2680,11 +3063,16 @@ var ZoomPanPinch = /** @class */ (function () {
2680
3063
  var isAllowed = isPanningAllowed(_this);
2681
3064
  if (!isAllowed)
2682
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
+ }
2683
3072
  if (event.cancelable) {
2684
3073
  event.preventDefault();
2685
3074
  }
2686
3075
  event.stopPropagation();
2687
- var touch = event.touches[0];
2688
3076
  handlePanning(_this, touch.clientX, touch.clientY, DeviceType.TOUCH);
2689
3077
  handleCallback(getContext(_this), event, onPanning);
2690
3078
  }
@@ -2724,6 +3112,7 @@ var ZoomPanPinch = /** @class */ (function () {
2724
3112
  if (_this.isPanning) {
2725
3113
  _this.isPanning = false;
2726
3114
  _this.startCoords = null;
3115
+ _this.panGesture = null;
2727
3116
  }
2728
3117
  _this.unlockTextSelection();
2729
3118
  _this.flushResizeAlignment();
@@ -2831,24 +3220,30 @@ var ZoomPanPinch = /** @class */ (function () {
2831
3220
  /// ///////
2832
3221
  // `fitOnInit` wins over `centerOnInit` (a fit is always centred). Falls
2833
3222
  // back to centring while the content has no size yet; `onSizeChange`
2834
- // re-runs this once it does. Returns whether both boxes had a size, i.e.
2835
- // 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.
2836
3226
  this.applyInitialLayout = function () {
2837
- var fitOnInit = _this.setup.fitOnInit;
2838
- var _a = _this, wrapperComponent = _a.wrapperComponent, contentComponent = _a.contentComponent;
2839
- var hasSizes = !!wrapperComponent &&
2840
- !!contentComponent &&
2841
- 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 &&
2842
3232
  wrapperComponent.clientHeight > 0 &&
2843
3233
  (contentComponent.offsetWidth > 0 || contentComponent.offsetHeight > 0);
2844
- if (fitOnInit) {
2845
- var fitted = calculateFitToView(_this, fitOnInit === "cover" ? "cover" : "contain");
2846
- if (fitted) {
2847
- _this.setState(fitted.scale, fitted.positionX, fitted.positionY);
2848
- return hasSizes;
2849
- }
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;
2850
3241
  }
2851
- _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);
2852
3247
  return hasSizes;
2853
3248
  };
2854
3249
  this.setCenter = function () {
@@ -2949,7 +3344,6 @@ var ZoomPanPinch = /** @class */ (function () {
2949
3344
  _this.isInitialized = true;
2950
3345
  var ctx = getContext(_this);
2951
3346
  handleCallback(ctx, undefined, _this.props.onInit);
2952
- assignRef(_this.props.ref, ctx);
2953
3347
  };
2954
3348
  this.props = props;
2955
3349
  this.setup = createSetup(this.props);
@@ -3055,210 +3449,317 @@ var useResize = function (ref, onResize, dependencies) {
3055
3449
 
3056
3450
  var previewStyles = {
3057
3451
  position: "absolute",
3058
- zIndex: 2,
3452
+ zIndex: 3,
3059
3453
  top: "0px",
3060
3454
  left: "0px",
3061
3455
  boxSizing: "border-box",
3062
- border: "3px solid red",
3456
+ border: "2px solid red",
3063
3457
  transformOrigin: "0% 0%",
3064
- boxShadow: "rgba(0,0,0,0.2) 0 0 0 10000000px",
3065
3458
  pointerEvents: "none",
3066
3459
  };
3460
+ var wrapperStyles = {
3461
+ position: "absolute",
3462
+ top: "0px",
3463
+ left: "0px",
3464
+ transformOrigin: "0% 0%",
3465
+ boxSizing: "border-box",
3466
+ zIndex: 1,
3467
+ pointerEvents: "none",
3468
+ };
3469
+ var maskStyles = {
3470
+ position: "absolute",
3471
+ top: "0px",
3472
+ left: "0px",
3473
+ zIndex: 2,
3474
+ pointerEvents: "none",
3475
+ display: "block",
3476
+ };
3477
+ var getTouchPoint = function (event) {
3478
+ var _a;
3479
+ var touch = (_a = event.touches[0]) !== null && _a !== void 0 ? _a : event.changedTouches[0];
3480
+ if (!touch)
3481
+ return null;
3482
+ return { x: touch.clientX, y: touch.clientY };
3483
+ };
3484
+ var isInside = function (element, target) {
3485
+ return !!element &&
3486
+ !!target &&
3487
+ (target === element || element.contains(target));
3488
+ };
3067
3489
  var MiniMap = function (_a) {
3068
- var _b = _a.width, width = _b === void 0 ? 200 : _b, _c = _a.height, height = _c === void 0 ? 200 : _c, _d = _a.borderColor, borderColor = _d === void 0 ? "red" : _d, previewStyle = _a.previewStyle, wrapperClassName = _a.wrapperClassName, previewClassName = _a.previewClassName, children = _a.children, _e = _a.panning, panning = _e === void 0 ? true : _e, rest = __rest(_a, ["width", "height", "borderColor", "previewStyle", "wrapperClassName", "previewClassName", "children", "panning"]);
3069
- var _f = useState(false), initialized = _f[0], setInitialized = _f[1];
3490
+ var _b = _a.width, width = _b === void 0 ? 200 : _b, _c = _a.height, height = _c === void 0 ? 200 : _c, _d = _a.borderColor, borderColor = _d === void 0 ? "red" : _d, _e = _a.maskColor, maskColor = _e === void 0 ? "rgba(0, 0, 0, 0.2)" : _e, previewStyle = _a.previewStyle, wrapperClassName = _a.wrapperClassName, previewClassName = _a.previewClassName, children = _a.children, _f = _a.panning, panning = _f === void 0 ? true : _f, _g = _a.inversePan, inversePan = _g === void 0 ? false : _g, _h = _a.zoomable, zoomable = _h === void 0 ? false : _h, _j = _a.zoomStep, zoomStep = _j === void 0 ? 0.2 : _j, _k = _a.offsetScale, offsetScale = _k === void 0 ? 5 : _k, onClick = _a.onClick, rest = __rest(_a, ["width", "height", "borderColor", "maskColor", "previewStyle", "wrapperClassName", "previewClassName", "children", "panning", "inversePan", "zoomable", "zoomStep", "offsetScale", "onClick"]);
3491
+ var _l = useState(false), initialized = _l[0], setInitialized = _l[1];
3070
3492
  var instance = useTransformContext();
3071
- var _g = useState(false), isDown = _g[0], setIsDown = _g[1];
3072
3493
  var mainRef = useRef(null);
3073
3494
  var wrapperRef = useRef(null);
3074
3495
  var previewRef = useRef(null);
3496
+ var maskPathRef = useRef(null);
3497
+ var dragRef = useRef(null);
3498
+ var layoutRef = useRef(null);
3499
+ // Latest props for the document listeners, which are registered once.
3500
+ var optionsRef = useRef({ panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick });
3501
+ optionsRef.current = { panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick };
3075
3502
  var getViewportSize = useCallback(function () {
3076
3503
  if (instance.wrapperComponent) {
3077
3504
  var rect = instance.wrapperComponent.getBoundingClientRect();
3078
- return {
3079
- width: rect.width,
3080
- height: rect.height,
3081
- };
3505
+ return { width: rect.width, height: rect.height };
3082
3506
  }
3083
- return {
3084
- width: 0,
3085
- height: 0,
3086
- };
3087
- }, [instance.wrapperComponent]);
3507
+ return { width: 0, height: 0 };
3508
+ }, [instance]);
3088
3509
  var getContentSize = useCallback(function () {
3089
3510
  if (instance.contentComponent) {
3090
3511
  var rect = instance.contentComponent.getBoundingClientRect();
3091
- return {
3092
- width: rect.width / instance.state.scale,
3093
- height: rect.height / instance.state.scale,
3094
- };
3095
- }
3096
- return {
3097
- width: 0,
3098
- height: 0,
3099
- };
3100
- }, [instance.contentComponent, instance.state.scale]);
3101
- var computeMiniMapScale = useCallback(function () {
3102
- var contentSize = getContentSize();
3103
- var scaleX = width / contentSize.width;
3104
- var scaleY = height / contentSize.height;
3105
- var scale = scaleY > scaleX ? scaleX : scaleY;
3106
- return scale;
3107
- }, [getContentSize, height, width]);
3108
- var computeMiniMapSize = function () {
3109
- var contentSize = getContentSize();
3110
- var scaleX = width / contentSize.width;
3111
- var scaleY = height / contentSize.height;
3112
- if (scaleY > scaleX) {
3113
- return { width: width, height: contentSize.height * scaleX };
3512
+ var scale = instance.state.scale;
3513
+ return { width: rect.width / scale, height: rect.height / scale };
3114
3514
  }
3115
- return { width: contentSize.width * scaleY, height: height };
3116
- };
3117
- var computeMiniMapStyle = function () {
3118
- var scale = computeMiniMapScale();
3119
- var style = {
3120
- transform: "scale(".concat(scale || 1, ")"),
3121
- transformOrigin: "0% 0%",
3122
- position: "absolute",
3123
- boxSizing: "border-box",
3124
- zIndex: 1,
3125
- // overflow: "hidden",
3126
- };
3127
- if (wrapperRef.current) {
3128
- var el = wrapperRef.current.style;
3129
- el.transform = style.transform;
3130
- el.transformOrigin = style.transformOrigin;
3131
- el.position = style.position;
3132
- el.boxSizing = style.boxSizing;
3133
- el.zIndex = String(style.zIndex);
3515
+ return { width: 0, height: 0 };
3516
+ }, [instance]);
3517
+ /**
3518
+ * Like React Flow, the map shows the union of the content and the current
3519
+ * viewport (plus `offsetScale` padding) fitted into the box, so the
3520
+ * viewport indicator never leaves the mini map even when the view is
3521
+ * panned or zoomed past the content.
3522
+ */
3523
+ var computeLayout = useCallback(function () {
3524
+ var content = getContentSize();
3525
+ var viewport = getViewportSize();
3526
+ var _a = instance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
3527
+ if (!content.width || !content.height || !width || !height || !scale) {
3528
+ return null;
3134
3529
  }
3135
- };
3136
- var transformMiniMap = function () {
3137
- computeMiniMapStyle();
3138
- var miniSize = computeMiniMapSize();
3139
- var wrapSize = getContentSize();
3530
+ var viewX = -positionX / scale;
3531
+ var viewY = -positionY / scale;
3532
+ var viewWidth = viewport.width / scale;
3533
+ var viewHeight = viewport.height / scale;
3534
+ var minX = Math.min(0, viewX);
3535
+ var minY = Math.min(0, viewY);
3536
+ var maxX = Math.max(content.width, viewX + viewWidth);
3537
+ var maxY = Math.max(content.height, viewY + viewHeight);
3538
+ var boundsWidth = maxX - minX;
3539
+ var boundsHeight = maxY - minY;
3540
+ var viewScale = Math.max(boundsWidth / width, boundsHeight / height);
3541
+ var fitWidth = viewScale * width;
3542
+ var fitHeight = viewScale * height;
3543
+ var offset = offsetScale * viewScale;
3544
+ var originX = minX - (fitWidth - boundsWidth) / 2 - offset;
3545
+ var originY = minY - (fitHeight - boundsHeight) / 2 - offset;
3546
+ var mapScale = width / (fitWidth + offset * 2);
3547
+ return {
3548
+ mapScale: mapScale,
3549
+ originX: originX,
3550
+ originY: originY,
3551
+ contentWidth: content.width,
3552
+ contentHeight: content.height,
3553
+ previewX: (viewX - originX) * mapScale,
3554
+ previewY: (viewY - originY) * mapScale,
3555
+ previewWidth: viewWidth * mapScale,
3556
+ previewHeight: viewHeight * mapScale,
3557
+ };
3558
+ }, [getContentSize, getViewportSize, height, instance, offsetScale, width]);
3559
+ var transformMiniMap = useCallback(function () {
3560
+ var layout = computeLayout();
3561
+ layoutRef.current = layout;
3562
+ if (!layout)
3563
+ return;
3564
+ var px = function (value) { return "".concat(roundNumber(value, 2), "px"); };
3140
3565
  if (wrapperRef.current) {
3141
- wrapperRef.current.style.width = "".concat(wrapSize.width, "px");
3142
- wrapperRef.current.style.height = "".concat(wrapSize.height, "px");
3143
- }
3144
- if (mainRef.current) {
3145
- mainRef.current.style.width = "".concat(miniSize.width, "px");
3146
- mainRef.current.style.height = "".concat(miniSize.height, "px");
3566
+ var style = wrapperRef.current.style;
3567
+ style.width = px(layout.contentWidth);
3568
+ style.height = px(layout.contentHeight);
3569
+ style.transform = "translate(".concat(px(-layout.originX * layout.mapScale), ", ").concat(px(-layout.originY * layout.mapScale), ") scale(").concat(roundNumber(layout.mapScale, 4), ")");
3147
3570
  }
3148
3571
  if (previewRef.current) {
3149
- var size = getViewportSize();
3150
- var scale = computeMiniMapScale();
3151
- var previewScale = scale * (1 / instance.state.scale);
3152
- var transform = instance.handleTransformStyles(-instance.state.positionX * previewScale, -instance.state.positionY * previewScale, 1);
3153
- previewRef.current.style.transform = transform;
3154
- previewRef.current.style.width = "".concat(size.width * previewScale, "px");
3155
- previewRef.current.style.height = "".concat(size.height * previewScale, "px");
3572
+ var style = previewRef.current.style;
3573
+ style.transform = "translate(".concat(px(layout.previewX), ", ").concat(px(layout.previewY), ")");
3574
+ style.width = px(layout.previewWidth);
3575
+ style.height = px(layout.previewHeight);
3156
3576
  }
3157
- };
3158
- var initialize = function () {
3159
- transformMiniMap();
3160
- };
3161
- useTransformEffect(function () {
3162
- transformMiniMap();
3163
- });
3577
+ if (maskPathRef.current) {
3578
+ var x = roundNumber(layout.previewX, 2);
3579
+ var y = roundNumber(layout.previewY, 2);
3580
+ var w = roundNumber(layout.previewWidth, 2);
3581
+ var h = roundNumber(layout.previewHeight, 2);
3582
+ maskPathRef.current.setAttribute("d", "M0 0H".concat(width, "V").concat(height, "H0Z M").concat(x, " ").concat(y, "h").concat(w, "v").concat(h, "h").concat(-w, "Z"));
3583
+ }
3584
+ }, [computeLayout, height, width]);
3585
+ useTransformEffect(transformMiniMap);
3164
3586
  useTransformInit(function () {
3165
- initialize();
3587
+ transformMiniMap();
3166
3588
  setInitialized(true);
3167
3589
  });
3168
- useResize(instance.contentComponent, initialize, [initialized]);
3590
+ useResize(instance.contentComponent, transformMiniMap, [initialized]);
3591
+ useEffect(function () {
3592
+ transformMiniMap();
3593
+ }, [transformMiniMap]);
3169
3594
  useEffect(function () {
3170
- var _a, _b;
3171
- var move = function (e) {
3595
+ var element = mainRef.current;
3596
+ if (!element)
3597
+ return;
3598
+ var getMapScale = function () {
3172
3599
  var _a, _b;
3173
- if (panning &&
3174
- isDown &&
3175
- instance.contentComponent &&
3176
- previewRef.current &&
3177
- mainRef.current) {
3178
- var scale = computeMiniMapScale();
3179
- var previewRect = previewRef.current.getBoundingClientRect();
3180
- var mainRect = mainRef.current.getBoundingClientRect();
3181
- var relativeX = (e.clientX - mainRect.left) / scale;
3182
- var relativeY = (e.clientY - mainRect.top) / scale;
3183
- var x = relativeX - previewRect.width / 2;
3184
- var y = relativeY - previewRect.height / 2;
3185
- var instanceWidth = (((_a = instance.wrapperComponent) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0) * instance.state.scale;
3186
- var instanceHeight = (((_b = instance.wrapperComponent) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0) * instance.state.scale;
3187
- var limitWidth = instanceWidth - previewRect.width * 2 * instance.state.scale;
3188
- var limitHeight = instanceHeight - previewRect.height * 2 * instance.state.scale;
3189
- var boundedX = boundLimiter(x * instance.state.scale, 0, limitWidth, true);
3190
- var boundedY = boundLimiter(y * instance.state.scale, 0, limitHeight, true);
3191
- instance.setState(instance.state.scale, -boundedX, -boundedY);
3192
- }
3600
+ var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
3601
+ return (_b = layout === null || layout === void 0 ? void 0 : layout.mapScale) !== null && _b !== void 0 ? _b : 0;
3602
+ };
3603
+ var start = function (point, target) {
3604
+ if (!optionsRef.current.panning)
3605
+ return;
3606
+ if (!isInside(element, target))
3607
+ return;
3608
+ if (!instance.wrapperComponent || !instance.contentComponent)
3609
+ return;
3610
+ handleCancelAnimation(instance);
3611
+ dragRef.current = { x: point.x, y: point.y, moved: false };
3612
+ element.style.cursor = "grabbing";
3613
+ };
3614
+ var move = function (point) {
3615
+ var drag = dragRef.current;
3616
+ if (!drag)
3617
+ return;
3618
+ var deltaX = point.x - drag.x;
3619
+ var deltaY = point.y - drag.y;
3620
+ drag.x = point.x;
3621
+ drag.y = point.y;
3622
+ if (!deltaX && !deltaY)
3623
+ return;
3624
+ drag.moved = true;
3625
+ var mapScale = getMapScale();
3626
+ var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
3627
+ if (!mapScale || !wrapperComponent || !contentComponent)
3628
+ return;
3629
+ // Dragging by one mini map px moves the viewport by 1 / mapScale
3630
+ // content units, so the indicator follows the pointer 1:1.
3631
+ var direction = optionsRef.current.inversePan ? 1 : -1;
3632
+ var moveScale = (direction * state.scale) / mapScale;
3633
+ var nextX = state.positionX + deltaX * moveScale;
3634
+ var nextY = state.positionY + deltaY * moveScale;
3635
+ var bounds = handleCalculateBounds(instance, state.scale);
3636
+ var _a = getMouseBoundedPosition(nextX, nextY, bounds, setup.limitToBounds, 0, 0, wrapperComponent), x = _a.x, y = _a.y;
3637
+ if (x === state.positionX && y === state.positionY)
3638
+ return;
3639
+ instance.setState(state.scale, x, y);
3193
3640
  };
3194
- var setDown = function (e) {
3641
+ var stop = function (event, point) {
3195
3642
  var _a;
3196
- if (((_a = mainRef.current) === null || _a === void 0 ? void 0 : _a.contains(e.target)) ||
3197
- e.target === mainRef.current) {
3198
- move(e);
3199
- setIsDown(true);
3643
+ var drag = dragRef.current;
3644
+ if (!drag)
3645
+ return;
3646
+ dragRef.current = null;
3647
+ element.style.cursor = optionsRef.current.panning ? "grab" : "";
3648
+ var handleClick = optionsRef.current.onClick;
3649
+ if (drag.moved || !handleClick || !point)
3650
+ return;
3651
+ var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
3652
+ if (!layout)
3653
+ return;
3654
+ var rect = element.getBoundingClientRect();
3655
+ handleClick(event, {
3656
+ x: layout.originX + (point.x - rect.left) / layout.mapScale,
3657
+ y: layout.originY + (point.y - rect.top) / layout.mapScale,
3658
+ });
3659
+ };
3660
+ var onMouseDown = function (event) {
3661
+ if (event.button !== 0)
3662
+ return;
3663
+ start({ x: event.clientX, y: event.clientY }, event.target);
3664
+ };
3665
+ var onMouseMove = function (event) {
3666
+ if (!dragRef.current)
3667
+ return;
3668
+ // The mouse was released outside the window: end the drag.
3669
+ if (event.buttons === 0) {
3670
+ stop(event, null);
3671
+ return;
3200
3672
  }
3673
+ move({ x: event.clientX, y: event.clientY });
3201
3674
  };
3202
- var setUp = function () {
3203
- setIsDown(false);
3675
+ var onMouseUp = function (event) {
3676
+ stop(event, { x: event.clientX, y: event.clientY });
3677
+ };
3678
+ var onTouchStart = function (event) {
3679
+ if (event.touches.length !== 1)
3680
+ return;
3681
+ var point = getTouchPoint(event);
3682
+ if (point)
3683
+ start(point, event.target);
3684
+ };
3685
+ var onTouchMove = function (event) {
3686
+ if (!dragRef.current)
3687
+ return;
3688
+ if (event.touches.length !== 1) {
3689
+ stop(event, null);
3690
+ return;
3691
+ }
3692
+ var point = getTouchPoint(event);
3693
+ if (point)
3694
+ move(point);
3695
+ };
3696
+ var onTouchEnd = function (event) {
3697
+ stop(event, getTouchPoint(event));
3204
3698
  };
3205
3699
  // Listen on the mini map's own document so it keeps working inside
3206
3700
  // portal windows and iframes.
3207
- var doc = (_b = (_a = mainRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) !== null && _b !== void 0 ? _b : document;
3208
- doc.addEventListener("mousedown", setDown);
3209
- doc.addEventListener("mouseup", setUp);
3210
- doc.addEventListener("mousemove", move);
3701
+ var doc = element.ownerDocument;
3702
+ doc.addEventListener("mousedown", onMouseDown);
3703
+ doc.addEventListener("mousemove", onMouseMove);
3704
+ doc.addEventListener("mouseup", onMouseUp);
3705
+ doc.addEventListener("touchstart", onTouchStart);
3706
+ doc.addEventListener("touchmove", onTouchMove);
3707
+ doc.addEventListener("touchend", onTouchEnd);
3708
+ doc.addEventListener("touchcancel", onTouchEnd);
3211
3709
  return function () {
3212
- doc.removeEventListener("mousedown", setDown);
3213
- doc.removeEventListener("mousemove", move);
3214
- doc.removeEventListener("mouseup", setUp);
3710
+ doc.removeEventListener("mousedown", onMouseDown);
3711
+ doc.removeEventListener("mousemove", onMouseMove);
3712
+ doc.removeEventListener("mouseup", onMouseUp);
3713
+ doc.removeEventListener("touchstart", onTouchStart);
3714
+ doc.removeEventListener("touchmove", onTouchMove);
3715
+ doc.removeEventListener("touchend", onTouchEnd);
3716
+ doc.removeEventListener("touchcancel", onTouchEnd);
3717
+ dragRef.current = null;
3718
+ };
3719
+ }, [computeLayout, instance]);
3720
+ useEffect(function () {
3721
+ var element = mainRef.current;
3722
+ if (!element || !zoomable)
3723
+ return;
3724
+ var onWheel = function (event) {
3725
+ var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
3726
+ if (setup.disabled || !wrapperComponent || !contentComponent)
3727
+ return;
3728
+ if (!event.deltaY)
3729
+ return;
3730
+ if (event.cancelable)
3731
+ event.preventDefault();
3732
+ event.stopPropagation();
3733
+ var delta = event.deltaY < 0 ? 1 : -1;
3734
+ var newScale = handleCalculateButtonZoom(instance, delta, optionsRef.current.zoomStep);
3735
+ if (newScale === state.scale)
3736
+ return;
3737
+ // Zoom around the centre of the current viewport.
3738
+ var centerX = (wrapperComponent.offsetWidth / 2 - state.positionX) / state.scale;
3739
+ var centerY = (wrapperComponent.offsetHeight / 2 - state.positionY) / state.scale;
3740
+ handleCancelAnimation(instance);
3741
+ var target = handleZoomToPoint(instance, newScale, centerX, centerY);
3742
+ if (!target)
3743
+ return;
3744
+ instance.setState(target.scale, target.positionX, target.positionY);
3215
3745
  };
3216
- }, [computeMiniMapScale, instance, isDown, panning]);
3217
- var wrapperStyle = useMemo(function () {
3218
- return {
3219
- position: "relative",
3220
- zIndex: 2,
3221
- // overflow: "hidden",
3222
- userSelect: "none",
3746
+ element.addEventListener("wheel", onWheel, { passive: false });
3747
+ return function () {
3748
+ element.removeEventListener("wheel", onWheel);
3223
3749
  };
3224
- }, []);
3225
- return (jsxs("div", __assign({}, rest, { ref: mainRef, style: wrapperStyle, className: "rzpp-mini-map ".concat(rest.className || "").trim() }, { children: [jsx("div", __assign({ style: { pointerEvents: "none" }, ref: wrapperRef, className: "rzpp-minimap-wrapper ".concat(wrapperClassName || "").trim() }, { children: children })), jsx("div", { className: "rzpp-minimap-preview ".concat(previewClassName || "").trim(), ref: previewRef, style: __assign(__assign(__assign({}, previewStyles), { borderColor: borderColor }), previewStyle) })] })));
3750
+ }, [instance, zoomable]);
3751
+ var containerStyle = __assign({ position: "relative", zIndex: 2, width: "".concat(width, "px"), height: "".concat(height, "px"), overflow: "hidden", userSelect: "none", WebkitUserSelect: "none", touchAction: "none", cursor: panning ? "grab" : undefined }, rest.style);
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) })] })));
3226
3753
  };
3227
3754
 
3228
- function styleInject(css, ref) {
3229
- if ( ref === void 0 ) ref = {};
3230
- var insertAt = ref.insertAt;
3231
-
3232
- if (!css || typeof document === 'undefined') { return; }
3233
-
3234
- var head = document.head || document.getElementsByTagName('head')[0];
3235
- var style = document.createElement('style');
3236
- style.type = 'text/css';
3237
-
3238
- if (insertAt === 'top') {
3239
- if (head.firstChild) {
3240
- head.insertBefore(style, head.firstChild);
3241
- } else {
3242
- head.appendChild(style);
3243
- }
3244
- } else {
3245
- head.appendChild(style);
3246
- }
3247
-
3248
- if (style.styleSheet) {
3249
- style.styleSheet.cssText = css;
3250
- } else {
3251
- style.appendChild(document.createTextNode(css));
3252
- }
3253
- }
3254
-
3255
- 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";
3256
- var styles = {"wrapper":"transform-component-module_wrapper__SPB86","content":"transform-component-module_content__FBWxo","infiniteGrid":"transform-component-module_infiniteGrid__Z-aP3"};
3257
- 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";
3258
3757
 
3758
+ var styles = transformComponent_module;
3759
+ var stylesheet = stylesheet$1;
3259
3760
  var TransformComponent = function (_a) {
3260
3761
  var _b;
3261
- 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;
3262
3763
  var instance = useContext(Context);
3263
3764
  var init = instance.init, cleanupWindowEvents = instance.cleanupWindowEvents;
3264
3765
  var wrapperRef = useRef(null);
@@ -3270,6 +3771,10 @@ var TransformComponent = function (_a) {
3270
3771
  useIsomorphicLayoutEffect(function () {
3271
3772
  var wrapper = wrapperRef.current;
3272
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
+ }
3273
3778
  if (wrapper !== null && content !== null && init) {
3274
3779
  init === null || init === void 0 ? void 0 : init(wrapper, content);
3275
3780
  }
@@ -3445,13 +3950,37 @@ var useTransformInit = function (callback) {
3445
3950
  }, []);
3446
3951
  };
3447
3952
 
3953
+ var takeSnapshot = function (state) { return ({
3954
+ scale: state.scale,
3955
+ positionX: state.positionX,
3956
+ positionY: state.positionY,
3957
+ }); };
3958
+ var isSameSnapshot = function (snapshot, state) {
3959
+ return snapshot !== null &&
3960
+ snapshot.scale === state.scale &&
3961
+ snapshot.positionX === state.positionX &&
3962
+ snapshot.positionY === state.positionY;
3963
+ };
3964
+
3448
3965
  var useTransformEffect = function (callback) {
3449
3966
  var libraryContext = useTransformContext();
3967
+ // Starts as the state the consumer rendered with, so a transform applied
3968
+ // during mount (initialPosition*, centerOnInit, fitOnInit) — which happens
3969
+ // in a layout effect, before this subscription exists — is delivered once.
3970
+ var lastSeen = useRef(null);
3971
+ if (lastSeen.current === null) {
3972
+ lastSeen.current = takeSnapshot(libraryContext.state);
3973
+ }
3450
3974
  useEffect(function () {
3451
3975
  var unmountCallback;
3452
3976
  var unmount = libraryContext.onChange(function (ref) {
3977
+ lastSeen.current = takeSnapshot(ref.instance.state);
3453
3978
  unmountCallback = callback(getState(ref.instance));
3454
3979
  });
3980
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
3981
+ lastSeen.current = takeSnapshot(libraryContext.state);
3982
+ unmountCallback = callback(getState(libraryContext));
3983
+ }
3455
3984
  return function () {
3456
3985
  unmount();
3457
3986
  unmountCallback === null || unmountCallback === void 0 ? void 0 : unmountCallback();
@@ -3461,16 +3990,27 @@ var useTransformEffect = function (callback) {
3461
3990
 
3462
3991
  function useTransformComponent(callback) {
3463
3992
  var libraryContext = useTransformContext();
3993
+ var lastSeen = useRef(null);
3464
3994
  var _a = useState(function () {
3995
+ lastSeen.current = takeSnapshot(libraryContext.state);
3465
3996
  return callback(getState(libraryContext));
3466
3997
  }), transformRender = _a[0], setTransformRender = _a[1];
3467
3998
  useEffect(function () {
3468
3999
  var mounted = true;
3469
4000
  var unmount = libraryContext.onChange(function (ref) {
3470
4001
  if (mounted) {
4002
+ lastSeen.current = takeSnapshot(ref.instance.state);
3471
4003
  setTransformRender(callback(getState(ref.instance)));
3472
4004
  }
3473
4005
  });
4006
+ // The instance initializes in a layout effect, so a transform applied on
4007
+ // mount (initialPosition*, centerOnInit, fitOnInit) can land before this
4008
+ // subscription exists. Catch up when the state moved since the last
4009
+ // render; the snapshot check keeps this from re-rendering in a loop.
4010
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
4011
+ lastSeen.current = takeSnapshot(libraryContext.state);
4012
+ setTransformRender(callback(getState(libraryContext)));
4013
+ }
3474
4014
  return function () {
3475
4015
  unmount();
3476
4016
  mounted = false;
@@ -3515,5 +4055,5 @@ var useZoomPanPinch = function (props) {
3515
4055
  };
3516
4056
  };
3517
4057
 
3518
- 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 };
3519
4059
  //# sourceMappingURL=index.esm.js.map