react-zoom-pan-pinch 2.1.3 → 2.2.1
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 +43 -0
- package/dist/index.cjs.js +60 -34
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.esm.js +60 -34
- package/dist/index.esm.js.map +1 -1
- package/package.json +5 -4
- package/src/.eslintrc.js +36 -0
- package/src/components/transform-component.module.css +30 -0
- package/src/components/transform-component.tsx +52 -0
- package/src/components/transform-context.tsx +493 -0
- package/src/components/transform-wrapper.tsx +18 -0
- package/src/constants/state.constants.ts +88 -0
- package/src/core/animations/animations.constants.ts +76 -0
- package/src/core/animations/animations.utils.ts +109 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +157 -0
- package/src/core/double-click/double-click.logic.ts +75 -0
- package/src/core/handlers/handlers.logic.ts +112 -0
- package/src/core/handlers/handlers.utils.ts +202 -0
- package/src/core/pan/panning.logic.ts +99 -0
- package/src/core/pan/panning.utils.ts +199 -0
- package/src/core/pan/velocity.logic.ts +148 -0
- package/src/core/pan/velocity.utils.ts +79 -0
- package/src/core/pinch/pinch.logic.ts +83 -0
- package/src/core/pinch/pinch.utils.ts +88 -0
- package/src/core/wheel/wheel.logic.ts +118 -0
- package/src/core/wheel/wheel.utils.ts +121 -0
- package/src/core/zoom/zoom.logic.ts +69 -0
- package/src/core/zoom/zoom.utils.ts +58 -0
- package/src/hooks/use-transform-context.tsx +16 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +26 -0
- package/src/models/context.model.ts +179 -0
- package/src/models/helpers.model.ts +15 -0
- package/src/models/index.ts +3 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/stories/assets/big-image.jpeg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/Introduction.stories.mdx +97 -0
- package/src/stories/docs/handlers.stories.mdx +48 -0
- package/src/stories/docs/handlers.tsx +107 -0
- package/src/stories/docs/props.stories.mdx +92 -0
- package/src/stories/docs/props.tsx +490 -0
- package/src/stories/examples/content/content.stories.mdx +122 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +42 -0
- package/src/stories/examples/content-rerendering/example.tsx +74 -0
- package/src/stories/examples/controls/controls.stories.mdx +71 -0
- package/src/stories/examples/image-big/image-big.stories.mdx +44 -0
- package/src/stories/examples/image-medium/image-medium.stories.mdx +42 -0
- package/src/stories/examples/image-small/image-small.stories.mdx +42 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +47 -0
- package/src/stories/examples/zoom-to-element/example.tsx +91 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
- package/src/stories/types/args.types.ts +201 -0
- package/src/stories/utils/args.utils.ts +34 -0
- package/src/stories/utils/code.utils.tsx +21 -0
- package/src/stories/utils/controls.utils.tsx +26 -0
- package/src/stories/utils/index.ts +3 -0
- package/src/stories/utils/styles.module.css +29 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +10 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +27 -0
- package/src/utils/event.utils.ts +22 -0
- package/src/utils/helpers.utils.ts +27 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/state.utils.ts +40 -0
- package/src/utils/styles.utils.ts +35 -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,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
package/src/typings.d.ts
ADDED
|
@@ -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,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,35 @@
|
|
|
1
|
+
import { StateType } from "models";
|
|
2
|
+
|
|
3
|
+
export const getTransformStyles = (
|
|
4
|
+
x: number,
|
|
5
|
+
y: number,
|
|
6
|
+
scale: number,
|
|
7
|
+
): string => {
|
|
8
|
+
// The shorthand for matrix does not work for Safari hence the need to explicitly use matrix3d
|
|
9
|
+
// Refer to https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix
|
|
10
|
+
const a = scale;
|
|
11
|
+
const b = 0;
|
|
12
|
+
const c = 0;
|
|
13
|
+
const d = scale;
|
|
14
|
+
const tx = x;
|
|
15
|
+
const ty = y;
|
|
16
|
+
return `matrix3d(${a}, ${b}, 0, 0, ${c}, ${d}, 0, 0, 0, 0, 1, 0, ${tx}, ${ty}, 0, 1)`;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const getCenterPosition = (
|
|
20
|
+
scale: number,
|
|
21
|
+
wrapperComponent: HTMLDivElement,
|
|
22
|
+
contentComponent: HTMLDivElement,
|
|
23
|
+
): StateType => {
|
|
24
|
+
const contentWidth = contentComponent.offsetWidth * scale;
|
|
25
|
+
const contentHeight = contentComponent.offsetHeight * scale;
|
|
26
|
+
|
|
27
|
+
const centerPositionX = (wrapperComponent.offsetWidth - contentWidth) / 2;
|
|
28
|
+
const centerPositionY = (wrapperComponent.offsetHeight - contentHeight) / 2;
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
scale,
|
|
32
|
+
positionX: centerPositionX,
|
|
33
|
+
positionY: centerPositionY,
|
|
34
|
+
};
|
|
35
|
+
};
|