react-zoom-pan-pinch 2.3.1 → 2.4.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.
@@ -5,6 +5,8 @@ type Props = {
5
5
  contentClass?: string;
6
6
  wrapperStyle?: React.CSSProperties;
7
7
  contentStyle?: React.CSSProperties;
8
+ wrapperProps?: React.HTMLAttributes<HTMLDivElement>;
9
+ contentProps?: React.HTMLAttributes<HTMLDivElement>;
8
10
  };
9
11
  export declare const TransformComponent: React.FC<Props>;
10
12
  export default TransformComponent;
@@ -1,8 +1,6 @@
1
1
  import React from "react";
2
2
  import { ZoomPanPinch } from "../core/instance.core";
3
3
  import { ReactZoomPanPinchProps, ReactZoomPanPinchRef } from "../models";
4
- export declare const Context: React.Context<import("../models").ReactZoomPanPinchComponentHelpers & import("../models").ReactZoomPanPinchState & {
5
- contextInstance: ZoomPanPinch | null;
6
- }>;
4
+ export declare const Context: React.Context<ZoomPanPinch>;
7
5
  export declare const TransformWrapper: React.ForwardRefExoticComponent<Omit<ReactZoomPanPinchProps, "ref"> & React.RefAttributes<ReactZoomPanPinchRef>>;
8
6
  export default TransformWrapper;
@@ -1,6 +1,3 @@
1
- import { LibrarySetup, ReactZoomPanPinchContext, ReactZoomPanPinchComponentHelpers, ReactZoomPanPinchState } from "../models/context.model";
1
+ import { LibrarySetup, ReactZoomPanPinchState } from "../models/context.model";
2
2
  export declare const initialState: ReactZoomPanPinchState;
3
- export declare const contextInitialState: ReactZoomPanPinchComponentHelpers & ReactZoomPanPinchState & {
4
- contextInstance: ReactZoomPanPinchContext | null;
5
- };
6
3
  export declare const initialSetup: LibrarySetup;
@@ -1,3 +1,5 @@
1
1
  import { ReactZoomPanPinchContext } from "../../models";
2
+ export declare const handleDoubleClickStop: (contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent) => void;
3
+ export declare const handleDoubleClickResetMode: (contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent) => void;
2
4
  export declare function handleDoubleClick(contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent): void;
3
5
  export declare const isDoubleClickAllowed: (contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent) => boolean;
@@ -3,7 +3,7 @@ import { animations } from "../animations/animations.constants";
3
3
  import { PositionType } from "../../models/calculations.model";
4
4
  export declare const handleCalculateButtonZoom: (contextInstance: ReactZoomPanPinchContext, delta: number, step: number) => number;
5
5
  export declare function handleZoomToViewCenter(contextInstance: ReactZoomPanPinchContext, delta: number, step: number, animationTime: number, animationType: keyof typeof animations): void;
6
- export declare function resetTransformations(contextInstance: ReactZoomPanPinchContext, animationTime: number, animationType: keyof typeof animations): void;
6
+ export declare function resetTransformations(contextInstance: ReactZoomPanPinchContext, animationTime: number, animationType: keyof typeof animations, onResetTransformation?: () => void): void;
7
7
  export declare function getOffset(element: HTMLElement): PositionType;
