react-zoom-pan-pinch 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -282,6 +282,61 @@ var calculateBounds = function (contextInstance, newScale) {
282
282
  }
283
283
  return bounds;
284
284
  };
285
+ function clamp(v, min, max) {
286
+ return Math.max(min, Math.min(v, max));
287
+ }
288
+ // Based on @aholachek ;)
289
+ // https://twitter.com/chpwn/status/285540192096497664
290
+ // iOS constant = 0.55
291
+ // https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
292
+ function rubberband(distance, dimension, constant) {
293
+ if (dimension === 0 || Math.abs(dimension) === Infinity)
294
+ return Math.pow(distance, (constant * 5));
295
+ return (distance * dimension * constant) / (dimension + constant * distance);
296
+ }
297
+ /**
298
+ * Furthest a grabbed position is traced back along the rubber band curve, as
299
+ * a fraction of the padding. The curve never reaches the padding itself, so
300
+ * a position at (or past) it has no pointer distance to map back to.
301
+ */
302
+ var MAX_RUBBER_BAND_RATIO = 0.9;
303
+ /**
304
+ * Resistance of the rubber band, the constant UIScrollView uses: past a bound
305
+ * the content moves at 0.55× the pointer and slows down from there.
306
+ */
307
+ var RUBBER_BAND_CONSTANT = 0.55;
308
+ /**
309
+ * Where a pan that was dragged past a bound ends up: `(1 - 1 / (x * c / d + 1)) * d`
310
+ * with `d` = `padding`. It trails the pointer from the first pixel past the
311
+ * bound and approaches — never reaches — `padding` px past it.
312
+ */
313
+ function getRubberBandPosition(position, min, max, padding) {
314
+ if (padding <= 0)
315
+ return clamp(position, min, max);
316
+ if (position < min)
317
+ return min - rubberband(min - position, padding, RUBBER_BAND_CONSTANT);
318
+ if (position > max)
319
+ return max + rubberband(position - max, padding, RUBBER_BAND_CONSTANT);
320
+ return position;
321
+ }
322
+ /**
323
+ * Inverse of `getRubberBandPosition`: the pointer position that puts the
324
+ * content at `position`. Lets a pan that starts inside the padding (grabbed
325
+ * mid return animation, trackpad deltas) continue from where the content is.
326
+ */
327
+ function getRubberBandSource(position, min, max, padding) {
328
+ if (padding <= 0)
329
+ return position;
330
+ var stretch = function (offset) {
331
+ var limited = Math.min(offset, padding * MAX_RUBBER_BAND_RATIO);
332
+ return (limited * padding) / (RUBBER_BAND_CONSTANT * (padding - limited));
333
+ };
334
+ if (position < min)
335
+ return min - stretch(min - position);
336
+ if (position > max)
337
+ return max + stretch(position - max);
338
+ return position;
339
+ }
285
340
  /**
286
341
  * Keeps value between given bounds, used for limiting view to given boundaries
287
342
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -316,6 +371,23 @@ function getMouseBoundedPosition(positionX, positionY, bounds, limitToBounds, pa
316
371
  var y = boundLimiter(positionY, minPositionY - paddingY, maxPositionY + paddingY, limitToBounds);
317
372
  return { x: x, y: y };
318
373
  }
374
+ /**
375
+ * Bounded position for a pan gesture: free inside the bounds and, inside the
376
+ * padding around them, rubber banded (`rubberBand`) or 1:1 up to its edge.
377
+ */
378
+ function getPanBoundedPosition(positionX, positionY, bounds, limitToBounds, paddingX, paddingY, rubberBand) {
379
+ var minPositionX = bounds.minPositionX, minPositionY = bounds.minPositionY, maxPositionX = bounds.maxPositionX, maxPositionY = bounds.maxPositionY;
380
+ var limit = function (position, min, max, padding) {
381
+ if (limitToBounds && rubberBand && padding > 0) {
382
+ return roundNumber(getRubberBandPosition(position, min, max, padding), 2);
383
+ }
384
+ return boundLimiter(position, min - padding, max + padding, limitToBounds);
385
+ };
386
+ return {
387
+ x: limit(positionX, minPositionX, maxPositionX, paddingX),
388
+ y: limit(positionY, minPositionY, maxPositionY, paddingY),
389
+ };
390
+ }
319
391
 
