react-zoom-pan-pinch 2.6.1 → 3.0.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 (130) hide show
  1. package/.eslintignore +6 -0
  2. package/.github/CODEOWNERS +36 -0
  3. package/.github/FUNDING.yml +13 -0
  4. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  5. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. package/.github/workflows/docs.yml +27 -0
  7. package/.github/workflows/release.yml +46 -0
  8. package/.github/workflows/tests.yml +42 -0
  9. package/.prettierrc +10 -0
  10. package/.releaserc.json +63 -0
  11. package/CHANGELOG.md +12 -0
  12. package/Code_of_Conduct.md +61 -0
  13. package/Contributing.md +236 -0
  14. package/README.md +119 -88
  15. package/commitlint.config.js +1 -0
  16. package/dist/index.cjs.js +206 -35
  17. package/dist/index.cjs.js.map +1 -1
  18. package/dist/index.d.ts +100 -77
  19. package/dist/index.esm.js +203 -37
  20. package/dist/index.esm.js.map +1 -1
  21. package/dist/src/components/index.d.ts +4 -0
  22. package/dist/src/components/mini-map/mini-map.d.ts +7 -0
  23. package/dist/{components → src/components/transform-component}/transform-component.d.ts +1 -1
  24. package/dist/{components → src/components/transform-wrapper}/transform-wrapper.d.ts +3 -4
  25. package/dist/{core → src/core}/instance.core.d.ts +5 -0
  26. package/dist/src/hooks/index.d.ts +4 -0
  27. package/dist/src/hooks/use-controls.d.ts +2 -0
  28. package/dist/src/hooks/use-transform-context.d.ts +1 -0
  29. package/dist/src/hooks/use-transform-effect.d.ts +2 -0
  30. package/dist/src/hooks/use-transform-init.d.ts +2 -0
  31. package/dist/src/index.d.ts +5 -0
  32. package/dist/{models → src/models}/context.model.d.ts +6 -2
  33. package/dist/src/utils/context.utils.d.ts +4 -0
  34. package/package.json +35 -15
  35. package/rollup.config.js +51 -0
  36. package/src/components/index.ts +4 -0
  37. package/src/components/{keep-scale.tsx → keep-scale/keep-scale.tsx} +2 -2
  38. package/src/components/mini-map/mini-map.tsx +155 -0
  39. package/src/components/{transform-component.tsx → transform-component/transform-component.tsx} +1 -3
  40. package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
  41. package/src/core/instance.core.ts +18 -1
  42. package/src/hooks/index.ts +4 -0
  43. package/src/hooks/use-controls.tsx +9 -0
  44. package/src/hooks/use-transform-context.tsx +4 -15
  45. package/src/hooks/use-transform-effect.tsx +22 -0
  46. package/src/hooks/use-transform-init.tsx +29 -0
  47. package/src/index.ts +2 -4
  48. package/src/models/context.model.ts +11 -2
  49. package/src/utils/context.utils.ts +24 -3
  50. package/tsconfig.json +32 -0
  51. package/dist/hooks/use-transform-context.d.ts +0 -3
  52. package/dist/stories/docs/handlers.d.ts +0 -12
  53. package/dist/stories/docs/props.d.ts +0 -14
  54. package/dist/stories/examples/content-rerendering/example.d.ts +0 -2
  55. package/dist/stories/examples/keep-scale/example.d.ts +0 -2
  56. package/dist/stories/examples/stress/example.d.ts +0 -2
  57. package/dist/stories/examples/svg-zoom-to-element/example.d.ts +0 -2
  58. package/dist/stories/examples/zoom-to-element/example.d.ts +0 -2
  59. package/dist/stories/types/args.types.d.ts +0 -268
  60. package/dist/stories/utils/args.utils.d.ts +0 -3
  61. package/dist/stories/utils/code.utils.d.ts +0 -6
  62. package/dist/stories/utils/controls.utils.d.ts +0 -3
  63. package/dist/stories/utils/index.d.ts +0 -3
  64. package/dist/utils/context.utils.d.ts +0 -2
  65. package/src/components/transform-wrapper.tsx +0 -56
  66. package/src/stories/assets/big-image.jpeg +0 -0
  67. package/src/stories/assets/map.jpg +0 -0
  68. package/src/stories/assets/medium-image.jpg +0 -0
  69. package/src/stories/assets/pin.svg +0 -1
  70. package/src/stories/assets/small-image.jpg +0 -0
  71. package/src/stories/assets/svg-example.svg +0 -14
  72. package/src/stories/docs/Introduction.stories.mdx +0 -97
  73. package/src/stories/docs/handlers.stories.mdx +0 -48
  74. package/src/stories/docs/handlers.tsx +0 -107
  75. package/src/stories/docs/props.stories.mdx +0 -92
  76. package/src/stories/docs/props.tsx +0 -496
  77. package/src/stories/examples/content/content.stories.mdx +0 -122
  78. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +0 -45
  79. package/src/stories/examples/content-rerendering/example.tsx +0 -74
  80. package/src/stories/examples/controls/controls.stories.mdx +0 -72
  81. package/src/stories/examples/image-big/image-big.stories.mdx +0 -44
  82. package/src/stories/examples/image-medium/image-medium.stories.mdx +0 -42
  83. package/src/stories/examples/image-small/image-small.stories.mdx +0 -42
  84. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +0 -47
  85. package/src/stories/examples/keep-scale/example.tsx +0 -94
  86. package/src/stories/examples/keep-scale/keep-scale.stories.mdx +0 -45
  87. package/src/stories/examples/stress/example.tsx +0 -24
  88. package/src/stories/examples/stress/stress-test.stories.mdx +0 -28
  89. package/src/stories/examples/svg-zoom-to-element/creativity.svg +0 -143
  90. package/src/stories/examples/svg-zoom-to-element/example.tsx +0 -57
  91. package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +0 -28
  92. package/src/stories/examples/zoom-to-element/example.tsx +0 -95
  93. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +0 -28
  94. package/src/stories/types/args.types.ts +0 -201
  95. package/src/stories/utils/args.utils.ts +0 -34
  96. package/src/stories/utils/code.utils.tsx +0 -22
  97. package/src/stories/utils/controls.utils.tsx +0 -43
  98. package/src/stories/utils/index.ts +0 -3
  99. package/src/stories/utils/styles.module.css +0 -34
  100. /package/dist/{components → src/components/keep-scale}/keep-scale.d.ts +0 -0
  101. /package/dist/{constants → src/constants}/state.constants.d.ts +0 -0
  102. /package/dist/{core → src/core}/animations/animations.constants.d.ts +0 -0
  103. /package/dist/{core → src/core}/animations/animations.utils.d.ts +0 -0
  104. /package/dist/{core → src/core}/bounds/bounds.types.d.ts +0 -0
  105. /package/dist/{core → src/core}/bounds/bounds.utils.d.ts +0 -0
  106. /package/dist/{core → src/core}/double-click/double-click.logic.d.ts +0 -0
  107. /package/dist/{core → src/core}/handlers/handlers.logic.d.ts +0 -0
  108. /package/dist/{core → src/core}/handlers/handlers.utils.d.ts +0 -0
  109. /package/dist/{core → src/core}/pan/panning.logic.d.ts +0 -0
  110. /package/dist/{core → src/core}/pan/panning.utils.d.ts +0 -0
  111. /package/dist/{core → src/core}/pan/velocity.logic.d.ts +0 -0
  112. /package/dist/{core → src/core}/pan/velocity.utils.d.ts +0 -0
  113. /package/dist/{core → src/core}/pinch/pinch.logic.d.ts +0 -0
  114. /package/dist/{core → src/core}/pinch/pinch.utils.d.ts +0 -0
  115. /package/dist/{core → src/core}/wheel/wheel.logic.d.ts +0 -0
  116. /package/dist/{core → src/core}/wheel/wheel.utils.d.ts +0 -0
  117. /package/dist/{core → src/core}/zoom/zoom.logic.d.ts +0 -0
  118. /package/dist/{core → src/core}/zoom/zoom.utils.d.ts +0 -0
  119. /package/dist/{models → src/models}/calculations.model.d.ts +0 -0
  120. /package/dist/{models → src/models}/helpers.model.d.ts +0 -0
  121. /package/dist/{models → src/models}/index.d.ts +0 -0
  122. /package/dist/{utils → src/utils}/calculations.utils.d.ts +0 -0
  123. /package/dist/{utils → src/utils}/callback.utils.d.ts +0 -0
  124. /package/dist/{utils → src/utils}/event.utils.d.ts +0 -0
  125. /package/dist/{utils → src/utils}/helpers.utils.d.ts +0 -0
  126. /package/dist/{utils → src/utils}/index.d.ts +0 -0
  127. /package/dist/{utils → src/utils}/ref.utils.d.ts +0 -0
  128. /package/dist/{utils → src/utils}/state.utils.d.ts +0 -0
  129. /package/dist/{utils → src/utils}/styles.utils.d.ts +0 -0
  130. /package/src/components/{transform-component.module.css → transform-component/transform-component.module.css} +0 -0
