react-zoom-pan-pinch 3.0.0 → 3.0.2

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, { useRef, useImperativeHandle, useEffect, useContext, useState, useMemo } from 'react';
1
+ import React, { useRef, useImperativeHandle, useEffect, useContext, useLayoutEffect, useState, useCallback, useMemo } from 'react';
2
2
 
3
3
  /**
4
4
  * Rounds number to given decimal
@@ -1584,10 +1584,10 @@ var ZoomPanPinch = /** @class */ (function () {
1584
1584
  }
1585
1585
  _this.transformState.positionX = positionX;
1586
1586
  _this.transformState.positionY = positionY;
1587
+ _this.applyTransformation();
1587
1588
  var ctx_1 = getContext(_this);
1588
1589
  _this.onChangeCallbacks.forEach(function (callback) { return callback(ctx_1); });
1589
1590
  handleCallback(ctx_1, { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1590
- _this.applyTransformation();
1591
1591
  }
1592
1592
  else {
1593
1593
  console.error("Detected NaN set state values");
@@ -1688,6 +1688,48 @@ var KeepScale = React.forwardRef(function (props, ref) {
1688
1688
  return React.createElement("div", __assign({}, props, { ref: mergeRefs([localRef, ref]) }));
1689
1689
  });
1690
1690
 
1691
+ var initialElementRect = {
1692
+ width: 0,
1693
+ height: 0,
1694
+ y: 0,
1695
+ x: 0,
1696
+ top: 0,
1697
+ bottom: 0,
1698
+ left: 0,
1699
+ right: 0,
1700
+ };
1701
+ var useResize = function (ref, onResize, dependencies) {
1702
+ var resizeObserverRef = useRef();
1703
+ var rectRef = useRef(initialElementRect);
1704
+ var didUnmount = useRef(false);
1705
+ useLayoutEffect(function () {
1706
+ var _a;
1707
+ didUnmount.current = false;
1708
+ if (ref) {
1709
+ resizeObserverRef.current = new ResizeObserver(function (entries) {
1710
+ var newSize = ref.getBoundingClientRect();
1711
+ if (!Array.isArray(entries) ||
1712
+ !entries.length ||
1713
+ didUnmount.current ||
1714
+ (newSize.width === rectRef.current.width &&
1715
+ newSize.height === rectRef.current.height))
1716
+ return;
1717
+ onResize(newSize, ref);
1718
+ rectRef.current = newSize;
1719
+ });
1720
+ (_a = resizeObserverRef.current) === null || _a === void 0 ? void 0 : _a.observe(ref);
1721
+ }
1722
+ return function () {
1723
+ var _a;
1724
+ didUnmount.current = true;
1725
+ if (ref) {
1726
+ (_a = resizeObserverRef.current) === null || _a === void 0 ? void 0 : _a.unobserve(ref);
1727
+ }
1728
+ };
1729
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1730
+ }, __spreadArray([onResize, ref], dependencies, true));
1731
+ };
1732
+
1691
1733
  var previewStyles = {
1692
1734
  position: "absolute",
1693
1735
  zIndex: 2,
@@ -1700,29 +1742,32 @@ var previewStyles = {
1700
1742
  };
1701
1743
  var MiniMap = function (_a) {
1702
1744
  var _b = _a.width, width = _b === void 0 ? 200 : _b, _c = _a.height, height = _c === void 0 ? 200 : _c, children = _a.children, rest = __rest(_a, ["width", "height", "children"]);
1703
- var _d = useState({ width: 0, height: 0 }), size = _d[0], setSize = _d[1];
1745
+ var _d = useState(false), initialized = _d[0], setInitialized = _d[1];
1704
1746
  var instance = useTransformContext();
1747
+ var miniMapInstance = useRef(null);
1748
+ var mainRef = useRef(null);
1705
1749
  var wrapperRef = useRef(null);
1706
1750
  var previewRef = useRef(null);
1707
- var getContentSize = function () {
1751
+ var getContentSize = useCallback(function () {
1708
1752
  if (instance.contentComponent) {
1753
+ var rect = instance.contentComponent.getBoundingClientRect();
1709
1754
  return {
1710
- width: instance.contentComponent.offsetWidth,
1711
- height: instance.contentComponent.offsetHeight,
1755
+ width: rect.width / instance.transformState.scale,
1756
+ height: rect.height / instance.transformState.scale,
1712
1757
  };
1713
1758
  }
1714
1759
  return {
1715
1760
  width: 0,
1716
1761
  height: 0,
1717
1762
  };
1718
- };
1719
- var computeMiniMapScale = function () {
1763
+ }, [instance.contentComponent, instance.transformState.scale]);
1764
+ var computeMiniMapScale = useCallback(function () {
1720
1765
  var contentSize = getContentSize();
1721
1766
  var scaleX = width / contentSize.width;
1722
1767
  var scaleY = height / contentSize.height;
1723
1768
  var scale = scaleY > scaleX ? scaleX : scaleY;
1724
1769
  return scale;
1725
- };
1770
+ }, [getContentSize, height, width]);
1726
1771
  var computeMiniMapSize = function () {
1727
1772
  var contentSize = getContentSize();
1728
1773
  var scaleX = width / contentSize.width;
@@ -1732,16 +1777,9 @@ var MiniMap = function (_a) {
1732
1777
  }
1733
1778
  return { width: contentSize.width * scaleY, height: height };
1734
1779
  };
1735
- var computeWrapperStyle = function () {
1736
- var _a, _b;
1737
- return {
1738
- width: ((_a = instance.contentComponent) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0,
1739
- height: ((_b = instance.contentComponent) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0,
1740
- };
1741
- };
1742
1780
  var computeMiniMapStyle = function () {
1743
1781
  var scale = computeMiniMapScale();
1744
- return {
1782
+ var style = {
1745
1783
  transform: "scale(".concat(scale || 1, ")"),
1746
1784
  transformOrigin: "0% 0%",
1747
1785
  position: "absolute",
@@ -1749,32 +1787,35 @@ var MiniMap = function (_a) {
1749
1787
  zIndex: 1,
1750
1788
  overflow: "hidden",
1751
1789
  };
1790
+ Object.keys(style).forEach(function (key) {
1791
+ if (wrapperRef.current) {
1792
+ wrapperRef.current.style[key] = style[key];
1793
+ }
1794
+ });
1752
1795
  };
1753
1796
  var transformMiniMap = function () {
1754
- var style = computeWrapperStyle();
1797
+ computeMiniMapStyle();
1798
+ var miniSize = computeMiniMapSize();
1799
+ var wrapSize = getContentSize();
1755
1800
  if (wrapperRef.current) {
1756
- wrapperRef.current.style.width = "".concat(style.width, "px");
1757
- wrapperRef.current.style.width = "".concat(style.width, "px");
1758
- wrapperRef.current.style.height = "".concat(style.height, "px");
1801
+ wrapperRef.current.style.width = "".concat(wrapSize.width, "px");
1802
+ wrapperRef.current.style.height = "".concat(wrapSize.height, "px");
1803
+ }
1804
+ if (mainRef.current) {
1805
+ mainRef.current.style.width = "".concat(miniSize.width, "px");
1806
+ mainRef.current.style.height = "".concat(miniSize.height, "px");
1759
1807
  }
1760
1808
  if (previewRef.current) {
1809
+ var size = getContentSize();
1761
1810
  var scale = computeMiniMapScale();
1762
1811
  var previewScale = scale * (1 / instance.transformState.scale);
1763
1812
  var transform = instance.handleTransformStyles(-instance.transformState.positionX * previewScale, -instance.transformState.positionY * previewScale, 1);
1764
1813
  previewRef.current.style.transform = transform;
1765
- previewRef.current.style.width = "".concat(style.width * previewScale, "px");
1766
- previewRef.current.style.height = "".concat(style.height * previewScale, "px");
1814
+ previewRef.current.style.width = "".concat(size.width * previewScale, "px");
1815
+ previewRef.current.style.height = "".concat(size.height * previewScale, "px");
1767
1816
  }
1768
1817
  };
1769
1818
  var initialize = function () {
1770
- var style = computeMiniMapStyle();
1771
- var initSize = computeMiniMapSize();
1772
- setSize(initSize);
1773
- Object.keys(style).forEach(function (key) {
1774
- if (wrapperRef.current) {
1775
- wrapperRef.current.style[key] = style[key];
1776
- }
1777
- });
1778
1819
  transformMiniMap();
1779
1820
  };
1780
1821
  useTransformEffect(function () {
@@ -1782,11 +1823,30 @@ var MiniMap = function (_a) {
1782
1823
  });
1783
1824
  useTransformInit(function () {
1784
1825
  initialize();
1826
+ setInitialized(true);
1785
1827
  });
1828
+ useResize(instance.contentComponent, initialize, [initialized]);
1829
+ useEffect(function () {
1830
+ return instance.onChange(function (zpp) {
1831
+ var scale = computeMiniMapScale();
1832
+ if (miniMapInstance.current) {
1833
+ miniMapInstance.current.instance.transformState.scale =
1834
+ zpp.instance.transformState.scale;
1835
+ miniMapInstance.current.instance.transformState.positionX =
1836
+ zpp.instance.transformState.positionX * scale;
1837
+ miniMapInstance.current.instance.transformState.positionY =
1838
+ zpp.instance.transformState.positionY * scale;
1839
+ }
1840
+ });
1841
+ }, [computeMiniMapScale, instance, miniMapInstance]);
1786
1842
  var wrapperStyle = useMemo(function () {
1787
- return __assign(__assign({}, size), { position: "relative", zIndex: 2, overflow: "hidden" });
1788
- }, [size]);
1789
- return (React.createElement("div", __assign({}, rest, { style: wrapperStyle, className: "rzpp-mini-map ".concat(rest.className || "") }),
1843
+ return {
1844
+ position: "relative",
1845
+ zIndex: 2,
1846
+ overflow: "hidden",
1847
+ };
1848
+ }, []);
1849
+ return (React.createElement("div", __assign({}, rest, { ref: mainRef, style: wrapperStyle, className: "rzpp-mini-map ".concat(rest.className || "") }),
1790
1850
  React.createElement("div", __assign({}, rest, { ref: wrapperRef, className: "rzpp-wrapper" }), children),
1791
1851
  React.createElement("div", { className: "rzpp-preview", ref: previewRef, style: previewStyles })));
1792
1852
  };
@@ -1858,11 +1918,9 @@ var useTransformInit = function (callback) {
1858
1918
  var unmountCallback;
1859
1919
  var unmount;
1860
1920
  if (libraryContext.contentComponent && libraryContext.wrapperComponent) {
1861
- console.warn(333);
1862
1921
  unmountCallback = callback(getState(libraryContext));
1863
1922
  }
1864
1923
  else {
1865
- console.warn(444);
1866
1924
  unmount = libraryContext.onInit(function (ref) {
1867
1925
  unmountCallback = callback(getState(ref.instance));
1868
1926
  });