react-zoom-pan-pinch 2.1.2 → 2.2.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +43 -0
  3. package/dist/index.cjs.js +87 -60
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.d.ts +11 -2
  6. package/dist/index.esm.js +87 -60
  7. package/dist/index.esm.js.map +1 -1
  8. package/package.json +13 -12
  9. package/src/.eslintrc.js +36 -0
  10. package/src/components/transform-component.module.css +30 -0
  11. package/src/components/transform-component.tsx +52 -0
  12. package/src/components/transform-context.tsx +493 -0
  13. package/src/components/transform-wrapper.tsx +18 -0
  14. package/src/constants/state.constants.ts +88 -0
  15. package/src/core/animations/animations.constants.ts +76 -0
  16. package/src/core/animations/animations.utils.ts +109 -0
  17. package/src/core/bounds/bounds.types.ts +8 -0
  18. package/src/core/bounds/bounds.utils.ts +157 -0
  19. package/src/core/double-click/double-click.logic.ts +75 -0
  20. package/src/core/handlers/handlers.logic.ts +112 -0
  21. package/src/core/handlers/handlers.utils.ts +202 -0
  22. package/src/core/pan/panning.logic.ts +99 -0
  23. package/src/core/pan/panning.utils.ts +199 -0
  24. package/src/core/pan/velocity.logic.ts +148 -0
  25. package/src/core/pan/velocity.utils.ts +79 -0
  26. package/src/core/pinch/pinch.logic.ts +83 -0
  27. package/src/core/pinch/pinch.utils.ts +88 -0
  28. package/src/core/wheel/wheel.logic.ts +118 -0
  29. package/src/core/wheel/wheel.utils.ts +121 -0
  30. package/src/core/zoom/zoom.logic.ts +69 -0
  31. package/src/core/zoom/zoom.utils.ts +58 -0
  32. package/src/hooks/use-transform-context.tsx +16 -0
  33. package/src/index.ts +6 -0
  34. package/src/models/calculations.model.ts +26 -0
  35. package/src/models/context.model.ts +179 -0
  36. package/src/models/helpers.model.ts +15 -0
  37. package/src/models/index.ts +3 -0
  38. package/src/react-app-env.d.ts +1 -0
  39. package/src/stories/assets/big-image.jpeg +0 -0
  40. package/src/stories/assets/medium-image.jpg +0 -0
  41. package/src/stories/assets/small-image.jpg +0 -0
  42. package/src/stories/assets/svg-example.svg +14 -0
  43. package/src/stories/docs/Introduction.stories.mdx +97 -0
  44. package/src/stories/docs/handlers.stories.mdx +48 -0
  45. package/src/stories/docs/handlers.tsx +107 -0
  46. package/src/stories/docs/props.stories.mdx +92 -0
  47. package/src/stories/docs/props.tsx +490 -0
  48. package/src/stories/examples/content/content.stories.mdx +122 -0
  49. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +42 -0
  50. package/src/stories/examples/content-rerendering/example.tsx +74 -0
  51. package/src/stories/examples/controls/controls.stories.mdx +71 -0
  52. package/src/stories/examples/image-big/image-big.stories.mdx +44 -0
  53. package/src/stories/examples/image-medium/image-medium.stories.mdx +42 -0
  54. package/src/stories/examples/image-small/image-small.stories.mdx +42 -0
  55. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +47 -0
  56. package/src/stories/examples/zoom-to-element/example.tsx +91 -0
  57. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
  58. package/src/stories/types/args.types.ts +201 -0
  59. package/src/stories/utils/args.utils.ts +34 -0
  60. package/src/stories/utils/code.utils.tsx +21 -0
  61. package/src/stories/utils/controls.utils.tsx +26 -0
  62. package/src/stories/utils/index.ts +3 -0
  63. package/src/stories/utils/styles.module.css +29 -0
  64. package/src/styles.css +8 -0
  65. package/src/typings.d.ts +10 -0
  66. package/src/utils/calculations.utils.ts +16 -0
  67. package/src/utils/callback.utils.ts +11 -0
  68. package/src/utils/context.utils.ts +27 -0
  69. package/src/utils/event.utils.ts +22 -0
  70. package/src/utils/helpers.utils.ts +27 -0
  71. package/src/utils/index.ts +7 -0
  72. package/src/utils/state.utils.ts +40 -0
  73. package/src/utils/styles.utils.ts +27 -0
