react-zoom-pan-pinch 4.0.6 → 4.0.7

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.d.ts CHANGED
@@ -77,6 +77,8 @@ declare class ZoomPanPinch {
77
77
  lastMousePosition: PositionType | null;
78
78
  isAnimating: boolean;
79
79
  animation: AnimationType | null;
80
+ animationFrame: number | null;
81
+ initObserverTimer: ReturnType<typeof setTimeout> | null;
80
82
  pressedKeys: {
81
83
  [key: string]: boolean;
82
84
  };
@@ -84,8 +86,10 @@ declare class ZoomPanPinch {
84
86
  mount: () => void;
85
87
  unmount: () => void;
86
88
  update: (newProps: ReactZoomPanPinchProps) => void;
89
+ clearTimers: () => void;
87
90
  initializeWindowEvents: () => void;
88
91
  cleanupWindowEvents: () => void;
92
+ cleanupWrapperEvents: () => void;
89
93
  handleInitializeWrapperEvents: (wrapper: HTMLDivElement) => void;
90
94
  handleInitialize: (contentComponent: HTMLDivElement) => void;
91
95
  onWheelZoom: (event: WheelEvent) => void;
package/dist/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useRef, useImperativeHandle, useEffect, useContext, useLayoutEffect, useState, useCallback, useMemo } from 'react';
1
+ import React, { useLayoutEffect, useEffect, useRef, useImperativeHandle, useContext, useState, useCallback, useMemo } from 'react';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
3
 
4
4
  /**
@@ -102,16 +102,12 @@ var animations = {
102
102
  };
103
103
 
104
104
  /* eslint-disable no-param-reassign */
105
- var handleCancelAnimationFrame = function (animation) {
106
- if (typeof animation === "number") {
107
- cancelAnimationFrame(animation);
108
- }
109
- };
110
105
  var handleCancelAnimation = function (contextInstance) {
111
- if (!contextInstance.mounted)
112
- return;
113
- handleCancelAnimationFrame(contextInstance.animation);
106
+ if (contextInstance.animationFrame !== null) {
107
+ cancelAnimationFrame(contextInstance.animationFrame);
108
+ }
114
109
  // Clear animation state
110
+ contextInstance.animationFrame = null;
115
111
  contextInstance.isAnimating = false;
116
112
  contextInstance.animation = null;
117
113
  contextInstance.velocity = null;
@@ -124,24 +120,31 @@ function handleSetupAnimation(contextInstance, animationName, animationTime, cal
124
120
  // if another animation is active
125
121
  handleCancelAnimation(contextInstance);
126
122
  // new animation
127
- contextInstance.animation = function () {
128
- if (!contextInstance.mounted) {
129
- return handleCancelAnimationFrame(contextInstance.animation);
123
+ var animation = function () {
124
+ // A frame that was already queued when this animation got cancelled or
125
+ // replaced must not run: it would apply a stale target and re-schedule
126
+ // the replacement animation a second time per frame.
127
+ if (!contextInstance.mounted || contextInstance.animation !== animation) {
128
+ return;
130
129
  }
131
130
  var frameTime = new Date().getTime() - startTime;
132
131
  var animationProgress = frameTime / animationTime;
133
132
  var animationType = animations[animationName];
134
133
  var step = animationType(animationProgress);
135
134
  if (frameTime >= animationTime) {
136
- callback(lastStep);
135
+ // Clear before the final step so a callback that starts a new
136
+ // animation (e.g. from onTransform) is not wiped out afterwards.
137
137
  contextInstance.animation = null;
138
+ contextInstance.animationFrame = null;
139
+ callback(lastStep);
138
140
  }
139
- else if (contextInstance.animation) {
141
+ else {
140
142
  callback(step);
141
- requestAnimationFrame(contextInstance.animation);
143
+ contextInstance.animationFrame = requestAnimationFrame(animation);
142
144
  }
143
145
  };
144
- requestAnimationFrame(contextInstance.animation);
146
+ contextInstance.animation = animation;
147
+ contextInstance.animationFrame = requestAnimationFrame(animation);
145
148
  }
146
149
  function isValidTargetState(targetState) {
147
150
  var scale = targetState.scale, positionX = targetState.positionX, positionY = targetState.positionY;
@@ -332,9 +335,7 @@ var isPanningStartAllowed = function (contextInstance, event) {
332
335
  var isExcluded = isExcludedNode(target, excluded);
333
336
  if (isExcluded)
334
337
  return false;
335
- if (target.getAttribute("draggable") === "true" ||
336
- target.getAttribute("contenteditable") === "true" ||
337
- target.isContentEditable) {
338
+ if (isDraggableTarget(target) || isEditableTarget(target)) {
338
339
  return false;
339
340
  }
340
341
  return true;
@@ -876,14 +877,15 @@ var createSetup = function (props) {
876
877
  var handleCalculateButtonZoom = function (contextInstance, delta, step) {
877
878
  var scale = contextInstance.state.scale;
878
879
  var wrapperComponent = contextInstance.wrapperComponent, setup = contextInstance.setup;
879
- var maxScale = setup.maxScale, minScale = setup.minScale, zoomAnimation = setup.zoomAnimation, smooth = setup.smooth;
880
+ var maxScale = setup.maxScale, minScale = setup.minScale, zoomAnimation = setup.zoomAnimation;
880
881
  var size = zoomAnimation.size;
881
882
  if (!wrapperComponent) {
882
883
  throw new Error("Wrapper is not mounted");
883
884
  }
884
- var targetScale = smooth
885
- ? scale * Math.exp(delta * step)
886
- : scale + delta * step;
885
+ // `step` is an absolute scale increment for zoomIn/zoomOut and double
886
+ // click: zoomIn(0.25) from 1 lands on 1.25. The `smooth` option only
887
+ // shapes wheel/trackpad deltas (#545, #431).
888
+ var targetScale = scale + delta * step;
887
889
  var newScale = checkZoomBounds(roundNumber(targetScale, 3), minScale, maxScale, size, false);
888
890
  return newScale;
889
891
  };
@@ -1052,13 +1054,19 @@ var centerView = function (contextInstance) {
1052
1054
  };
1053
1055
  var zoomToElement = function (contextInstance) {
1054
1056
  return function (node, scale, animationTime, animationType, offsetX, offsetY) {
1057
+ var _a, _b;
1055
1058
  if (animationTime === void 0) { animationTime = 600; }
1056
1059
  if (animationType === void 0) { animationType = "easeOut"; }
1057
1060
  if (offsetX === void 0) { offsetX = 0; }
1058
1061
  if (offsetY === void 0) { offsetY = 0; }
1059
1062
  handleCancelAnimation(contextInstance);
1060
1063
  var wrapperComponent = contextInstance.wrapperComponent;
1061
- var target = typeof node === "string" ? document.getElementById(node) : node;
1064
+ // Resolve ids in the wrapper's own document so it works inside portal
1065
+ // windows and iframes, not only the top-level document (#290).
1066
+ var ownerDocument = (_a = wrapperComponent === null || wrapperComponent === void 0 ? void 0 : wrapperComponent.ownerDocument) !== null && _a !== void 0 ? _a : (typeof document !== "undefined" ? document : null);
1067
+ var target = typeof node === "string"
1068
+ ? (_b = ownerDocument === null || ownerDocument === void 0 ? void 0 : ownerDocument.getElementById(node)) !== null && _b !== void 0 ? _b : null
1069
+ : node;
1062
1070
  if (wrapperComponent && target && wrapperComponent.contains(target)) {
1063
1071
  var targetState = calculateZoomToNode(contextInstance, target, scale, offsetX, offsetY);
1064
1072
  animate(contextInstance, targetState, animationTime, animationType);
@@ -1119,6 +1127,42 @@ var isExcludedNode = function (node, excluded) {
1119
1127
  return node.matches("".concat(matchPrefix, " ").concat(exclude, ", ").concat(matchPrefix, " .").concat(exclude, ", ").concat(matchPrefix, " ").concat(exclude, " *, ").concat(matchPrefix, " .").concat(exclude, " *"));
1120
1128
  });
1121
1129
  };
1130
+ var EDITABLE_TAGS = new Set(["INPUT", "TEXTAREA", "SELECT", "OPTION"]);
1131
+ var CONTENT_EDITABLE_SELECTOR = '[contenteditable=""], [contenteditable="true"], [contenteditable="plaintext-only"]';
1132
+ var DRAGGABLE_SELECTOR = '[draggable="true"]';
1133
+ var asElement = function (node) {
1134
+ // Duck-typed on purpose: `instanceof Element` fails for nodes that belong
1135
+ // to another window (portals, popups), see #290.
1136
+ if (!node || typeof node.closest !== "function") {
1137
+ return null;
1138
+ }
1139
+ return node;
1140
+ };
1141
+ /**
1142
+ * Form controls and contenteditable regions (or anything nested inside them).
1143
+ * Starting a pan or a double-click zoom from these would steal focus and block
1144
+ * native text editing (#437, #544).
1145
+ */
1146
+ var isEditableTarget = function (node) {
1147
+ var element = asElement(node);
1148
+ if (!element)
1149
+ return false;
1150
+ if (EDITABLE_TAGS.has(element.tagName))
1151
+ return true;
1152
+ if (element.isContentEditable)
1153
+ return true;
1154
+ return element.closest(CONTENT_EDITABLE_SELECTOR) !== null;
1155
+ };
1156
+ /**
1157
+ * HTML5 drag sources (or anything nested inside them). A pan started here
1158
+ * would call `preventDefault` on mousedown and cancel the native drag (#460).
1159
+ */
1160
+ var isDraggableTarget = function (node) {
1161
+ var element = asElement(node);
1162
+ if (!element)
1163
+ return false;
1164
+ return element.closest(DRAGGABLE_SELECTOR) !== null;
1165
+ };
1122
1166
  var cancelTimeout = function (timeout) {
1123
1167
  if (timeout) {
1124
1168
  clearTimeout(timeout);
@@ -1180,6 +1224,15 @@ function mergeRefs(refs) {
1180
1224
  };
1181
1225
  }
1182
1226
 
1227
+ /**
1228
+ * `useLayoutEffect` in the browser, `useEffect` during server rendering.
1229
+ *
1230
+ * DOM measurements and the initial transform must run before the first paint
1231
+ * (otherwise `centerOnInit`, `initialPosition*` and virtualized children flash
1232
+ * into place one frame late), but `useLayoutEffect` warns on the server.
1233
+ */
1234
+ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
1235
+
1183
1236
  var isWheelAllowed = function (contextInstance, event) {
1184
1237
  var _a = contextInstance.setup.wheel, disabled = _a.disabled, wheelDisabled = _a.wheelDisabled, touchPadDisabled = _a.touchPadDisabled, excluded = _a.excluded;
1185
1238
  var isInitialized = contextInstance.isInitialized, isPanning = contextInstance.isPanning;
@@ -1187,6 +1240,11 @@ var isWheelAllowed = function (contextInstance, event) {
1187
1240
  var isAllowed = isInitialized && !isPanning && !disabled && target;
1188
1241
  if (!isAllowed)
1189
1242
  return false;
1243
+ // A purely horizontal wheel/swipe (Magic Mouse, trackpad) carries no zoom
1244
+ // intent. Treating its deltaY of 0 as "zoom out" was #168; leaving it
1245
+ // unclaimed also lets trackpad panning pick it up.
1246
+ if (event.deltaY === 0)
1247
+ return false;
1190
1248
  // Event ctrlKey detects if touchpad action is executing wheel or pinch gesture
1191
1249
  if (wheelDisabled && !event.ctrlKey)
1192
1250
  return false;
@@ -1413,6 +1471,9 @@ var handleWheelPanningStart = function (contextInstance, event) {
1413
1471
  var _a = contextInstance.props, onWheelStart = _a.onWheelStart, onPanningStart = _a.onPanningStart;
1414
1472
  if (!contextInstance.wheelStopEventTimer) {
1415
1473
  handleCancelAnimation(contextInstance);
1474
+ // Same as a mouse pan start: bounds must reflect the current scale and
1475
+ // content size, not the ones measured at mount (#396, #433).
1476
+ handleCalculateBounds(contextInstance, contextInstance.state.scale);
1416
1477
  handleCallback(getContext(contextInstance), event, onWheelStart);
1417
1478
  handleCallback(getContext(contextInstance), event, onPanningStart);
1418
1479
  }
@@ -1523,6 +1584,8 @@ var handleDoubleClickStop = function (contextInstance, event) {
1523
1584
  cancelTimeout(contextInstance.doubleClickStopEventTimer);
1524
1585
  contextInstance.doubleClickStopEventTimer = setTimeout(function () {
1525
1586
  contextInstance.doubleClickStopEventTimer = null;
1587
+ if (!contextInstance.mounted)
1588
+ return;
1526
1589
  handleCallback(getContext(contextInstance), event, onZoomStop);
1527
1590
  }, animationTime);
1528
1591
  };
@@ -1581,6 +1644,9 @@ var isDoubleClickAllowed = function (contextInstance, event) {
1581
1644
  var isExcluded = isExcludedNode(target, excluded);
1582
1645
  if (isExcluded)
1583
1646
  return false;
1647
+ // Double-clicking a word inside an input selects it; it must not zoom.
1648
+ if (isEditableTarget(target))
1649
+ return false;
1584
1650
  return true;
1585
1651
  };
1586
1652
 
@@ -1624,20 +1690,44 @@ var ZoomPanPinch = /** @class */ (function () {
1624
1690
  // animations helpers
1625
1691
  this.isAnimating = false;
1626
1692
  this.animation = null;
1693
+ this.animationFrame = null;
1694
+ // init helpers
1695
+ this.initObserverTimer = null;
1627
1696
  // key press
1628
1697
  this.pressedKeys = {};
1629
1698
  this.mount = function () {
1699
+ _this.mounted = true;
1630
1700
  _this.initializeWindowEvents();
1631
1701
  };
1702
+ // Marks the instance as gone so pending timers, animation frames and
1703
+ // callbacks scheduled before unmount become no-ops.
1632
1704
  this.unmount = function () {
1705
+ _this.mounted = false;
1633
1706
  _this.cleanupWindowEvents();
1634
1707
  };
1635
1708
  this.update = function (newProps) {
1636
1709
  _this.props = newProps;
1710
+ // Setup first: bounds depend on the new props (min/max positions,
1711
+ // centerZoomedOut, disablePadding).
1712
+ _this.setup = createSetup(newProps);
1637
1713
  if (_this.wrapperComponent && _this.contentComponent) {
1638
1714
  handleCalculateBounds(_this, _this.state.scale);
1639
1715
  }
1640
- _this.setup = createSetup(newProps);
1716
+ };
1717
+ this.clearTimers = function () {
1718
+ if (_this.wheelStopEventTimer)
1719
+ clearTimeout(_this.wheelStopEventTimer);
1720
+ if (_this.wheelAnimationTimer)
1721
+ clearTimeout(_this.wheelAnimationTimer);
1722
+ if (_this.doubleClickStopEventTimer) {
1723
+ clearTimeout(_this.doubleClickStopEventTimer);
1724
+ }
1725
+ if (_this.initObserverTimer)
1726
+ clearTimeout(_this.initObserverTimer);
1727
+ _this.wheelStopEventTimer = null;
1728
+ _this.wheelAnimationTimer = null;
1729
+ _this.doubleClickStopEventTimer = null;
1730
+ _this.initObserverTimer = null;
1641
1731
  };
1642
1732
  this.initializeWindowEvents = function () {
1643
1733
  var _a, _b, _c, _d;
@@ -1668,13 +1758,25 @@ var ZoomPanPinch = /** @class */ (function () {
1668
1758
  currentWindow === null || currentWindow === void 0 ? void 0 : currentWindow.removeEventListener("keyup", _this.setKeyUnPressed, passive);
1669
1759
  currentWindow === null || currentWindow === void 0 ? void 0 : currentWindow.removeEventListener("keydown", _this.setKeyPressed, passive);
1670
1760
  currentWindow === null || currentWindow === void 0 ? void 0 : currentWindow.removeEventListener("blur", _this.handleWindowBlur);
1671
- document.removeEventListener("mouseleave", _this.clearPanning, passive);
1672
1761
  (_b = _this.wrapperComponent) === null || _b === void 0 ? void 0 : _b.removeEventListener("wheel", _this.onWheelPanning, passive);
1673
1762
  (_c = _this.wrapperComponent) === null || _c === void 0 ? void 0 : _c.removeEventListener("keyup", _this.setKeyUnPressed, passive);
1674
1763
  (_d = _this.wrapperComponent) === null || _d === void 0 ? void 0 : _d.removeEventListener("keydown", _this.setKeyPressed, passive);
1764
+ _this.cleanupWrapperEvents();
1765
+ _this.clearTimers();
1675
1766
  handleCancelAnimation(_this);
1676
1767
  (_e = _this.observer) === null || _e === void 0 ? void 0 : _e.disconnect();
1677
1768
  };
1769
+ this.cleanupWrapperEvents = function () {
1770
+ var wrapper = _this.wrapperComponent;
1771
+ if (!wrapper)
1772
+ return;
1773
+ var passive = makePassiveEventOption();
1774
+ wrapper.removeEventListener("wheel", _this.onWheelZoom, passive);
1775
+ wrapper.removeEventListener("dblclick", _this.onDoubleClick, passive);
1776
+ wrapper.removeEventListener("touchstart", _this.onTouchPanningStart, passive);
1777
+ wrapper.removeEventListener("touchmove", _this.onTouchPanning, passive);
1778
+ wrapper.removeEventListener("touchend", _this.onTouchPanningStop, passive);
1779
+ };
1678
1780
  this.handleInitializeWrapperEvents = function (wrapper) {
1679
1781
  // Zooming events on wrapper
1680
1782
  var passive = makePassiveEventOption();
@@ -1685,11 +1787,13 @@ var ZoomPanPinch = /** @class */ (function () {
1685
1787
  wrapper.addEventListener("touchend", _this.onTouchPanningStop, passive);
1686
1788
  };
1687
1789
  this.handleInitialize = function (contentComponent) {
1790
+ var _a;
1688
1791
  var centerOnInit = _this.setup.centerOnInit;
1689
1792
  _this.applyTransformation();
1690
1793
  _this.onInitCallbacks.forEach(function (callback) { return callback(getContext(_this)); });
1691
1794
  if (centerOnInit) {
1692
1795
  _this.setCenter();
1796
+ (_a = _this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
1693
1797
  _this.observer = new ResizeObserver(function () {
1694
1798
  var _a;
1695
1799
  var currentWidth = contentComponent.offsetWidth;
@@ -1704,8 +1808,11 @@ var ZoomPanPinch = /** @class */ (function () {
1704
1808
  });
1705
1809
  // TODO: CHANGE to first interaction?
1706
1810
  // if nothing about the contentComponent has changed after 5 seconds, disconnect the observer
1707
- setTimeout(function () {
1811
+ if (_this.initObserverTimer)
1812
+ clearTimeout(_this.initObserverTimer);
1813
+ _this.initObserverTimer = setTimeout(function () {
1708
1814
  var _a;
1815
+ _this.initObserverTimer = null;
1709
1816
  (_a = _this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
1710
1817
  }, 5000);
1711
1818
  // Start observing the target node for configured mutations
@@ -1867,6 +1974,9 @@ var ZoomPanPinch = /** @class */ (function () {
1867
1974
  var isDoubleTapAllowed = !(doubleClick === null || doubleClick === void 0 ? void 0 : doubleClick.disabled);
1868
1975
  var isDoubleTap = _this.lastTouch && +new Date() - _this.lastTouch < 200;
1869
1976
  if (isDoubleTapAllowed && isDoubleTap && event.touches.length === 1) {
1977
+ // Consume the pair so a third quick tap starts a fresh sequence
1978
+ // instead of counting as another double tap.
1979
+ _this.lastTouch = null;
1870
1980
  _this.onDoubleClick(event);
1871
1981
  }
1872
1982
  else {
@@ -2080,6 +2190,7 @@ var ZoomPanPinch = /** @class */ (function () {
2080
2190
  * Initialization
2081
2191
  */
2082
2192
  this.init = function (wrapperComponent, contentComponent) {
2193
+ _this.mounted = true;
2083
2194
  _this.cleanupWindowEvents();
2084
2195
  _this.wrapperComponent = wrapperComponent;
2085
2196
  _this.contentComponent = contentComponent;
@@ -2107,9 +2218,21 @@ var getContent = function (children, ctx) {
2107
2218
  return children;
2108
2219
  };
2109
2220
  var TransformWrapper = React.forwardRef(function (props, ref) {
2110
- var instance = useRef(new ZoomPanPinch(props)).current;
2221
+ // Lazy so a fresh (immediately discarded) instance is not built on every
2222
+ // render.
2223
+ var instanceRef = useRef(null);
2224
+ if (instanceRef.current === null) {
2225
+ instanceRef.current = new ZoomPanPinch(props);
2226
+ }
2227
+ var instance = instanceRef.current;
2111
2228
  var content = getContent(props.children, getControls(instance));
2112
2229
  useImperativeHandle(ref, function () { return getControls(instance); }, [instance]);
2230
+ useEffect(function () {
2231
+ instance.mount();
2232
+ return function () {
2233
+ instance.unmount();
2234
+ };
2235
+ }, [instance]);
2113
2236
  useEffect(function () {
2114
2237
  instance.update(props);
2115
2238
  }, [instance, props]);
@@ -2119,13 +2242,19 @@ var TransformWrapper = React.forwardRef(function (props, ref) {
2119
2242
  var KeepScale = React.forwardRef(function (props, ref) {
2120
2243
  var localRef = useRef(null);
2121
2244
  var instance = useContext(Context);
2122
- useEffect(function () {
2123
- return instance.onChange(function (ctx) {
2245
+ useIsomorphicLayoutEffect(function () {
2246
+ var applyInverseScale = function (scale) {
2124
2247
  if (localRef.current) {
2125
2248
  var positionX = 0;
2126
2249
  var positionY = 0;
2127
- localRef.current.style.transform = instance.handleTransformStyles(positionX, positionY, 1 / ctx.instance.state.scale);
2250
+ localRef.current.style.transform = instance.handleTransformStyles(positionX, positionY, 1 / scale);
2128
2251
  }
2252
+ };
2253
+ // Apply immediately: an element mounted while already zoomed must not
2254
+ // wait for the next transform change to get its counter-scale.
2255
+ applyInverseScale(instance.state.scale);
2256
+ return instance.onChange(function (ctx) {
2257
+ applyInverseScale(ctx.instance.state.scale);
2129
2258
  });
2130
2259
  }, [instance]);
2131
2260
  return jsx("div", __assign({}, props, { ref: mergeRefs([localRef, ref]) }));
@@ -2192,7 +2321,6 @@ var MiniMap = function (_a) {
2192
2321
  var _f = useState(false), initialized = _f[0], setInitialized = _f[1];
2193
2322
  var instance = useTransformContext();
2194
2323
  var _g = useState(false), isDown = _g[0], setIsDown = _g[1];
2195
- var miniMapInstance = useRef(null);
2196
2324
  var mainRef = useRef(null);
2197
2325
  var wrapperRef = useRef(null);
2198
2326
  var previewRef = useRef(null);
@@ -2291,18 +2419,7 @@ var MiniMap = function (_a) {
2291
2419
  });
2292
2420
  useResize(instance.contentComponent, initialize, [initialized]);
2293
2421
  useEffect(function () {
2294
- return instance.onChange(function (zpp) {
2295
- var scale = computeMiniMapScale();
2296
- if (miniMapInstance.current) {
2297
- miniMapInstance.current.instance.state.scale = zpp.instance.state.scale;
2298
- miniMapInstance.current.instance.state.positionX =
2299
- zpp.instance.state.positionX * scale;
2300
- miniMapInstance.current.instance.state.positionY =
2301
- zpp.instance.state.positionY * scale;
2302
- }
2303
- });
2304
- }, [computeMiniMapScale, instance, miniMapInstance]);
2305
- useEffect(function () {
2422
+ var _a, _b;
2306
2423
  var move = function (e) {
2307
2424
  var _a, _b;
2308
2425
  if (panning &&
@@ -2337,14 +2454,18 @@ var MiniMap = function (_a) {
2337
2454
  var setUp = function () {
2338
2455
  setIsDown(false);
2339
2456
  };
2340
- document.addEventListener("mousedown", setDown);
2341
- document.addEventListener("mouseup", setUp);
2342
- document.addEventListener("mousemove", move);
2457
+ // Listen on the mini map's own document so it keeps working inside
2458
+ // portal windows and iframes.
2459
+ var doc = (_b = (_a = mainRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) !== null && _b !== void 0 ? _b : document;
2460
+ doc.addEventListener("mousedown", setDown);
2461
+ doc.addEventListener("mouseup", setUp);
2462
+ doc.addEventListener("mousemove", move);
2343
2463
  return function () {
2344
- document.removeEventListener("mousemove", move);
2345
- document.removeEventListener("mouseup", setUp);
2464
+ doc.removeEventListener("mousedown", setDown);
2465
+ doc.removeEventListener("mousemove", move);
2466
+ doc.removeEventListener("mouseup", setUp);
2346
2467
  };
2347
- });
2468
+ }, [computeMiniMapScale, instance, isDown, panning]);
2348
2469
  var wrapperStyle = useMemo(function () {
2349
2470
  return {
2350
2471
  position: "relative",
@@ -2394,7 +2515,10 @@ var TransformComponent = function (_a) {
2394
2515
  var wrapperRef = useRef(null);
2395
2516
  var contentRef = useRef(null);
2396
2517
  var gridRef = useRef(null);
2397
- useEffect(function () {
2518
+ // Layout effect: the initial transform (initialPosition*, centerOnInit)
2519
+ // and the first visibility check of virtualized children happen before the
2520
+ // browser paints, so nothing jumps into place a frame late.
2521
+ useIsomorphicLayoutEffect(function () {
2398
2522
  var wrapper = wrapperRef.current;
2399
2523
  var content = contentRef.current;
2400
2524
  if (wrapper !== null && content !== null && init) {
@@ -2465,31 +2589,41 @@ function isElementVisible(opts) {
2465
2589
  var Virtualize = React.forwardRef(function (_a, ref) {
2466
2590
  var x = _a.x, y = _a.y, width = _a.width, height = _a.height, _b = _a.margin, margin = _b === void 0 ? 0 : _b, _c = _a.threshold, threshold = _c === void 0 ? 0 : _c, _d = _a.placeholder, placeholder = _d === void 0 ? null : _d, onShow = _a.onShow, onHide = _a.onHide, children = _a.children, className = _a.className, style = _a.style;
2467
2591
  var instance = useContext(Context);
2468
- var _e = useState(false), visible = _e[0], setVisible = _e[1];
2592
+ var computeVisible = function () {
2593
+ var wrapper = instance.wrapperComponent;
2594
+ if (!wrapper)
2595
+ return false;
2596
+ return isElementVisible({
2597
+ elementX: x,
2598
+ elementY: y,
2599
+ elementWidth: width,
2600
+ elementHeight: height,
2601
+ scale: instance.state.scale,
2602
+ positionX: instance.state.positionX,
2603
+ positionY: instance.state.positionY,
2604
+ viewportWidth: wrapper.offsetWidth,
2605
+ viewportHeight: wrapper.offsetHeight,
2606
+ margin: margin,
2607
+ threshold: threshold,
2608
+ });
2609
+ };
2610
+ // Elements mounted after the wrapper is initialized (items added to a
2611
+ // canvas at runtime) render their children in the very first commit
2612
+ // instead of one frame later.
2613
+ var _e = useState(computeVisible), visible = _e[0], setVisible = _e[1];
2469
2614
  var visibleRef = useRef(false);
2470
2615
  var onShowRef = useRef(onShow);
2471
2616
  var onHideRef = useRef(onHide);
2472
2617
  onShowRef.current = onShow;
2473
2618
  onHideRef.current = onHide;
2474
- useEffect(function () {
2619
+ // Layout effect so the check triggered by the wrapper's init (also a
2620
+ // layout effect) re-renders before the browser paints.
2621
+ useIsomorphicLayoutEffect(function () {
2475
2622
  var check = function () {
2476
2623
  var _a, _b;
2477
- var wrapper = instance.wrapperComponent;
2478
- if (!wrapper)
2624
+ if (!instance.wrapperComponent)
2479
2625
  return;
2480
- var nowVisible = isElementVisible({
2481
- elementX: x,
2482
- elementY: y,
2483
- elementWidth: width,
2484
- elementHeight: height,
2485
- scale: instance.state.scale,
2486
- positionX: instance.state.positionX,
2487
- positionY: instance.state.positionY,
2488
- viewportWidth: wrapper.offsetWidth,
2489
- viewportHeight: wrapper.offsetHeight,
2490
- margin: margin,
2491
- threshold: threshold,
2492
- });
2626
+ var nowVisible = computeVisible();
2493
2627
  if (nowVisible !== visibleRef.current) {
2494
2628
  visibleRef.current = nowVisible;
2495
2629
  setVisible(nowVisible);
@@ -2569,7 +2703,9 @@ var useTransformEffect = function (callback) {
2569
2703
 
2570
2704
  function useTransformComponent(callback) {
2571
2705
  var libraryContext = useTransformContext();
2572
- var _a = useState(callback(getState(libraryContext))), transformRender = _a[0], setTransformRender = _a[1];
2706
+ var _a = useState(function () {
2707
+ return callback(getState(libraryContext));
2708
+ }), transformRender = _a[0], setTransformRender = _a[1];
2573
2709
  useEffect(function () {
2574
2710
  var mounted = true;
2575
2711
  var unmount = libraryContext.onChange(function (ref) {