320
392
  function handleCalculateZoomPositions(contextInstance, mouseX, mouseY, newScale, bounds, limitToBounds) {
321
393
  var _a = contextInstance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
@@ -381,8 +453,45 @@ var isPanningAllowed = function (contextInstance) {
381
453
  return false;
382
454
  return true;
383
455
  };
456
+ var getPaddingValue = function (contextInstance, size, explicitScale) {
457
+ var setup = contextInstance.setup, state = contextInstance.state;
458
+ var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
459
+ var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
460
+ if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
461
+ return size;
462
+ }
463
+ return 0;
464
+ };
465
+ /**
466
+ * Dragging past the bounds stretches like a rubber band only when the content
467
+ * is going to return to them on release (`autoAlignment`). Without that return
468
+ * the padding is a plain area the content can be left in, so it follows the
469
+ * pointer 1:1 up to its edge.
470
+ */
471
+ function isRubberBandEnabled(contextInstance) {
472
+ var _a = contextInstance.setup, limitToBounds = _a.limitToBounds, autoAlignment = _a.autoAlignment;
473
+ return limitToBounds && !autoAlignment.disabled;
474
+ }
475
+ /**
476
+ * The position a pan continues from. Inside the bounds that is the content
477
+ * position; inside the padding the content trails the pointer (rubber band),
478
+ * so the pointer's own position is traced back from it — otherwise the first
479
+ * move of a pan started there would make the content jump.
480
+ */
481
+ function getPanSourcePosition(contextInstance) {
482
+ var bounds = contextInstance.bounds, setup = contextInstance.setup, state = contextInstance.state;
483
+ var positionX = state.positionX, positionY = state.positionY;
484
+ if (!bounds || !isRubberBandEnabled(contextInstance)) {
485
+ return { x: positionX, y: positionY };
486
+ }
487
+ var _a = setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
488
+ return {
489
+ x: getRubberBandSource(positionX, bounds.minPositionX, bounds.maxPositionX, getPaddingValue(contextInstance, sizeX)),
490
+ y: getRubberBandSource(positionY, bounds.minPositionY, bounds.maxPositionY, getPaddingValue(contextInstance, sizeY)),
491
+ };
492
+ }
384
493
  var handlePanningSetup = function (contextInstance, event) {
385
- var _a = contextInstance.state, positionX = _a.positionX, positionY = _a.positionY;
494
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
386
495
  contextInstance.isPanning = true;
387
496
  // Panning with mouse
388
497
  var x = event.clientX;
@@ -391,7 +500,7 @@ var handlePanningSetup = function (contextInstance, event) {
391
500
  };
392
501
  var handleTouchPanningSetup = function (contextInstance, event) {
393
502
  var touches = event.touches;
394
- var _a = contextInstance.state, positionX = _a.positionX, positionY = _a.positionY;
503
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
395
504
  contextInstance.isPanning = true;
396
505
  // Panning with touch
397
506
  var oneFingerTouch = touches.length === 1;
@@ -432,14 +541,16 @@ function handleNewPosition(contextInstance, newPositionX, newPositionY, paddingV
432
541
  (newPositionX === positionX && newPositionY === positionY)) {
433
542
  return;
434
543
  }
435
- var _b = getMouseBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, paddingValueX, paddingValueY, wrapperComponent), x = _b.x, y = _b.y;
544
+ var _b = getPanBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, paddingValueX, paddingValueY, isRubberBandEnabled(contextInstance)), x = _b.x, y = _b.y;
436
545
  contextInstance.setState(scale, x, y);
437
546
  }
438
547
  var getPanningClientPosition = function (contextInstance, clientX, clientY) {
439
- var startCoords = contextInstance.startCoords, state = contextInstance.state;
548
+ var startCoords = contextInstance.startCoords;
440
549
  var panning = contextInstance.setup.panning;
441
550
  var lockAxisX = panning.lockAxisX, lockAxisY = panning.lockAxisY;
442
- var positionX = state.positionX, positionY = state.positionY;
551
+ // A locked axis keeps its place: the source maps back onto the position
552
+ // it has now, also when it rests inside the padding.
553
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
443
554
  if (!startCoords) {
444
555
  return { x: positionX, y: positionY };
445
556
  }
@@ -449,15 +560,6 @@ var getPanningClientPosition = function (contextInstance, clientX, clientY) {
449
560
  var newPositionY = lockAxisY ? positionY : mouseY;
450
561
  return { x: newPositionX, y: newPositionY };
451
562
  };
452
- var getPaddingValue = function (contextInstance, size, explicitScale) {
453
- var setup = contextInstance.setup, state = contextInstance.state;
454
- var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
455
- var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
456
- if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
457
- return size;
458
- }
459
- return 0;
460
- };
461
563
 
462
564
  exports.DeviceType = void 0;