package/package.json CHANGED
@@ -1,30 +1,25 @@
1
1
  {
2
2
  "name": "react-zoom-pan-pinch",
3
- "version": "2.6.1",
3
+ "version": "3.0.0",
4
4
  "description": "Zoom and pan html elements in easy way",
5
5
  "author": "prc5",
6
6
  "license": "MIT",
7
- "repository": "prc5/react-zoom-pan-pinch",
8
7
  "main": "dist/index.cjs.js",
9
8
  "module": "dist/index.esm.js",
10
9
  "types": "dist/index.d.ts",
11
10
  "source": "src/index.ts",
12
- "jsnext:main": "dist/index.esm.js",
13
- "engines": {
14
- "node": ">=8",
15
- "npm": ">=5"
16
- },
17
11
  "scripts": {
18
- "test": "react-scripts test --watchAll=false --passWithNoTests",
19
12
  "build": "rollup -c",
20
13
  "build:docs": "build-storybook",
21
14
  "start": "start-storybook -p 6006 --no-manager-cache",
22
- "lint": "eslint 'src/**/*.{js,jsx,ts,tsx}' --fix",
15
+ "test": "jest",
16
+ "lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
17
+ "format": "prettier --write .",
18
+ "typecheck": "tsc --noEmit --emitDeclarationOnly false",
23
19
  "prepare": "install-peers && yarn build",
24
20
  "predeploy": "yarn run build:docs",
25
21
  "deploy": "gh-pages -d storybook-static",
26
- "test:lint": "eslint src/ --ext .js,.jsx,.tsx,.ts",
27
- "test:prettier": "npm run prettier -check \"**/*.+(js|json|ts|tsx)\""
22
+ "release": "yarn semantic-release"
28
23
  },
29
24
  "peerDependencies": {
30
25
  "react": "*",
@@ -33,8 +28,16 @@
33
28
  "devDependencies": {
34
29
  "@babel/core": "^7.20.12",
35
30
  "@babel/runtime": "^7.20.13",
31
+ "@commitlint/cli": "^17.0.0",
32
+ "@commitlint/config-conventional": "^17.0.2",
36
33
  "@rollup/plugin-babel": "^5.3.0",
37
34
  "@rollup/plugin-typescript": "^8.2.1",
35
+ "@semantic-release/changelog": "^6.0.1",
36
+ "@semantic-release/commit-analyzer": "^9.0.2",
37
+ "@semantic-release/git": "^10.0.1",
38
+ "@semantic-release/github": "^8.0.6",
39
+ "@semantic-release/npm": "^9.0.1",
40
+ "@semantic-release/release-notes-generator": "^10.0.3",
38
41
  "@storybook/addon-a11y": "^6.5.16",
39
42
  "@storybook/addon-actions": "^6.5.16",
40
43
  "@storybook/addon-docs": "^6.5.16",
@@ -55,6 +58,7 @@
55
58
  "@typescript-eslint/parser": "^5.28.0",
56
59
  "babel-eslint": "9.0.0",
57
60
  "babel-loader": "^8.2.2",
61
+ "commitlint": "^17.0.2",
58
62
  "cross-env": "^7.0.3",
59
63
  "eslint": "^8.17.0",
60
64
  "eslint-config-airbnb": "^18.2.1",
@@ -69,24 +73,40 @@
69
73
  "eslint-plugin-react-hooks": "^4.6.0",
70
74
  "gh-pages": "^3.1.0",
71
75
  "install-peers-cli": "^2.2.0",
76
+ "jest-css-modules": "^2.1.0",
77
+ "jest-extended": "^3.2.0",
72
78
  "postcss": "^8.2.13",
73
79
  "prism-react-renderer": "^1.2.1",
80
+ "react": "*",
81
+ "react-dom": "*",
74
82
  "react-scripts": "5.0.1",
75
83
  "rollup": "^2.46.0",
76
84
  "rollup-plugin-delete": "^2.0.0",
77
85
  "rollup-plugin-dts": "^3.0.2",
78
86
  "rollup-plugin-peer-deps-external": "^2.2.4",
79
87
  "rollup-plugin-postcss": "^4.0.0",
88
+ "semantic-release": "^19.0.5",
80
89
  "source-map-loader": "^2.0.1",
90
+ "ts-jest": "^29.0.3",
81
91
  "ts-loader": "^9.4.2",
92
+ "ts-node": "~10.9.1",
82
93
  "typescript": "^4.2.3",
83
94
  "webpack": "5",
84
95
  "webpack-cli": "^5.0.1"
85
96
  },
86
- "files": [
87
- "dist",
88
- "src"
89
- ],
97
+ "engines": {
98
+ "node": ">=8",
99
+ "npm": ">=5"
100
+ },
101
+ "config": {
102
+ "commitizen": {
103
+ "path": "git-cz"
104
+ }
105
+ },
106
+ "repository": {
107
+ "type": "git",
108
+ "url": "https://github.com/prc5/react-zoom-pan-pinch.git"
109
+ },
90
110
  "eslintConfig": {
91
111
  "extends": [
92
112
  "react-app",
@@ -0,0 +1,51 @@
1
+ import babel from "@rollup/plugin-babel";
2
+ import external from "rollup-plugin-peer-deps-external";
3
+ import del from "rollup-plugin-delete";
4
+ import typescript from "@rollup/plugin-typescript";
5
+ import postcss from "rollup-plugin-postcss";
6
+ import dts from "rollup-plugin-dts";
7
+ import pkg from "./package.json";
8
+
9
+ export default [
10
+ {
11
+ input: pkg.source,
12
+ output: [
13
+ {
14
+ file: pkg.main,
15
+ format: "cjs",
16
+ exports: "named",
17
+ sourcemap: true,
18
+ },
19
+ {
20
+ file: pkg.module,
21
+ format: "es",
22
+ exports: "named",
23
+ sourcemap: true,
24
+ },
25
+ ],
26
+ plugins: [
27
+ external(),
28
+ babel({
29
+ exclude: "node_modules/**",
30
+ }),
31
+ del({ targets: ["dist/*"] }),
32
+ typescript(),
33
+ postcss({
34
+ modules: true,
35
+ }),
36
+ ],
37
+ external: Object.keys(pkg.peerDependencies || {}),
38
+ },
39
+ {
40
+ input: pkg.source,
41
+ output: [{ file: pkg.types, format: "es" }],
42
+ plugins: [
43
+ external(),
44
+ dts({
45
+ compilerOptions: {
46
+ baseUrl: "./src",
47
+ },
48
+ }),
49
+ ],
50
+ },
51
+ ];
@@ -0,0 +1,4 @@
1
+ export * from "./keep-scale/keep-scale";
2
+ export * from "./mini-map/mini-map";
3
+ export * from "./transform-component/transform-component";
4
+ export * from "./transform-wrapper/transform-wrapper";
@@ -1,7 +1,7 @@
1
1
  import React, { useContext, useEffect, useRef } from "react";
2
2
 
3
3
  import { mergeRefs } from "utils/ref.utils";
4
- import { Context } from "./transform-wrapper";
4
+ import { Context } from "../transform-wrapper/transform-wrapper";
5
5
 
6
6
  export const KeepScale = React.forwardRef<
7
7
  HTMLDivElement,
@@ -18,7 +18,7 @@ export const KeepScale = React.forwardRef<
18
18
  localRef.current.style.transform = instance.handleTransformStyles(
19
19
  positionX,
20
20
  positionY,
21
- 1 / ctx.state.scale,
21
+ 1 / ctx.instance.transformState.scale,
22
22
  );
23
23
  }
24
24
  });
@@ -0,0 +1,155 @@
1
+ /* eslint-disable react/require-default-props */
2
+ import React, { useMemo, useRef, useState } from "react";
3
+
4
+ import {
5
+ useTransformContext,
6
+ useTransformEffect,
7
+ useTransformInit,
8
+ } from "hooks";
9
+
10
+ export type MiniMapProps = {
11
+ width?: number;
12
+ height?: number;
13
+ children: React.ReactNode;
14
+ } & React.DetailedHTMLProps<
15
+ React.HTMLAttributes<HTMLDivElement>,
16
+ HTMLDivElement
17
+ >;
18
+
19
+ const previewStyles = {
20
+ position: "absolute",
21
+ zIndex: 2,
22
+ top: "0px",
23
+ left: "0px",
24
+ boxSizing: "border-box",
25
+ border: "3px solid red",
26
+ transformOrigin: "0% 0%",
27
+ boxShadow: "rgba(0,0,0,0.2) 0 0 0 10000000px",
28
+ } as const;
29
+
30
+ export const MiniMap: React.FC<MiniMapProps> = ({
31
+ width = 200,
32
+ height = 200,
33
+ children,
34
+ ...rest
35
+ }) => {
36
+ const [size, setSize] = useState({ width: 0, height: 0 });
37
+ const instance = useTransformContext();
38
+
39
+ const wrapperRef = useRef<HTMLDivElement | null>(null);
40
+ const previewRef = useRef<HTMLDivElement | null>(null);
41
+
42
+ const getContentSize = () => {
43
+ if (instance.contentComponent) {
44
+ return {
45
+ width: instance.contentComponent.offsetWidth,
46
+ height: instance.contentComponent.offsetHeight,
47
+ };
48
+ }
49
+ return {
50
+ width: 0,
51
+ height: 0,
52
+ };
53
+ };
54
+
55
+ const computeMiniMapScale = () => {
56
+ const contentSize = getContentSize();
57
+ const scaleX = width / contentSize.width;
58
+ const scaleY = height / contentSize.height;
59
+ const scale = scaleY > scaleX ? scaleX : scaleY;
60
+
61
+ return scale;
62
+ };
63
+
64
+ const computeMiniMapSize = () => {
65
+ const contentSize = getContentSize();
66
+ const scaleX = width / contentSize.width;
67
+ const scaleY = height / contentSize.height;
68
+ if (scaleY > scaleX) {
69
+ return { width, height: contentSize.height * scaleX };
70
+ }
71
+ return { width: contentSize.width * scaleY, height };
72
+ };
73
+
74
+ const computeWrapperStyle = () => {
75
+ return {
76
+ width: instance.contentComponent?.offsetWidth || 0,
77
+ height: instance.contentComponent?.offsetHeight || 0,
78
+ };
79
+ };
80
+
81
+ const computeMiniMapStyle = () => {
82
+ const scale = computeMiniMapScale();
83
+ return {
84
+ transform: `scale(${scale || 1})`,
85
+ transformOrigin: "0% 0%",
86
+ position: "absolute",
87
+ boxSizing: "border-box",
88
+ zIndex: 1,
89
+ overflow: "hidden",
90
+ } as const;
91
+ };
92
+
93
+ const transformMiniMap = () => {
94
+ const style = computeWrapperStyle();
95
+ if (wrapperRef.current) {
96
+ wrapperRef.current.style.width = `${style.width}px`;
97
+ wrapperRef.current.style.width = `${style.width}px`;
98
+ wrapperRef.current.style.height = `${style.height}px`;
99
+ }
100
+ if (previewRef.current) {
101
+ const scale = computeMiniMapScale();
102
+ const previewScale = scale * (1 / instance.transformState.scale);
103
+ const transform = instance.handleTransformStyles(
104
+ -instance.transformState.positionX * previewScale,
105
+ -instance.transformState.positionY * previewScale,
106
+ 1,
107
+ );
108
+ previewRef.current.style.transform = transform;
109
+ previewRef.current.style.width = `${style.width * previewScale}px`;
110
+ previewRef.current.style.height = `${style.height * previewScale}px`;
111
+ }
112
+ };
113
+
114
+ const initialize = () => {
115
+ const style = computeMiniMapStyle();
116
+ const initSize = computeMiniMapSize();
117
+ setSize(initSize);
118
+ Object.keys(style).forEach((key) => {
119
+ if (wrapperRef.current) {
120
+ wrapperRef.current.style[key] = style[key];
121
+ }
122
+ });
123
+ transformMiniMap();
124
+ };
125
+
126
+ useTransformEffect(() => {
127
+ transformMiniMap();
128
+ });
129
+
130
+ useTransformInit(() => {
131
+ initialize();
132
+ });
133
+
134
+ const wrapperStyle = useMemo(() => {
135
+ return {
136
+ ...size,
137
+ position: "relative",
138
+ zIndex: 2,
139
+ overflow: "hidden",
140
+ } as const;
141
+ }, [size]);
142
+
143
+ return (
144
+ <div
145
+ {...rest}
146
+ style={wrapperStyle}
147
+ className={`rzpp-mini-map ${rest.className || ""}`}
148
+ >
149
+ <div {...rest} ref={wrapperRef} className="rzpp-wrapper">
150
+ {children}
151
+ </div>
152
+ <div className="rzpp-preview" ref={previewRef} style={previewStyles} />
153
+ </div>
154
+ );
155
+ };
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable react/require-default-props */
2
2
  import React, { useContext, useEffect, useRef } from "react";
3
3
 
4
- import { Context } from "./transform-wrapper";
4
+ import { Context } from "../transform-wrapper/transform-wrapper";
5
5
 
6
6
  import styles from "./transform-component.module.css";
7
7
 
@@ -56,5 +56,3 @@ export const TransformComponent: React.FC<Props> = ({
56
56
  </div>
57
57
  );
58
58
  };
59
-
60
- export default TransformComponent;
@@ -0,0 +1,39 @@
1
+ import React, { useEffect, useImperativeHandle, useRef } from "react";
2
+
3
+ import { ZoomPanPinch } from "../../core/instance.core";
4
+ import {
5
+ ReactZoomPanPinchContentRef,
6
+ ReactZoomPanPinchProps,
7
+ } from "../../models";
8
+ import { getControls } from "../../utils";
9
+
10
+ export const Context = React.createContext<ZoomPanPinch>(null as any);
11
+
12
+ const getContent = (
13
+ children: ReactZoomPanPinchProps["children"],
14
+ ctx: ReactZoomPanPinchContentRef,
15
+ ) => {
16
+ if (typeof children === "function") {
17
+ return children(ctx);
18
+ }
19
+ return children;
20
+ };
21
+
22
+ export const TransformWrapper = React.forwardRef(
23
+ (
24
+ props: Omit<ReactZoomPanPinchProps, "ref">,
25
+ ref: React.Ref<ReactZoomPanPinchContentRef>,
26
+ ) => {
27
+ const instance = useRef(new ZoomPanPinch(props)).current;
28
+
29
+ const content = getContent(props.children, getControls(instance));
30
+
31
+ useImperativeHandle(ref, () => getControls(instance), [instance]);
32
+
33
+ useEffect(() => {
34
+ instance.update(props);
35
+ }, [instance, props]);
36
+
37
+ return <Context.Provider value={instance}>{content}</Context.Provider>;
38
+ },
39
+ );
@@ -54,6 +54,7 @@ export class ZoomPanPinch {
54
54
  public observer?: ResizeObserver;
55
55
  public onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> =
56
56
  new Set();
57
+ public onInitCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> = new Set();
57
58
 
58
59
  // Components
59
60
  public wrapperComponent: HTMLDivElement | null = null;
@@ -158,10 +159,12 @@ export class ZoomPanPinch {
158
159
  handleInitialize = (contentComponent: HTMLDivElement): void => {
159
160
  const { centerOnInit } = this.setup;
160
161
  this.applyTransformation();
162
+ this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
161
163
 
162
164
  if (centerOnInit) {
163
165
  this.setCenter();
164
166
  this.observer = new ResizeObserver(() => {
167
+ this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
165
168
  this.setCenter();
166
169
  this.observer?.disconnect();
167
170
  });
@@ -453,6 +456,10 @@ export class ZoomPanPinch {
453
456
  return getContext(this);
454
457
  };
455
458
 
459
+ /**
460
+ * Hooks
461
+ */
462
+
456
463
  onChange = (callback: (ref: ReactZoomPanPinchRef) => void) => {
457
464
  if (!this.onChangeCallbacks.has(callback)) {
458
465
  this.onChangeCallbacks.add(callback);
@@ -462,6 +469,15 @@ export class ZoomPanPinch {
462
469
  };
463
470
  };
464
471
 
472
+ onInit = (callback: (ref: ReactZoomPanPinchRef) => void) => {
473
+ if (!this.onInitCallbacks.has(callback)) {
474
+ this.onInitCallbacks.add(callback);
475
+ }
476
+ return () => {
477
+ this.onInitCallbacks.delete(callback);
478
+ };
479
+ };
480
+
465
481
  /**
466
482
  * Initialization
467
483
  */
@@ -478,6 +494,7 @@ export class ZoomPanPinch {
478
494
  this.handleInitialize(contentComponent);
479
495
  this.initializeWindowEvents();
480
496
  this.isInitialized = true;
481
- handleCallback(getContext(this), undefined, this.props.onInit);
497
+ const ctx = getContext(this);
498
+ handleCallback(ctx, undefined, this.props.onInit);
482
499
  };
483
500
  }
@@ -0,0 +1,4 @@
1
+ export * from "./use-controls";
2
+ export * from "./use-transform-init";
3
+ export * from "./use-transform-effect";
4
+ export * from "./use-transform-context";
@@ -0,0 +1,9 @@
1
+ import { ReactZoomPanPinchContentRef } from "models";
2
+ import { getControls } from "utils";
3
+ import { useTransformContext } from "./use-transform-context";
4
+
5
+ export const useControls = (): ReactZoomPanPinchContentRef => {
6
+ const libraryContext = useTransformContext();
7
+
8
+ return getControls(libraryContext);
9
+ };
@@ -1,24 +1,13 @@
1
- import { useContext, useEffect, useState } from "react";
1
+ import { useContext } from "react";
2
2
 
3
- import { getContext } from "../utils";
4
- import { Context } from "../components/transform-wrapper";
5
- import { ReactZoomPanPinchRef } from "../models";
3
+ import { Context } from "components";
6
4
 
7
- const useTransformContext = (): ReactZoomPanPinchRef => {
5
+ export const useTransformContext = () => {
8
6
  const libraryContext = useContext(Context);
9
- const [values, setValues] = useState(getContext(libraryContext || {}));
10
7
 
11
8
  if (!libraryContext) {
12
9
  throw new Error("Transform context mus be placed inside TransformWrapper");
13
10
  }
14
11
 
15
- useEffect(() => {
16
- libraryContext.onChange((ref) => {
17
- setValues(ref);
18
- });
19
- }, [libraryContext]);
20
-
21
- return values;
12
+ return libraryContext;
22
13
  };
23
-
24
- export default useTransformContext;
@@ -0,0 +1,22 @@
1
+ import { useEffect } from "react";
2
+
3
+ import { getState } from "utils";
4
+ import { ReactZoomPanPinchContextState } from "../models";
5
+ import { useTransformContext } from "./use-transform-context";
6
+
7
+ export const useTransformEffect = (
8
+ callback: (ref: ReactZoomPanPinchContextState) => void | (() => void),
9
+ ): void => {
10
+ const libraryContext = useTransformContext();
11
+
12
+ useEffect(() => {
13
+ let unmountCallback: void | (() => void);
14
+ const unmount = libraryContext.onChange((ref) => {
15
+ unmountCallback = callback(getState(ref.instance));
16
+ });
17
+ return () => {
18
+ unmount();
19
+ unmountCallback?.();
20
+ };
21
+ }, [callback, libraryContext]);
22
+ };
@@ -0,0 +1,29 @@
1
+ import { useEffect } from "react";
2
+
3
+ import { getState } from "utils";
4
+ import { ReactZoomPanPinchContextState } from "../models";
5
+ import { useTransformContext } from "./use-transform-context";
6
+
7
+ export const useTransformInit = (
8
+ callback: (ref: ReactZoomPanPinchContextState) => void | (() => void),
9
+ ): void => {
10
+ const libraryContext = useTransformContext();
11
+
12
+ useEffect(() => {
13
+ let unmountCallback: void | (() => void);
14
+ let unmount: void | (() => void);
15
+ if (libraryContext.contentComponent && libraryContext.wrapperComponent) {
16
+ console.warn(333);
17
+ unmountCallback = callback(getState(libraryContext));
18
+ } else {
19
+ console.warn(444);
20
+ unmount = libraryContext.onInit((ref) => {
21
+ unmountCallback = callback(getState(ref.instance));
22
+ });
23
+ }
24
+ return () => {
25
+ unmount?.();
26
+ unmountCallback?.();
27
+ };
28
+ }, []);
29
+ };
package/src/index.ts CHANGED
@@ -1,7 +1,5 @@
1
- export { default as TransformWrapper } from "./components/transform-wrapper";
2
- export { default as TransformComponent } from "./components/transform-component";
3
- export { default as useTransformContext } from "./hooks/use-transform-context";
4
- export * from "./components/keep-scale";
1
+ export * from "./hooks";
2
+ export * from "./components";
5
3
 
6
4
  export * from "./utils/styles.utils";
7
5
  export * from "./models/context.model";
@@ -14,11 +14,18 @@ import { ZoomPanPinch } from "../core/instance.core";
14
14
 
15
15
  export type ReactZoomPanPinchContext = typeof ZoomPanPinch.prototype;
16
16
 
17
- export type ReactZoomPanPinchRef = {
17
+ export type ReactZoomPanPinchContextState = {
18
18
  instance: ReactZoomPanPinchContext;
19
19
  state: ReactZoomPanPinchState;
20
+ };
21
+
22
+ export type ReactZoomPanPinchContentRef = {
23
+ instance: ReactZoomPanPinchContext;
20
24
  } & ReactZoomPanPinchHandlers;
21
25
 
26
+ export type ReactZoomPanPinchRef = ReactZoomPanPinchContextState &
27
+ ReactZoomPanPinchHandlers;
28
+
22
29
  export type ReactZoomPanPinchState = {
23
30
  previousScale: number;
24
31
  scale: number;
@@ -40,7 +47,9 @@ export type ReactZoomPanPinchRefProps = {
40
47
  } & Omit<ReactZoomPanPinchProps, "ref">;
41
48
 
42
49
  export type ReactZoomPanPinchProps = {
43
- children?: React.ReactNode | ((ref: ReactZoomPanPinchRef) => React.ReactNode);
50
+ children?:
51
+ | React.ReactNode
52
+ | ((ref: ReactZoomPanPinchContentRef) => React.ReactNode);
44
53
  ref?: React.Ref<ReactZoomPanPinchRef>;
45
54
  initialScale?: number;
46
55
  initialPositionX?: number;
@@ -1,5 +1,7 @@
1
1
  import {
2
+ ReactZoomPanPinchContentRef,
2
3
  ReactZoomPanPinchContext,
4
+ ReactZoomPanPinchContextState,
3
5
  ReactZoomPanPinchRef,
4
6
  } from "../models/context.model";
5
7
  import {
@@ -11,12 +13,11 @@ import {
11
13
  centerView,
12
14
  } from "../core/handlers/handlers.logic";
13
15
 
14
- export const getContext = (
16
+ export const getControls = (
15
17
  contextInstance: ReactZoomPanPinchContext,
16
- ): ReactZoomPanPinchRef => {
18
+ ): ReactZoomPanPinchContentRef => {
17
19
  return {
18
20
  instance: contextInstance,
19
- state: contextInstance.transformState,
20
21
  zoomIn: zoomIn(contextInstance),
21
22
  zoomOut: zoomOut(contextInstance),
22
23
  setTransform: setTransform(contextInstance),
@@ -25,3 +26,23 @@ export const getContext = (
25
26
  zoomToElement: zoomToElement(contextInstance),
26
27
  };
27
28
  };
29
+
30
+ export const getState = (
31
+ contextInstance: ReactZoomPanPinchContext,
32
+ ): ReactZoomPanPinchContextState => {
33
+ return {
34
+ instance: contextInstance,
35
+ state: contextInstance.transformState,
36
+ };
37
+ };
38
+
39
+ export const getContext = (
40
+ contextInstance: ReactZoomPanPinchContext,
41
+ ): ReactZoomPanPinchRef => {
42
+ const ref = {} as ReactZoomPanPinchRef;
43
+
44
+ Object.assign(ref, getState(contextInstance));
45
+ Object.assign(ref, getControls(contextInstance));
46
+
47
+ return ref;
48
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist/",
4
+ "module": "esnext",
5
+ "target": "es5",
6
+ "sourceMap": true,
7
+ "allowJs": false,
8
+ "jsx": "react",
9
+ "declaration": true,
10
+ "moduleResolution": "node",
11
+ "forceConsistentCasingInFileNames": true,
12
+ "noImplicitReturns": true,
13
+ "noImplicitThis": true,
14
+ "noImplicitAny": true,
15
+ "strictNullChecks": true,
16
+ "suppressImplicitAnyIndexErrors": true,
17
+ "noUnusedLocals": true,
18
+ "noUnusedParameters": true,
19
+ "allowSyntheticDefaultImports": true,
20
+ "baseUrl": "src",
21
+ "lib": ["dom", "dom.iterable", "esnext"],
22
+ "skipLibCheck": true,
23
+ "esModuleInterop": true,
24
+ "strict": true,
25
+ "noFallthroughCasesInSwitch": true,
26
+ "resolveJsonModule": true,
27
+ "isolatedModules": true,
28
+ "noEmit": true
29
+ },
30
+ "include": ["src", "__tests__"],
31
+ "exclude": ["node_modules", "build", "dist", "rollup.config.js"]
32
+ }
@@ -1,3 +0,0 @@
1
- import { ReactZoomPanPinchRef } from "../models";
2
- declare const useTransformContext: () => ReactZoomPanPinchRef;
3
- export default useTransformContext;