react-zoom-pan-pinch 3.0.1 → 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/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # 🖼 React Zoom Pan Pinch
2
2
 
3
+ > Super fast and light react npm package for zooming, panning and pinching html
4
+ > elements in easy way
5
+
3
6
  <p>
4
7
  <a href="https://bettertyped.com/">
5
8
  <img src="https://custom-icon-badges.demolab.com/static/v1?label=&message=BetterTyped&color=333&logo=BT" />
@@ -33,8 +36,10 @@
33
36
  </a>
34
37
  </p>
35
38
 
36
- > Super fast and light react npm package for zooming, panning and pinching html
37
- > elements in easy way
39
+ Do you like this library? Try out other projects
40
+ **[⚡Hyper Fetch](https://github.com/BetterTyped/hyper-fetch)**
41
+
42
+ #### Sources
38
43
 
39
44
  - [Demo](https://prc5.github.io/react-zoom-pan-pinch/?path=/story/examples-big-image--big-image)
40
45
  - [Docs](https://prc5.github.io/react-zoom-pan-pinch/?path=/story/docs-props--page)
package/dist/index.cjs.js CHANGED
@@ -1696,6 +1696,48 @@ var KeepScale = React__default["default"].forwardRef(function (props, ref) {
1696
1696
  return React__default["default"].createElement("div", __assign({}, props, { ref: mergeRefs([localRef, ref]) }));
1697
1697
  });
1698
1698
 
1699
+ var initialElementRect = {
1700
+ width: 0,
1701
+ height: 0,
1702
+ y: 0,
1703
+ x: 0,
1704
+ top: 0,
1705
+ bottom: 0,
1706
+ left: 0,
1707
+ right: 0,
1708
+ };
1709
+ var useResize = function (ref, onResize, dependencies) {
1710
+ var resizeObserverRef = React.useRef();
1711
+ var rectRef = React.useRef(initialElementRect);
1712
+ var didUnmount = React.useRef(false);
1713
+ React.useLayoutEffect(function () {
1714
+ var _a;
1715
+ didUnmount.current = false;
1716
+ if (ref) {
1717
+ resizeObserverRef.current = new ResizeObserver(function (entries) {
1718
+ var newSize = ref.getBoundingClientRect();
1719
+ if (!Array.isArray(entries) ||
1720
+ !entries.length ||
1721
+ didUnmount.current ||
1722
+ (newSize.width === rectRef.current.width &&
1723
+ newSize.height === rectRef.current.height))
1724
+ return;
1725
+ onResize(newSize, ref);
1726
+ rectRef.current = newSize;
1727
+ });
1728
+ (_a = resizeObserverRef.current) === null || _a === void 0 ? void 0 : _a.observe(ref);
1729
+ }
1730
+ return function () {
1731
+ var _a;
1732
+ didUnmount.current = true;
1733
+ if (ref) {
1734
+ (_a = resizeObserverRef.current) === null || _a === void 0 ? void 0 : _a.unobserve(ref);
1735
+ }
1736
+ };
1737
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1738
+ }, __spreadArray([onResize, ref], dependencies, true));
1739
+ };
1740
+
1699
1741
  var previewStyles = {
1700
1742
  position: "absolute",
1701
1743
  zIndex: 2,
@@ -1708,29 +1750,32 @@ var previewStyles = {
1708
1750
  };
1709
1751
  var MiniMap = function (_a) {
1710
1752
  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"]);
1711
- var _d = React.useState({ width: 0, height: 0 }), size = _d[0], setSize = _d[1];
1753
+ var _d = React.useState(false), initialized = _d[0], setInitialized = _d[1];
1712
1754
  var instance = useTransformContext();
1755
+ var miniMapInstance = React.useRef(null);
1756
+ var mainRef = React.useRef(null);
1713
1757
  var wrapperRef = React.useRef(null);
1714
1758
  var previewRef = React.useRef(null);
1715
- var getContentSize = function () {
1759
+ var getContentSize = React.useCallback(function () {
1716
1760
  if (instance.contentComponent) {
1761
+ var rect = instance.contentComponent.getBoundingClientRect();
1717
1762
  return {
1718
- width: instance.contentComponent.offsetWidth,
1719
- height: instance.contentComponent.offsetHeight,
1763
+ width: rect.width / instance.transformState.scale,
1764
+ height: rect.height / instance.transformState.scale,
1720
1765
  };
1721
1766
  }
1722
1767
  return {
1723
1768
  width: 0,
1724
1769
  height: 0,
1725
1770
  };
1726
- };
1727
- var computeMiniMapScale = function () {
1771
+ }, [instance.contentComponent, instance.transformState.scale]);
1772
+ var computeMiniMapScale = React.useCallback(function () {
1728
1773
  var contentSize = getContentSize();
1729
1774
  var scaleX = width / contentSize.width;
1730
1775
  var scaleY = height / contentSize.height;
1731
1776
  var scale = scaleY > scaleX ? scaleX : scaleY;
1732
1777
  return scale;
1733
- };
1778
+ }, [getContentSize, height, width]);
1734
1779
  var computeMiniMapSize = function () {
1735
1780
  var contentSize = getContentSize();
1736
1781
  var scaleX = width / contentSize.width;
@@ -1740,16 +1785,9 @@ var MiniMap = function (_a) {
1740
1785
  }
1741
1786
  return { width: contentSize.width * scaleY, height: height };
1742
1787
  };
1743
- var computeWrapperStyle = function () {
1744
- var _a, _b;
1745
- return {
1746
- width: ((_a = instance.contentComponent) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0,
1747
- height: ((_b = instance.contentComponent) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0,
1748
- };
1749
- };
1750
1788
  var computeMiniMapStyle = function () {
1751
1789
  var scale = computeMiniMapScale();
1752
- return {
1790
+ var style = {
1753
1791
  transform: "scale(".concat(scale || 1, ")"),
1754
1792
  transformOrigin: "0% 0%",
1755
1793
  position: "absolute",
@@ -1757,32 +1795,35 @@ var MiniMap = function (_a) {
1757
1795
  zIndex: 1,
1758
1796
  overflow: "hidden",
1759
1797
  };
1798
+ Object.keys(style).forEach(function (key) {
1799
+ if (wrapperRef.current) {
1800
+ wrapperRef.current.style[key] = style[key];
1801
+ }
1802
+ });
1760
1803
  };
1761
1804
  var transformMiniMap = function () {
1762
- var style = computeWrapperStyle();
1805
+ computeMiniMapStyle();
1806
+ var miniSize = computeMiniMapSize();
1807
+ var wrapSize = getContentSize();
1763
1808
  if (wrapperRef.current) {
1764
- wrapperRef.current.style.width = "".concat(style.width, "px");
1765
- wrapperRef.current.style.width = "".concat(style.width, "px");
1766
- wrapperRef.current.style.height = "".concat(style.height, "px");
1809
+ wrapperRef.current.style.width = "".concat(wrapSize.width, "px");
1810
+ wrapperRef.current.style.height = "".concat(wrapSize.height, "px");
1811
+ }
1812
+ if (mainRef.current) {
1813
+ mainRef.current.style.width = "".concat(miniSize.width, "px");
1814
+ mainRef.current.style.height = "".concat(miniSize.height, "px");
1767
1815
  }
1768
1816
  if (previewRef.current) {
1817
+ var size = getContentSize();
1769
1818
  var scale = computeMiniMapScale();
1770
1819
  var previewScale = scale * (1 / instance.transformState.scale);
1771
1820
  var transform = instance.handleTransformStyles(-instance.transformState.positionX * previewScale, -instance.transformState.positionY * previewScale, 1);
1772
1821
  previewRef.current.style.transform = transform;
1773
- previewRef.current.style.width = "".concat(style.width * previewScale, "px");
1774
- previewRef.current.style.height = "".concat(style.height * previewScale, "px");
1822
+ previewRef.current.style.width = "".concat(size.width * previewScale, "px");
1823
+ previewRef.current.style.height = "".concat(size.height * previewScale, "px");
1775
1824
  }
1776
1825
  };
1777
1826
  var initialize = function () {
1778
- var style = computeMiniMapStyle();
1779
- var initSize = computeMiniMapSize();
1780
- setSize(initSize);
1781
- Object.keys(style).forEach(function (key) {
1782
- if (wrapperRef.current) {
1783
- wrapperRef.current.style[key] = style[key];
1784
- }
1785
- });
1786
1827
  transformMiniMap();
1787
1828
  };
1788
1829
  useTransformEffect(function () {
@@ -1790,11 +1831,30 @@ var MiniMap = function (_a) {
1790
1831
  });
1791
1832
  useTransformInit(function () {
1792
1833
  initialize();
1834
+ setInitialized(true);
1793
1835
  });
1836
+ useResize(instance.contentComponent, initialize, [initialized]);
1837
+ React.useEffect(function () {
1838
+ return instance.onChange(function (zpp) {
1839
+ var scale = computeMiniMapScale();
1840
+ if (miniMapInstance.current) {
1841
+ miniMapInstance.current.instance.transformState.scale =
1842
+ zpp.instance.transformState.scale;
1843
+ miniMapInstance.current.instance.transformState.positionX =
1844
+ zpp.instance.transformState.positionX * scale;
1845
+ miniMapInstance.current.instance.transformState.positionY =
1846
+ zpp.instance.transformState.positionY * scale;
1847
+ }
1848
+ });
1849
+ }, [computeMiniMapScale, instance, miniMapInstance]);
1794
1850
  var wrapperStyle = React.useMemo(function () {
1795
- return __assign(__assign({}, size), { position: "relative", zIndex: 2, overflow: "hidden" });
1796
- }, [size]);
1797
- return (React__default["default"].createElement("div", __assign({}, rest, { style: wrapperStyle, className: "rzpp-mini-map ".concat(rest.className || "") }),
1851
+ return {
1852
+ position: "relative",
1853
+ zIndex: 2,
1854
+ overflow: "hidden",
1855
+ };
1856
+ }, []);
1857
+ return (React__default["default"].createElement("div", __assign({}, rest, { ref: mainRef, style: wrapperStyle, className: "rzpp-mini-map ".concat(rest.className || "") }),
1798
1858
  React__default["default"].createElement("div", __assign({}, rest, { ref: wrapperRef, className: "rzpp-wrapper" }), children),
1799
1859
  React__default["default"].createElement("div", { className: "rzpp-preview", ref: previewRef, style: previewStyles })));
1800
1860
  };
@@ -1866,11 +1926,9 @@ var useTransformInit = function (callback) {
1866
1926
  var unmountCallback;
1867
1927
  var unmount;
1868
1928
  if (libraryContext.contentComponent && libraryContext.wrapperComponent) {
1869
- console.warn(333);
1870
1929
  unmountCallback = callback(getState(libraryContext));
1871
1930
  }
1872
1931
  else {
1873
- console.warn(444);
1874
1932
  unmount = libraryContext.onInit(function (ref) {
1875
1933
  unmountCallback = callback(getState(ref.instance));
1876
1934
  });