8
8
  export declare function calculateZoomToNode(contextInstance: ReactZoomPanPinchContext, node: HTMLElement, customZoom?: number): {
9
9
  positionX: number;
@@ -1,4 +1,4 @@
1
- import { BoundsType, LibrarySetup, PositionType, VelocityType, AnimationType, ReactZoomPanPinchProps, ReactZoomPanPinchState } from "../models";
1
+ import { BoundsType, LibrarySetup, PositionType, VelocityType, AnimationType, ReactZoomPanPinchProps, ReactZoomPanPinchState, ReactZoomPanPinchRef } from "../models";
2
2
  type StartCoordsType = {
3
3
  x: number;
4
4
  y: number;
@@ -9,6 +9,7 @@ export declare class ZoomPanPinch {
9
9
  transformState: ReactZoomPanPinchState;
10
10
  setup: LibrarySetup;
11
11
  observer?: ResizeObserver;
12
+ onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void>;
12
13
  wrapperComponent: HTMLDivElement | null;
13
14
  contentComponent: HTMLDivElement | null;
14
15
  isInitialized: boolean;
@@ -24,6 +25,7 @@ export declare class ZoomPanPinch {
24
25
  pinchStartDistance: null | number;
25
26
  pinchStartScale: null | number;
26
27
  pinchMidpoint: null | PositionType;
28
+ doubleClickStopEventTimer: ReturnType<typeof setTimeout> | null;
27
29
  velocity: VelocityType | null;
28
30
  velocityTime: number | null;
29
31
  lastMousePosition: PositionType | null;
@@ -59,7 +61,8 @@ export declare class ZoomPanPinch {
59
61
  setTransformState: (scale: number, positionX: number, positionY: number) => void;
60
62
  setCenter: () => void;
61
63
  applyTransformation: () => void;
62
- getContext: () => import("../models").ReactZoomPanPinchRef;
64
+ getContext: () => ReactZoomPanPinchRef;
65
+ onChange: (callback: (ref: ReactZoomPanPinchRef) => void) => () => void;
63
66
  /**
64
67
  * Initialization
65
68
  */
package/dist/index.cjs.js CHANGED
@@ -8,42 +8,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
8
8
 
9
9
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
10
10
 
11
- /******************************************************************************
12
- Copyright (c) Microsoft Corporation.
13
-
14
- Permission to use, copy, modify, and/or distribute this software for any
15
- purpose with or without fee is hereby granted.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
18
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
20
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
22
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23
- PERFORMANCE OF THIS SOFTWARE.
24
- ***************************************************************************** */
25
-
26
- var __assign = function() {
27
- __assign = Object.assign || function __assign(t) {
28
- for (var s, i = 1, n = arguments.length; i < n; i++) {
29
- s = arguments[i];
30
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
31
- }
32
- return t;
33
- };
34
- return __assign.apply(this, arguments);
35
- };
36
-
37
- function __spreadArray(to, from, pack) {
38
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
39
- if (ar || !(i in from)) {
40
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
41
- ar[i] = from[i];
42
- }
43
- }
44
- return to.concat(ar || Array.prototype.slice.call(from));
45
- }
46
-
47
11
  /**
48
12
  * Rounds number to given decimal
49
13
  * eg. roundNumber(2.34343, 1) => 2.3
@@ -630,13 +594,48 @@ function handleAlignToScaleBounds(contextInstance, mousePositionX, mousePosition
630
594
  }
631
595
  }
632
596
 
597
+ /******************************************************************************
598
+ Copyright (c) Microsoft Corporation.
599
+
600
+ Permission to use, copy, modify, and/or distribute this software for any
601
+ purpose with or without fee is hereby granted.
602
+
603
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
604
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
605
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
606
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
607
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
608
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
609
+ PERFORMANCE OF THIS SOFTWARE.
610
+ ***************************************************************************** */
611
+
612
+ var __assign = function() {
613
+ __assign = Object.assign || function __assign(t) {
614
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
615
+ s = arguments[i];
616
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
617
+ }
618
+ return t;
619
+ };
620
+ return __assign.apply(this, arguments);
621
+ };
622
+
623
+ function __spreadArray(to, from, pack) {
624
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
625
+ if (ar || !(i in from)) {
626
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
627
+ ar[i] = from[i];
628
+ }
629
+ }
630
+ return to.concat(ar || Array.prototype.slice.call(from));
631
+ }
632
+
633
633
  var initialState = {
634
634
  previousScale: 1,
635
635
  scale: 1,
636
636
  positionX: 0,
637
637
  positionY: 0,
638
638
  };
639
- var contextInitialState = __assign(__assign({}, initialState), { setComponents: function () { return undefined; }, contextInstance: null });
640
639
  var initialSetup = {
641
640
  disabled: false,
642
641
  minPositionX: null,
@@ -761,7 +760,7 @@ function handleZoomToViewCenter(contextInstance, delta, step, animationTime, ani
761
760
  }
762
761
  animate(contextInstance, targetState, animationTime, animationType);
763
762
  }
764
- function resetTransformations(contextInstance, animationTime, animationType) {
763
+ function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
765
764
  var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
766
765
  var limitToBounds = setup.limitToBounds;
767
766
  var initialTransformation = createState(contextInstance.props);
@@ -780,6 +779,7 @@ function resetTransformations(contextInstance, animationTime, animationType) {
780
779
  positionY === initialTransformation.positionY) {
781
780
  return;
782
781
  }
782
+ onResetTransformation === null || onResetTransformation === void 0 ? void 0 : onResetTransformation();
783
783
  animate(contextInstance, newState, animationTime, animationType);
784
784
  }
785
785
  function getOffset(element) {
@@ -1211,25 +1211,52 @@ var handlePinchStop = function (contextInstance) {
1211
1211
  handleAlignToScaleBounds(contextInstance, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.x, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.y);
1212
1212
  };
1213
1213
 
1214
+ var handleDoubleClickStop = function (contextInstance, event) {
1215
+ var onZoomStop = contextInstance.props.onZoomStop;
1216
+ var animationTime = contextInstance.setup.doubleClick.animationTime;
1217
+ cancelTimeout(contextInstance.doubleClickStopEventTimer);
1218
+ contextInstance.doubleClickStopEventTimer = setTimeout(function () {
1219
+ contextInstance.doubleClickStopEventTimer = null;
1220
+ handleCallback(getContext(contextInstance), event, onZoomStop);
1221
+ }, animationTime);
1222
+ };
1223
+ var handleDoubleClickResetMode = function (contextInstance, event) {
1224
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1225
+ var _b = contextInstance.setup.doubleClick, animationTime = _b.animationTime, animationType = _b.animationType;
1226
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1227
+ resetTransformations(contextInstance, animationTime, animationType, function () {
1228
+ return handleCallback(getContext(contextInstance), event, onZoom);
1229
+ });
1230
+ handleDoubleClickStop(contextInstance, event);
1231
+ };
1214
1232
  function handleDoubleClick(contextInstance, event) {
1215
- var _a = contextInstance.setup.doubleClick, disabled = _a.disabled, mode = _a.mode, step = _a.step, animationTime = _a.animationTime, animationType = _a.animationType;
1233
+ var setup = contextInstance.setup, doubleClickStopEventTimer = contextInstance.doubleClickStopEventTimer, transformState = contextInstance.transformState, contentComponent = contextInstance.contentComponent;
1234
+ var scale = transformState.scale;
1235
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1236
+ var _b = setup.doubleClick, disabled = _b.disabled, mode = _b.mode, step = _b.step, animationTime = _b.animationTime, animationType = _b.animationType;
1216
1237
  if (disabled)
1217
1238
  return;
1239
+ if (doubleClickStopEventTimer)
1240
+ return;
1218
1241
  if (mode === "reset") {
1219
- return resetTransformations(contextInstance, animationTime, animationType);
1242
+ return handleDoubleClickResetMode(contextInstance, event);
1220
1243
  }
1221
- var scale = contextInstance.transformState.scale;
1222
- var contentComponent = contextInstance.contentComponent;
1223
1244
  if (!contentComponent)
1224
1245
  return console.error("No ContentComponent found");
1225
1246
  var delta = mode === "zoomOut" ? -1 : 1;
1226
1247
  var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
1248
+ // stop execution when scale didn't change
1249
+ if (scale === newScale)
1250
+ return;
1251
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1227
1252
  var mousePosition = getMousePosition(event, contentComponent, scale);
1228
1253
  var targetState = handleZoomToPoint(contextInstance, newScale, mousePosition.x, mousePosition.y);
1229
1254
  if (!targetState) {
1230
1255
  return console.error("Error during zoom event. New transformation state was not calculated.");
1231
1256
  }
1257
+ handleCallback(getContext(contextInstance), event, onZoom);
1232
1258
  animate(contextInstance, targetState, animationTime, animationType);
1259
+ handleDoubleClickStop(contextInstance, event);
1233
1260
  }
1234
1261
  var isDoubleClickAllowed = function (contextInstance, event) {
1235
1262
  var isInitialized = contextInstance.isInitialized, setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
@@ -1242,8 +1269,6 @@ var isDoubleClickAllowed = function (contextInstance, event) {
1242
1269
  var isExcluded = isExcludedNode(target, excluded);
1243
1270
  if (isExcluded)
1244
1271
  return false;
1245
- if (!isAllowed)
1246
- return false;
1247
1272
  return true;
1248
1273
  };
1249
1274
 
@@ -1251,6 +1276,7 @@ var ZoomPanPinch = /** @class */ (function () {
1251
1276
  function ZoomPanPinch(props) {
1252
1277
  var _this = this;
1253
1278
  this.mounted = true;
1279
+ this.onChangeCallbacks = new Set();
1254
1280
  // Components
1255
1281
  this.wrapperComponent = null;
1256
1282
  this.contentComponent = null;
@@ -1271,6 +1297,8 @@ var ZoomPanPinch = /** @class */ (function () {
1271
1297
  this.pinchStartDistance = null;
1272
1298
  this.pinchStartScale = null;
1273
1299
  this.pinchMidpoint = null;
1300
+ // double click helpers
1301
+ this.doubleClickStopEventTimer = null;
1274
1302
  // velocity helpers
1275
1303
  this.velocity = null;
1276
1304
  this.velocityTime = null;
@@ -1537,7 +1565,9 @@ var ZoomPanPinch = /** @class */ (function () {
1537
1565
  }
1538
1566
  _this.transformState.positionX = positionX;
1539
1567
  _this.transformState.positionY = positionY;
1540
- handleCallback(getContext(_this), { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1568
+ var ctx_1 = getContext(_this);
1569
+ _this.onChangeCallbacks.forEach(function (callback) { return callback(ctx_1); });
1570
+ handleCallback(ctx_1, { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1541
1571
  _this.applyTransformation();
1542
1572
  }
1543
1573
  else {
@@ -1560,6 +1590,14 @@ var ZoomPanPinch = /** @class */ (function () {
1560
1590
  this.getContext = function () {
1561
1591
  return getContext(_this);
1562
1592
  };
1593
+ this.onChange = function (callback) {
1594
+ if (!_this.onChangeCallbacks.has(callback)) {
1595
+ _this.onChangeCallbacks.add(callback);
1596
+ }
1597
+ return function () {
1598
+ _this.onChangeCallbacks.delete(callback);
1599
+ };
1600
+ };
1563
1601
  /**
1564
1602
  * Initialization
1565
1603
  */
@@ -1581,25 +1619,31 @@ var ZoomPanPinch = /** @class */ (function () {
1581
1619
  return ZoomPanPinch;
1582
1620
  }());
1583
1621
 
1584
- var Context = React__default["default"].createContext(contextInitialState);
1622
+ var Context = React__default["default"].createContext(null);
1623
+ var getContent = function (children, ctx) {
1624
+ if (typeof children === "function") {
1625
+ return children(ctx);
1626
+ }
1627
+ return children;
1628
+ };
1585
1629
  var TransformWrapper = React__default["default"].forwardRef(function (props, ref) {
1630
+ var _a = React.useState(0), forceUpdate = _a[1];
1586
1631
  var children = props.children;
1587
1632
  var instance = React.useRef(new ZoomPanPinch(props)).current;
1588
- var content = React.useMemo(function () {
1633
+ var content = getContent(props.children, getContext(instance));
1634
+ var handleOnChange = React.useCallback(function () {
1589
1635
  if (typeof children === "function") {
1590
- return children(getContext(instance));
1636
+ forceUpdate(function (prev) { return prev + 1; });
1591
1637
  }
1592
- return children;
1593
- }, [children, instance]);
1594
- var value = React.useMemo(function () {
1595
- return __assign(__assign({}, instance.transformState), { setComponents: instance.init, contextInstance: instance });
1596
- // eslint-disable-next-line react-hooks/exhaustive-deps
1597
- }, [JSON.stringify(instance.transformState)]);
1638
+ }, [children]);
1598
1639
  React.useImperativeHandle(ref, function () { return getContext(instance); }, [instance]);
1599
1640
  React.useEffect(function () {
1600
1641
  instance.update(props);
1601
1642
  }, [instance, props]);
1602
- return React__default["default"].createElement(Context.Provider, { value: value }, content);
1643
+ React.useEffect(function () {
1644
+ return instance.onChange(handleOnChange);
1645
+ }, [instance, props, handleOnChange]);
1646
+ return React__default["default"].createElement(Context.Provider, { value: instance }, content);
1603
1647
  });
1604
1648
 
1605
1649
  function styleInject(css, ref) {
@@ -1633,29 +1677,35 @@ var css_248z = ".transform-component-module_wrapper__7HFJe {\n position: relati
1633
1677
  var styles = {"wrapper":"transform-component-module_wrapper__7HFJe","content":"transform-component-module_content__uCDPE"};
1634
1678
  styleInject(css_248z);
1635
1679
 
1636
- /* eslint-disable react/require-default-props */
1637
1680
  var TransformComponent = function (_a) {
1638
- var children = _a.children, _b = _a.wrapperClass, wrapperClass = _b === void 0 ? "" : _b, _c = _a.contentClass, contentClass = _c === void 0 ? "" : _c, wrapperStyle = _a.wrapperStyle, contentStyle = _a.contentStyle;
1639
- var setComponents = React.useContext(Context).setComponents;
1681
+ var children = _a.children, _b = _a.wrapperClass, wrapperClass = _b === void 0 ? "" : _b, _c = _a.contentClass, contentClass = _c === void 0 ? "" : _c, wrapperStyle = _a.wrapperStyle, contentStyle = _a.contentStyle, _d = _a.wrapperProps, wrapperProps = _d === void 0 ? {} : _d, _e = _a.contentProps, contentProps = _e === void 0 ? {} : _e;
1682
+ var init = React.useContext(Context).init;
1640
1683
  var wrapperRef = React.useRef(null);
1641
1684
  var contentRef = React.useRef(null);
1642
1685
  React.useEffect(function () {
1643
1686
  var wrapper = wrapperRef.current;
1644
1687
  var content = contentRef.current;
1645
- if (wrapper !== null && content !== null && setComponents) {
1646
- setComponents(wrapper, content);
1688
+ if (wrapper !== null && content !== null && init) {
1689
+ init(wrapper, content);
1647
1690
  }
1691
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1648
1692
  }, []);
1649
- return (React__default["default"].createElement("div", { ref: wrapperRef, className: "react-transform-wrapper ".concat(styles.wrapper, " ").concat(wrapperClass), style: wrapperStyle },
1650
- React__default["default"].createElement("div", { ref: contentRef, className: "react-transform-component ".concat(styles.content, " ").concat(contentClass), style: contentStyle }, children)));
1693
+ return (React__default["default"].createElement("div", __assign({}, wrapperProps, { ref: wrapperRef, className: "react-transform-wrapper ".concat(styles.wrapper, " ").concat(wrapperClass), style: wrapperStyle }),
1694
+ React__default["default"].createElement("div", __assign({}, contentProps, { ref: contentRef, className: "react-transform-component ".concat(styles.content, " ").concat(contentClass), style: contentStyle }), children)));
1651
1695
  };
1652
1696
 
1653
1697
  var useTransformContext = function () {
1654
1698
  var libraryContext = React.useContext(Context);
1655
- if (!libraryContext.contextInstance) {
1699
+ var _a = React.useState(getContext(libraryContext || {})), values = _a[0], setValues = _a[1];
1700
+ if (!libraryContext) {
1656
1701
  throw new Error("Transform context mus be placed inside TransformWrapper");
1657
1702
  }
1658
- return getContext(libraryContext.contextInstance);
1703
+ React.useEffect(function () {
1704
+ libraryContext.onChange(function (ref) {
1705
+ setValues(ref);
1706
+ });
1707
+ }, [libraryContext]);
1708
+ return values;
1659
1709
  };
1660
1710
 
1661
1711
  exports.TransformComponent = TransformComponent;