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