463
565
  (function (DeviceType) {
@@ -591,8 +693,15 @@ function handleVelocityPanning(contextInstance) {
591
693
  var finalAnimationTime = Math.max(moveAnimationTime, alignAnimationTime);
592
694
  var paddingValueX = getPaddingValue(contextInstance, sizeX);
593
695
  var paddingValueY = getPaddingValue(contextInstance, sizeY);
594
- var paddingX = (paddingValueX * wrapperComponent.clientWidth) / 100;
595
- var paddingY = (paddingValueY * wrapperComponent.clientHeight) / 100;
696
+ // A fling released inside the rubber band stays within the same stretch a
697
+ // drag could reach (`padding` px), not a share of the wrapper size.
698
+ var isRubberBand = isRubberBandEnabled(contextInstance);
699
+ var paddingX = isRubberBand
700
+ ? paddingValueX
701
+ : (paddingValueX * wrapperComponent.clientWidth) / 100;
702
+ var paddingY = isRubberBand
703
+ ? paddingValueY
704
+ : (paddingValueY * wrapperComponent.clientHeight) / 100;
596
705
  var maxTargetX = maxPositionX + paddingX;
597
706
  var minTargetX = minPositionX - paddingX;
598
707
  var maxTargetY = maxPositionY + paddingY;
@@ -679,8 +788,13 @@ function handlePanningEnd(contextInstance, velocityDisabled) {
679
788
  if (start) {
680
789
  var dx = positionX - start.x;
681
790
  var dy = positionY - start.y;
682
- if (dx * dx + dy * dy <= 25)
791
+ // A click is not a fling, but it can still land on content left past
792
+ // the bounds — e.g. grabbed mid return animation, which the press
793
+ // cancelled. Finish the return (a no-op inside the bounds).
794
+ if (dx * dx + dy * dy <= 25) {
795
+ handleAlignToBounds(contextInstance);
683
796
  return;
797
+ }
684
798
  }
685
799
  contextInstance.isAnimating = false;
686
800
  contextInstance.animation = null;
@@ -1519,16 +1633,6 @@ var getCenterPosition = function (scale, wrapperComponent, contentComponent) {
1519
1633
  };
1520
1634
  };
1521
1635
 
1522
- function assignRef(ref, value) {
1523
- if (ref == null)
1524
- return;
1525
- if (typeof ref === "function") {
1526
- ref(value);
1527
- }
1528
- else {
1529
- ref.current = value;
1530
- }
1531
- }
1532
1636
  function mergeRefs(refs) {
1533
1637
  return function (value) {
1534
1638
  refs.forEach(function (ref) {
@@ -2515,17 +2619,19 @@ var ZoomPanPinch = /** @class */ (function () {
2515
2619
  event.preventDefault();
2516
2620
  }
2517
2621
  event.stopPropagation();
2518
- var _a = _this.state, positionX = _a.positionX, positionY = _a.positionY;
2519
- var mouseX = positionX - event.deltaX;
2520
- var mouseY = positionY - event.deltaY;
2521
- var newPositionX = lockAxisX ? positionX : mouseX;
2522
- var newPositionY = lockAxisY ? positionY : mouseY;
2523
- var _b = _this.setup.autoAlignment, sizeX = _b.sizeX, sizeY = _b.sizeY;
2622
+ var deltaX = lockAxisX ? 0 : event.deltaX;
2623
+ var deltaY = lockAxisY ? 0 : event.deltaY;
2624
+ if (!deltaX && !deltaY)
2625
+ return;
2626
+ var _a = _this.setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
2524
2627
  var paddingValueX = getPaddingValue(_this, sizeX);
2525
2628
  var paddingValueY = getPaddingValue(_this, sizeY);
2526
- if (newPositionX === positionX && newPositionY === positionY)
2527
- return;
2528
2629
  handleWheelPanningStart(_this, event);
2630
+ // Deltas add up on the pointer's side of the rubber band, the same way
2631
+ // a drag does, so the content slows down towards the padding limit.
2632
+ var source = getPanSourcePosition(_this);
2633
+ var newPositionX = source.x - deltaX;
2634
+ var newPositionY = source.y - deltaY;
2529
2635
  handleNewPosition(_this, newPositionX, newPositionY, paddingValueX, paddingValueY);
2530
2636
  handleCallback(getContext(_this), event, onPanning);
2531
2637
  handleWheelPanningStop(_this, event);
@@ -2951,7 +3057,6 @@ var ZoomPanPinch = /** @class */ (function () {
2951
3057
  _this.isInitialized = true;
2952
3058
  var ctx = getContext(_this);
2953
3059
  handleCallback(ctx, undefined, _this.props.onInit);
2954
- assignRef(_this.props.ref, ctx);
2955
3060
  };
2956
3061
  this.props = props;
2957
3062
  this.setup = createSetup(this.props);
@@ -3057,174 +3162,307 @@ var useResize = function (ref, onResize, dependencies) {
3057
3162
 
3058
3163
  var previewStyles = {
3059
3164
  position: "absolute",
3060
- zIndex: 2,
3165
+ zIndex: 3,
3061
3166
  top: "0px",
3062
3167
  left: "0px",
3063
3168
  boxSizing: "border-box",
3064
- border: "3px solid red",
3169
+ border: "2px solid red",
3170
+ transformOrigin: "0% 0%",
3171
+ pointerEvents: "none",
3172
+ };
3173
+ var wrapperStyles = {
3174
+ position: "absolute",
3175
+ top: "0px",
3176
+ left: "0px",
3065
3177
  transformOrigin: "0% 0%",
3066
- boxShadow: "rgba(0,0,0,0.2) 0 0 0 10000000px",
3178
+ boxSizing: "border-box",
3179
+ zIndex: 1,
3180
+ pointerEvents: "none",
3181
+ };
3182
+ var maskStyles = {
3183
+ position: "absolute",
3184
+ top: "0px",
3185
+ left: "0px",
3186
+ zIndex: 2,
3067
3187
  pointerEvents: "none",
3188
+ display: "block",
3189
+ };
3190
+ var getTouchPoint = function (event) {
3191
+ var _a;
3192
+ var touch = (_a = event.touches[0]) !== null && _a !== void 0 ? _a : event.changedTouches[0];
3193
+ if (!touch)
3194
+ return null;
3195
+ return { x: touch.clientX, y: touch.clientY };
3196
+ };
3197
+ var isInside = function (element, target) {
3198
+ return !!element &&
3199
+ !!target &&
3200
+ (target === element || element.contains(target));
3068
3201
  };
3069
3202
  var MiniMap = function (_a) {
3070
- 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"]);
3071
- var _f = React.useState(false), initialized = _f[0], setInitialized = _f[1];
3203
+ 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"]);
3204
+ var _l = React.useState(false), initialized = _l[0], setInitialized = _l[1];
3072
3205
  var instance = useTransformContext();
3073
- var _g = React.useState(false), isDown = _g[0], setIsDown = _g[1];
3074
3206
  var mainRef = React.useRef(null);
3075
3207
  var wrapperRef = React.useRef(null);
3076
3208
  var previewRef = React.useRef(null);
3209
+ var maskPathRef = React.useRef(null);
3210
+ var dragRef = React.useRef(null);
3211
+ var layoutRef = React.useRef(null);
3212
+ // Latest props for the document listeners, which are registered once.
3213
+ var optionsRef = React.useRef({ panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick });
3214
+ optionsRef.current = { panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick };
3077
3215
  var getViewportSize = React.useCallback(function () {
3078
3216
  if (instance.wrapperComponent) {
3079
3217
  var rect = instance.wrapperComponent.getBoundingClientRect();
3080
- return {
3081
- width: rect.width,
3082
- height: rect.height,
3083
- };
3218
+ return { width: rect.width, height: rect.height };
3084
3219
  }
3085
- return {
3086
- width: 0,
3087
- height: 0,
3088
- };
3089
- }, [instance.wrapperComponent]);
3220
+ return { width: 0, height: 0 };
3221
+ }, [instance]);
3090
3222
  var getContentSize = React.useCallback(function () {
3091
3223
  if (instance.contentComponent) {
3092
3224
  var rect = instance.contentComponent.getBoundingClientRect();
3093
- return {
3094
- width: rect.width / instance.state.scale,
3095
- height: rect.height / instance.state.scale,
3096
- };
3225
+ var scale = instance.state.scale;
3226
+ return { width: rect.width / scale, height: rect.height / scale };
3097
3227
  }
3098
- return {
3099
- width: 0,
3100
- height: 0,
3101
- };
3102
- }, [instance.contentComponent, instance.state.scale]);
3103
- var computeMiniMapScale = React.useCallback(function () {
3104
- var contentSize = getContentSize();
3105
- var scaleX = width / contentSize.width;
3106
- var scaleY = height / contentSize.height;
3107
- var scale = scaleY > scaleX ? scaleX : scaleY;
3108
- return scale;
3109
- }, [getContentSize, height, width]);
3110
- var computeMiniMapSize = function () {
3111
- var contentSize = getContentSize();
3112
- var scaleX = width / contentSize.width;
3113
- var scaleY = height / contentSize.height;
3114
- if (scaleY > scaleX) {
3115
- return { width: width, height: contentSize.height * scaleX };
3116
- }
3117
- return { width: contentSize.width * scaleY, height: height };
3118
- };
3119
- var computeMiniMapStyle = function () {
3120
- var scale = computeMiniMapScale();
3121
- var style = {
3122
- transform: "scale(".concat(scale || 1, ")"),
3123
- transformOrigin: "0% 0%",
3124
- position: "absolute",
3125
- boxSizing: "border-box",
3126
- zIndex: 1,
3127
- // overflow: "hidden",
3128
- };
3129
- if (wrapperRef.current) {
3130
- var el = wrapperRef.current.style;
3131
- el.transform = style.transform;
3132
- el.transformOrigin = style.transformOrigin;
3133
- el.position = style.position;
3134
- el.boxSizing = style.boxSizing;
3135
- el.zIndex = String(style.zIndex);
3228
+ return { width: 0, height: 0 };
3229
+ }, [instance]);
3230
+ /**
3231
+ * Like React Flow, the map shows the union of the content and the current
3232
+ * viewport (plus `offsetScale` padding) fitted into the box, so the
3233
+ * viewport indicator never leaves the mini map even when the view is
3234
+ * panned or zoomed past the content.
3235
+ */
3236
+ var computeLayout = React.useCallback(function () {
3237
+ var content = getContentSize();
3238
+ var viewport = getViewportSize();
3239
+ var _a = instance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
3240
+ if (!content.width || !content.height || !width || !height || !scale) {
3241
+ return null;
3136
3242
  }
3137
- };
3138
- var transformMiniMap = function () {
3139
- computeMiniMapStyle();
3140
- var miniSize = computeMiniMapSize();
3141
- var wrapSize = getContentSize();
3243
+ var viewX = -positionX / scale;
3244
+ var viewY = -positionY / scale;
3245
+ var viewWidth = viewport.width / scale;
3246
+ var viewHeight = viewport.height / scale;
3247
+ var minX = Math.min(0, viewX);
3248
+ var minY = Math.min(0, viewY);
3249
+ var maxX = Math.max(content.width, viewX + viewWidth);
3250
+ var maxY = Math.max(content.height, viewY + viewHeight);
3251
+ var boundsWidth = maxX - minX;
3252
+ var boundsHeight = maxY - minY;
3253
+ var viewScale = Math.max(boundsWidth / width, boundsHeight / height);
3254
+ var fitWidth = viewScale * width;
3255
+ var fitHeight = viewScale * height;
3256
+ var offset = offsetScale * viewScale;
3257
+ var originX = minX - (fitWidth - boundsWidth) / 2 - offset;
3258
+ var originY = minY - (fitHeight - boundsHeight) / 2 - offset;
3259
+ var mapScale = width / (fitWidth + offset * 2);
3260
+ return {
3261
+ mapScale: mapScale,
3262
+ originX: originX,
3263
+ originY: originY,
3264
+ contentWidth: content.width,
3265
+ contentHeight: content.height,
3266
+ previewX: (viewX - originX) * mapScale,
3267
+ previewY: (viewY - originY) * mapScale,
3268
+ previewWidth: viewWidth * mapScale,
3269
+ previewHeight: viewHeight * mapScale,
3270
+ };
3271
+ }, [getContentSize, getViewportSize, height, instance, offsetScale, width]);
3272
+ var transformMiniMap = React.useCallback(function () {
3273
+ var layout = computeLayout();
3274
+ layoutRef.current = layout;
3275
+ if (!layout)
3276
+ return;
3277
+ var px = function (value) { return "".concat(roundNumber(value, 2), "px"); };
3142
3278
  if (wrapperRef.current) {
3143
- wrapperRef.current.style.width = "".concat(wrapSize.width, "px");
3144
- wrapperRef.current.style.height = "".concat(wrapSize.height, "px");
3145
- }
3146
- if (mainRef.current) {
3147
- mainRef.current.style.width = "".concat(miniSize.width, "px");
3148
- mainRef.current.style.height = "".concat(miniSize.height, "px");
3279
+ var style = wrapperRef.current.style;
3280
+ style.width = px(layout.contentWidth);
3281
+ style.height = px(layout.contentHeight);
3282
+ style.transform = "translate(".concat(px(-layout.originX * layout.mapScale), ", ").concat(px(-layout.originY * layout.mapScale), ") scale(").concat(roundNumber(layout.mapScale, 4), ")");
3149
3283
  }
3150
3284
  if (previewRef.current) {
3151
- var size = getViewportSize();
3152
- var scale = computeMiniMapScale();
3153
- var previewScale = scale * (1 / instance.state.scale);
3154
- var transform = instance.handleTransformStyles(-instance.state.positionX * previewScale, -instance.state.positionY * previewScale, 1);
3155
- previewRef.current.style.transform = transform;
3156
- previewRef.current.style.width = "".concat(size.width * previewScale, "px");
3157
- previewRef.current.style.height = "".concat(size.height * previewScale, "px");
3285
+ var style = previewRef.current.style;
3286
+ style.transform = "translate(".concat(px(layout.previewX), ", ").concat(px(layout.previewY), ")");
3287
+ style.width = px(layout.previewWidth);
3288
+ style.height = px(layout.previewHeight);
3158
3289
  }
3159
- };
3160
- var initialize = function () {
3161
- transformMiniMap();
3162
- };
3163
- useTransformEffect(function () {
3164
- transformMiniMap();
3165
- });
3290
+ if (maskPathRef.current) {
3291
+ var x = roundNumber(layout.previewX, 2);
3292
+ var y = roundNumber(layout.previewY, 2);
3293
+ var w = roundNumber(layout.previewWidth, 2);
3294
+ var h = roundNumber(layout.previewHeight, 2);
3295
+ 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"));
3296
+ }
3297
+ }, [computeLayout, height, width]);
3298
+ useTransformEffect(transformMiniMap);
3166
3299
  useTransformInit(function () {
3167
- initialize();
3300
+ transformMiniMap();
3168
3301
  setInitialized(true);
3169
3302
  });
3170
- useResize(instance.contentComponent, initialize, [initialized]);
3303
+ useResize(instance.contentComponent, transformMiniMap, [initialized]);
3304
+ React.useEffect(function () {
3305
+ transformMiniMap();
3306
+ }, [transformMiniMap]);
3171
3307
  React.useEffect(function () {
3172
- var _a, _b;
3173
- var move = function (e) {
3308
+ var element = mainRef.current;
3309
+ if (!element)
3310
+ return;
3311
+ var getMapScale = function () {
3174
3312
  var _a, _b;
3175
- if (panning &&
3176
- isDown &&
3177
- instance.contentComponent &&
3178
- previewRef.current &&
3179
- mainRef.current) {
3180
- var scale = computeMiniMapScale();
3181
- var previewRect = previewRef.current.getBoundingClientRect();
3182
- var mainRect = mainRef.current.getBoundingClientRect();
3183
- var relativeX = (e.clientX - mainRect.left) / scale;
3184
- var relativeY = (e.clientY - mainRect.top) / scale;
3185
- var x = relativeX - previewRect.width / 2;
3186
- var y = relativeY - previewRect.height / 2;
3187
- var instanceWidth = (((_a = instance.wrapperComponent) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0) * instance.state.scale;
3188
- var instanceHeight = (((_b = instance.wrapperComponent) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0) * instance.state.scale;
3189
- var limitWidth = instanceWidth - previewRect.width * 2 * instance.state.scale;
3190
- var limitHeight = instanceHeight - previewRect.height * 2 * instance.state.scale;
3191
- var boundedX = boundLimiter(x * instance.state.scale, 0, limitWidth, true);
3192
- var boundedY = boundLimiter(y * instance.state.scale, 0, limitHeight, true);
3193
- instance.setState(instance.state.scale, -boundedX, -boundedY);
3194
- }
3313
+ var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
3314
+ return (_b = layout === null || layout === void 0 ? void 0 : layout.mapScale) !== null && _b !== void 0 ? _b : 0;
3195
3315
  };
3196
- var setDown = function (e) {
3316
+ var start = function (point, target) {
3317
+ if (!optionsRef.current.panning)
3318
+ return;
3319
+ if (!isInside(element, target))
3320
+ return;
3321
+ if (!instance.wrapperComponent || !instance.contentComponent)
3322
+ return;
3323
+ handleCancelAnimation(instance);
3324
+ dragRef.current = { x: point.x, y: point.y, moved: false };
3325
+ element.style.cursor = "grabbing";
3326
+ };
3327
+ var move = function (point) {
3328
+ var drag = dragRef.current;
3329
+ if (!drag)
3330
+ return;
3331
+ var deltaX = point.x - drag.x;
3332
+ var deltaY = point.y - drag.y;
3333
+ drag.x = point.x;
3334
+ drag.y = point.y;
3335
+ if (!deltaX && !deltaY)
3336
+ return;
3337
+ drag.moved = true;
3338
+ var mapScale = getMapScale();
3339
+ var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
3340
+ if (!mapScale || !wrapperComponent || !contentComponent)
3341
+ return;
3342
+ // Dragging by one mini map px moves the viewport by 1 / mapScale
3343
+ // content units, so the indicator follows the pointer 1:1.
3344
+ var direction = optionsRef.current.inversePan ? 1 : -1;
3345
+ var moveScale = (direction * state.scale) / mapScale;
3346
+ var nextX = state.positionX + deltaX * moveScale;
3347
+ var nextY = state.positionY + deltaY * moveScale;
3348
+ var bounds = handleCalculateBounds(instance, state.scale);
3349
+ var _a = getMouseBoundedPosition(nextX, nextY, bounds, setup.limitToBounds, 0, 0, wrapperComponent), x = _a.x, y = _a.y;
3350
+ if (x === state.positionX && y === state.positionY)
3351
+ return;
3352
+ instance.setState(state.scale, x, y);
3353
+ };
3354
+ var stop = function (event, point) {
3197
3355
  var _a;
3198
- if (((_a = mainRef.current) === null || _a === void 0 ? void 0 : _a.contains(e.target)) ||
3199
- e.target === mainRef.current) {
3200
- move(e);
3201
- setIsDown(true);
3356
+ var drag = dragRef.current;
3357
+ if (!drag)
3358
+ return;
3359
+ dragRef.current = null;
3360
+ element.style.cursor = optionsRef.current.panning ? "grab" : "";
3361
+ var handleClick = optionsRef.current.onClick;
3362
+ if (drag.moved || !handleClick || !point)
3363
+ return;
3364
+ var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
3365
+ if (!layout)
3366
+ return;
3367
+ var rect = element.getBoundingClientRect();
3368
+ handleClick(event, {
3369
+ x: layout.originX + (point.x - rect.left) / layout.mapScale,
3370
+ y: layout.originY + (point.y - rect.top) / layout.mapScale,
3371
+ });
3372
+ };
3373
+ var onMouseDown = function (event) {
3374
+ if (event.button !== 0)
3375
+ return;
3376
+ start({ x: event.clientX, y: event.clientY }, event.target);
3377
+ };
3378
+ var onMouseMove = function (event) {
3379
+ if (!dragRef.current)
3380
+ return;
3381
+ // The mouse was released outside the window: end the drag.
3382
+ if (event.buttons === 0) {
3383
+ stop(event, null);
3384
+ return;
3385
+ }
3386
+ move({ x: event.clientX, y: event.clientY });
3387
+ };
3388
+ var onMouseUp = function (event) {
3389
+ stop(event, { x: event.clientX, y: event.clientY });
3390
+ };
3391
+ var onTouchStart = function (event) {
3392
+ if (event.touches.length !== 1)
3393
+ return;
3394
+ var point = getTouchPoint(event);
3395
+ if (point)
3396
+ start(point, event.target);
3397
+ };
3398
+ var onTouchMove = function (event) {
3399
+ if (!dragRef.current)
3400
+ return;
3401
+ if (event.touches.length !== 1) {
3402
+ stop(event, null);
3403
+ return;
3202
3404
  }
3405
+ var point = getTouchPoint(event);
3406
+ if (point)
3407
+ move(point);
3203
3408
  };
3204
- var setUp = function () {
3205
- setIsDown(false);
3409
+ var onTouchEnd = function (event) {
3410
+ stop(event, getTouchPoint(event));
3206
3411
  };
3207
3412
  // Listen on the mini map's own document so it keeps working inside
3208
3413
  // portal windows and iframes.
3209
- var doc = (_b = (_a = mainRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) !== null && _b !== void 0 ? _b : document;
3210
- doc.addEventListener("mousedown", setDown);
3211
- doc.addEventListener("mouseup", setUp);
3212
- doc.addEventListener("mousemove", move);
3414
+ var doc = element.ownerDocument;
3415
+ doc.addEventListener("mousedown", onMouseDown);
3416
+ doc.addEventListener("mousemove", onMouseMove);
3417
+ doc.addEventListener("mouseup", onMouseUp);
3418
+ doc.addEventListener("touchstart", onTouchStart);
3419
+ doc.addEventListener("touchmove", onTouchMove);
3420
+ doc.addEventListener("touchend", onTouchEnd);
3421
+ doc.addEventListener("touchcancel", onTouchEnd);
3213
3422
  return function () {
3214
- doc.removeEventListener("mousedown", setDown);
3215
- doc.removeEventListener("mousemove", move);
3216
- doc.removeEventListener("mouseup", setUp);
3423
+ doc.removeEventListener("mousedown", onMouseDown);
3424
+ doc.removeEventListener("mousemove", onMouseMove);
3425
+ doc.removeEventListener("mouseup", onMouseUp);
3426
+ doc.removeEventListener("touchstart", onTouchStart);
3427
+ doc.removeEventListener("touchmove", onTouchMove);
3428
+ doc.removeEventListener("touchend", onTouchEnd);
3429
+ doc.removeEventListener("touchcancel", onTouchEnd);
3430
+ dragRef.current = null;
3431
+ };
3432
+ }, [computeLayout, instance]);
3433
+ React.useEffect(function () {
3434
+ var element = mainRef.current;
3435
+ if (!element || !zoomable)
3436
+ return;
3437
+ var onWheel = function (event) {
3438
+ var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
3439
+ if (setup.disabled || !wrapperComponent || !contentComponent)
3440
+ return;
3441
+ if (!event.deltaY)
3442
+ return;
3443
+ if (event.cancelable)
3444
+ event.preventDefault();
3445
+ event.stopPropagation();
3446
+ var delta = event.deltaY < 0 ? 1 : -1;
3447
+ var newScale = handleCalculateButtonZoom(instance, delta, optionsRef.current.zoomStep);
3448
+ if (newScale === state.scale)
3449
+ return;
3450
+ // Zoom around the centre of the current viewport.
3451
+ var centerX = (wrapperComponent.offsetWidth / 2 - state.positionX) / state.scale;
3452
+ var centerY = (wrapperComponent.offsetHeight / 2 - state.positionY) / state.scale;
3453
+ handleCancelAnimation(instance);
3454
+ var target = handleZoomToPoint(instance, newScale, centerX, centerY);
3455
+ if (!target)
3456
+ return;
3457
+ instance.setState(target.scale, target.positionX, target.positionY);
3217
3458
  };
3218
- }, [computeMiniMapScale, instance, isDown, panning]);
3219
- var wrapperStyle = React.useMemo(function () {
3220
- return {
3221
- position: "relative",
3222
- zIndex: 2,
3223
- // overflow: "hidden",
3224
- userSelect: "none",
3459
+ element.addEventListener("wheel", onWheel, { passive: false });
3460
+ return function () {
3461
+ element.removeEventListener("wheel", onWheel);
3225
3462
  };
3226
- }, []);
3227
- return (jsxRuntime.jsxs("div", __assign({}, rest, { ref: mainRef, style: wrapperStyle, className: "rzpp-mini-map ".concat(rest.className || "").trim() }, { children: [jsxRuntime.jsx("div", __assign({ style: { pointerEvents: "none" }, ref: wrapperRef, className: "rzpp-minimap-wrapper ".concat(wrapperClassName || "").trim() }, { children: children })), jsxRuntime.jsx("div", { className: "rzpp-minimap-preview ".concat(previewClassName || "").trim(), ref: previewRef, style: __assign(__assign(__assign({}, previewStyles), { borderColor: borderColor }), previewStyle) })] })));
3463
+ }, [instance, zoomable]);
3464
+ 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);
3465
+ return (jsxRuntime.jsxs("div", __assign({}, rest, { ref: mainRef, style: containerStyle, className: "rzpp-mini-map ".concat(rest.className || "").trim() }, { children: [jsxRuntime.jsx("div", __assign({ style: wrapperStyles, ref: wrapperRef, className: "rzpp-minimap-wrapper ".concat(wrapperClassName || "").trim() }, { children: children })), jsxRuntime.jsx("svg", __assign({ className: "rzpp-minimap-mask", style: maskStyles, width: width, height: height, viewBox: "0 0 ".concat(width, " ").concat(height), "aria-hidden": "true" }, { children: jsxRuntime.jsx("path", { ref: maskPathRef, fill: maskColor, fillRule: "evenodd", d: "M0 0H".concat(width, "V").concat(height, "H0Z") }) })), jsxRuntime.jsx("div", { className: "rzpp-minimap-preview ".concat(previewClassName || "").trim(), ref: previewRef, style: __assign(__assign(__assign({}, previewStyles), { borderColor: borderColor }), previewStyle) })] })));
3228
3466
  };
3229
3467
 
3230
3468
  function styleInject(css, ref) {
@@ -3447,13 +3685,37 @@ var useTransformInit = function (callback) {
3447
3685
  }, []);
3448
3686
  };
3449
3687
 
3688
+ var takeSnapshot = function (state) { return ({
3689
+ scale: state.scale,
3690
+ positionX: state.positionX,
3691
+ positionY: state.positionY,
3692
+ }); };
3693
+ var isSameSnapshot = function (snapshot, state) {
3694
+ return snapshot !== null &&
3695
+ snapshot.scale === state.scale &&
3696
+ snapshot.positionX === state.positionX &&
3697
+ snapshot.positionY === state.positionY;
3698
+ };
3699
+
3450
3700
  var useTransformEffect = function (callback) {
3451
3701
  var libraryContext = useTransformContext();
3702
+ // Starts as the state the consumer rendered with, so a transform applied
3703
+ // during mount (initialPosition*, centerOnInit, fitOnInit) — which happens
3704
+ // in a layout effect, before this subscription exists — is delivered once.
3705
+ var lastSeen = React.useRef(null);
3706
+ if (lastSeen.current === null) {
3707
+ lastSeen.current = takeSnapshot(libraryContext.state);
3708
+ }
3452
3709
  React.useEffect(function () {
3453
3710
  var unmountCallback;
3454
3711
  var unmount = libraryContext.onChange(function (ref) {
3712
+ lastSeen.current = takeSnapshot(ref.instance.state);
3455
3713
  unmountCallback = callback(getState(ref.instance));
3456
3714
  });
3715
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
3716
+ lastSeen.current = takeSnapshot(libraryContext.state);
3717
+ unmountCallback = callback(getState(libraryContext));
3718
+ }
3457
3719
  return function () {
3458
3720
  unmount();
3459
3721
  unmountCallback === null || unmountCallback === void 0 ? void 0 : unmountCallback();
@@ -3463,16 +3725,27 @@ var useTransformEffect = function (callback) {
3463
3725
 
3464
3726
  function useTransformComponent(callback) {
3465
3727
  var libraryContext = useTransformContext();
3728
+ var lastSeen = React.useRef(null);
3466
3729
  var _a = React.useState(function () {
3730
+ lastSeen.current = takeSnapshot(libraryContext.state);
3467
3731
  return callback(getState(libraryContext));
3468
3732
  }), transformRender = _a[0], setTransformRender = _a[1];
3469
3733
  React.useEffect(function () {
3470
3734
  var mounted = true;
3471
3735
  var unmount = libraryContext.onChange(function (ref) {
3472
3736
  if (mounted) {
3737
+ lastSeen.current = takeSnapshot(ref.instance.state);
3473
3738
  setTransformRender(callback(getState(ref.instance)));
3474
3739
  }
3475
3740
  });
3741
+ // The instance initializes in a layout effect, so a transform applied on
3742
+ // mount (initialPosition*, centerOnInit, fitOnInit) can land before this
3743
+ // subscription exists. Catch up when the state moved since the last
3744
+ // render; the snapshot check keeps this from re-rendering in a loop.
3745
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
3746
+ lastSeen.current = takeSnapshot(libraryContext.state);
3747
+ setTransformRender(callback(getState(libraryContext)));
3748
+ }
3476
3749
  return function () {
3477
3750
  unmount();
3478
3751
  mounted = false;