@@ -0,0 +1,21 @@
1
+ import React from "react";
2
+ import Highlight, { defaultProps } from "prism-react-renderer";
3
+ import vsLight from "prism-react-renderer/themes/vsLight";
4
+
5
+ type Props = { code: string };
6
+
7
+ export const Code: React.FC<Props> = ({ code }: Props) => (
8
+ <Highlight {...defaultProps} code={code} theme={vsLight} language="tsx">
9
+ {({ className, style, tokens, getLineProps, getTokenProps }) => (
10
+ <pre className={className} style={style}>
11
+ {tokens.map((line, i) => (
12
+ <div key={i} {...getLineProps({ line, key: i })}>
13
+ {line.map((token, key) => (
14
+ <span key={key} {...getTokenProps({ token, key })} />
15
+ ))}
16
+ </div>
17
+ ))}
18
+ </pre>
19
+ )}
20
+ </Highlight>
21
+ );
@@ -0,0 +1,26 @@
1
+ import React from "react";
2
+ import { ReactZoomPanPinchRef } from "../../models/context.model";
3
+
4
+ import styles from "./styles.module.css";
5
+
6
+ export const Controls: React.FC<ReactZoomPanPinchRef> = ({
7
+ zoomIn,
8
+ zoomOut,
9
+ resetTransform,
10
+ centerView,
11
+ }: ReactZoomPanPinchRef) => (
12
+ <div className={styles.controlPanel}>
13
+ <button className={styles.controlBtn} onClick={() => zoomIn()}>
14
+ Zoom In +
15
+ </button>
16
+ <button className={styles.controlBtn} onClick={() => zoomOut()}>
17
+ Zoom Out -
18
+ </button>
19
+ <button className={styles.controlBtn} onClick={() => resetTransform()}>
20
+ Reset
21
+ </button>
22
+ <button className={styles.controlBtn} onClick={() => centerView()}>
23
+ Center
24
+ </button>
25
+ </div>
26
+ );
@@ -0,0 +1,3 @@
1
+ export * from "./args.utils";
2
+ export * from "./code.utils";
3
+ export * from "./controls.utils";
@@ -0,0 +1,29 @@
1
+ .controlPanel {
2
+ position: absolute;
3
+ z-index: 2;
4
+ transform: translate(10px, 10px);
5
+ max-width: calc(100% - 20px);
6
+ }
7
+ .controlBtn {
8
+ padding: 6px 12px;
9
+ background: white;
10
+ border: 1px solid grey;
11
+ border-radius: 5px;
12
+ margin-right: 10px;
13
+ font-size: 12px;
14
+ text-transform: uppercase;
15
+ font-weight: 600;
16
+ cursor: pointer;
17
+ }
18
+
19
+ .controlBtn:focus {
20
+ filter: brightness(90%);
21
+ }
22
+
23
+ .controlBtn:hover {
24
+ filter: brightness(120%);
25
+ }
26
+
27
+ .controlBtn:active {
28
+ opacity: 0.9;
29
+ }
package/src/styles.css ADDED
@@ -0,0 +1,8 @@
1
+ /* add css styles here (optional) */
2
+
3
+ .test {
4
+ display: inline-block;
5
+ margin: 2em auto;
6
+ border: 2px solid #000;
7
+ font-size: 2em;
8
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Default CSS definition for typescript,
3
+ * will be overridden with file-specific definitions by rollup
4
+ */
5
+ declare module "*.css" {
6
+ const content: { [className: string]: string };
7
+ export default content;
8
+ }
9
+
10
+ type SvgrComponent = React.StatelessComponent<React.SVGAttributes<SVGElement>>
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Rounds number to given decimal
3
+ * eg. roundNumber(2.34343, 1) => 2.3
4
+ */
5
+ export const roundNumber = (num: number, decimal: number) => {
6
+ return Number(num.toFixed(decimal));
7
+ };
8
+
9
+ /**
10
+ * Checks if value is number, if not it returns default value
11
+ * 1# eg. checkIsNumber(2, 30) => 2
12
+ * 2# eg. checkIsNumber(null, 30) => 30
13
+ */
14
+ export const checkIsNumber = (num: any, defaultValue: number) => {
15
+ return typeof num === "number" ? num : defaultValue;
16
+ };
@@ -0,0 +1,11 @@
1
+ import { ReactZoomPanPinchRef } from "../models";
2
+
3
+ export const handleCallback = <T>(
4
+ context: ReactZoomPanPinchRef,
5
+ event: T,
6
+ callback?: (context: ReactZoomPanPinchRef, event: T) => void,
7
+ ): void => {
8
+ if (callback && typeof callback === "function") {
9
+ callback(context, event);
10
+ }
11
+ };
@@ -0,0 +1,27 @@
1
+ import {
2
+ ReactZoomPanPinchContext,
3
+ ReactZoomPanPinchRef,
4
+ } from "../models/context.model";
5
+ import {
6
+ zoomIn,
7
+ zoomOut,
8
+ setTransform,
9
+ resetTransform,
10
+ zoomToElement,
11
+ centerView,
12
+ } from "../core/handlers/handlers.logic";
13
+
14
+ export const getContext = (
15
+ contextInstance: ReactZoomPanPinchContext,
16
+ ): ReactZoomPanPinchRef => {
17
+ return {
18
+ instance: contextInstance,
19
+ state: contextInstance.transformState,
20
+ zoomIn: zoomIn(contextInstance),
21
+ zoomOut: zoomOut(contextInstance),
22
+ setTransform: setTransform(contextInstance),
23
+ resetTransform: resetTransform(contextInstance),
24
+ centerView: centerView(contextInstance),
25
+ zoomToElement: zoomToElement(contextInstance),
26
+ };
27
+ };
@@ -0,0 +1,22 @@
1
+ // We want to make event listeners non-passive, and to do so have to check
2
+ // that browsers support EventListenerOptions in the first place.
3
+ // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
4
+ let passiveSupported = false;
5
+
6
+ export function makePassiveEventOption(): any {
7
+ try {
8
+ const options = {
9
+ get passive() {
10
+ // This function will be called when the browser
11
+ // attempts to access the passive property.
12
+ passiveSupported = true;
13
+ return false;
14
+ },
15
+ };
16
+
17
+ return options;
18
+ } catch (err) {
19
+ passiveSupported = false;
20
+ return passiveSupported;
21
+ }
22
+ }
@@ -0,0 +1,27 @@
1
+ export const isExcludedNode = (
2
+ node: HTMLElement,
3
+ excluded: string[],
4
+ ): boolean => {
5
+ const targetTagName = node.tagName.toUpperCase();
6
+ const isExcludedTag = excluded.find(
7
+ (tag) => tag.toUpperCase() === targetTagName,
8
+ );
9
+
10
+ if (isExcludedTag) return true;
11
+
12
+ const isExcludedClassName = excluded.find((className) =>
13
+ node.classList.contains(className),
14
+ );
15
+
16
+ if (isExcludedClassName) return true;
17
+
18
+ return false;
19
+ };
20
+
21
+ export const cancelTimeout = (
22
+ timeout: ReturnType<typeof setTimeout> | null,
23
+ ): void => {
24
+ if (timeout) {
25
+ clearTimeout(timeout);
26
+ }
27
+ };
@@ -0,0 +1,7 @@
1
+ export * from "./calculations.utils";
2
+ export * from "./callback.utils";
3
+ export * from "./context.utils";
4
+ export * from "./event.utils";
5
+ export * from "./helpers.utils";
6
+ export * from "./state.utils";
7
+ export * from "./styles.utils";
@@ -0,0 +1,40 @@
1
+ import {
2
+ LibrarySetup,
3
+ ReactZoomPanPinchProps,
4
+ ReactZoomPanPinchState,
5
+ } from "../models/context.model";
6
+ import { initialSetup, initialState } from "../constants/state.constants";
7
+
8
+ export const createState = (
9
+ props: ReactZoomPanPinchProps,
10
+ ): ReactZoomPanPinchState => {
11
+ return {
12
+ previousScale: props.initialScale ?? initialState.scale,
13
+ scale: props.initialScale ?? initialState.scale,
14
+ positionX: props.initialPositionX ?? initialState.positionX,
15
+ positionY: props.initialPositionY ?? initialState.positionY,
16
+ };
17
+ };
18
+
19
+ export const createSetup = (props: ReactZoomPanPinchProps): LibrarySetup => {
20
+ const newSetup = { ...initialSetup };
21
+
22
+ Object.keys(props).forEach((key) => {
23
+ const validValue = typeof props[key] !== "undefined";
24
+ const validParameter = typeof initialSetup[key] !== "undefined";
25
+ if (validParameter && validValue) {
26
+ const dataType = Object.prototype.toString.call(initialSetup[key]);
27
+ const isObject = dataType === "[object Object]";
28
+ const isArray = dataType === "[object Array]";
29
+ if (isObject) {
30
+ newSetup[key] = { ...initialSetup[key], ...props[key] };
31
+ } else if (isArray) {
32
+ newSetup[key] = [...initialSetup[key], ...props[key]];
33
+ } else {
34
+ newSetup[key] = props[key];
35
+ }
36
+ }
37
+ });
38
+
39
+ return newSetup;
40
+ };
@@ -0,0 +1,27 @@
1
+ import { StateType } from "models";
2
+
3
+ export const getTransformStyles = (
4
+ x: number,
5
+ y: number,
6
+ scale: number,
7
+ ): string => {
8
+ return `matrix(${scale}, 0,0, ${scale}, ${x}, ${y})`;
9
+ };
10
+
11
+ export const getCenterPosition = (
12
+ scale: number,
13
+ wrapperComponent: HTMLDivElement,
14
+ contentComponent: HTMLDivElement,
15
+ ): StateType => {
16
+ const contentWidth = contentComponent.offsetWidth * scale;
17
+ const contentHeight = contentComponent.offsetHeight * scale;
18
+
19
+ const centerPositionX = (wrapperComponent.offsetWidth - contentWidth) / 2;
20
+ const centerPositionY = (wrapperComponent.offsetHeight - contentHeight) / 2;
21
+
22
+ return {
23
+ scale,
24
+ positionX: centerPositionX,
25
+ positionY: centerPositionY,
26
+ };
27
+ };