point-focus 1.0.0 → 1.0.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.
@@ -1,3 +1,3 @@
1
1
  import { IImageMagnifierTypes } from './ImageMagnifier.types';
2
- declare const ImageMagnifier: ({ src, sources, width, height, zoomSrc, zoomScale, zoomPreload, fadeDuration, moveType, zoomType, baseImageStyle, hideCloseButton, containerClassName, baseImageClassName, zoomImageClassName, closeButtonClassName, onMouseEnter, onZoomedMouseMove, onMouseLeave, onClickImage, onZoom, onClose, afterZoomImgLoaded, afterZoomOut, onBaseImageError, onZoomImageError, onDragStart, onDragEnd, alt, containerAriaLabel, closeButtonAriaLabel, zoomImageAriaLabel, tabIndex, closeButtonContent, overlay, disableDrag, disableInertia, loadingPlaceholder, errorPlaceholder, clickToZoomOut, externalZoomState, setExternalZoomState, }: IImageMagnifierTypes) => import("react/jsx-runtime").JSX.Element;
2
+ declare const ImageMagnifier: ({ src, sources, width, height, zoomSrc, zoomScale, zoomPreload, fadeDuration, moveType, zoomType, baseImageStyle, hideCloseButton, containerClassName, baseImageClassName, zoomImageClassName, closeButtonClassName, onMouseEnter, onZoomedMouseMove, onMouseLeave, onClickImage, onZoom, onClose, afterZoomImgLoaded, afterZoomOut, onBaseImageError, onZoomImageError, onDragStart, onDragEnd, alt, containerAriaLabel, closeButtonAriaLabel, zoomImageAriaLabel, tabIndex, closeButtonContent, overlay, disableDrag, disableInertia, loadingPlaceholder, errorPlaceholder, clickToZoomOut, externalZoomState, setExternalZoomState, disableMobile, }: IImageMagnifierTypes) => import("react/jsx-runtime").JSX.Element;
3
3
  export default ImageMagnifier;
@@ -1,4 +1,5 @@
1
1
  export type ImageSource = React.SourceHTMLAttributes<HTMLSourceElement>;
2
+ export type IInteractionTYpe = 'click' | 'hover' | 'keyboard' | 'external';
2
3
  export interface IImageMagnifierTypes {
3
4
  src: string;
4
5
  sources?: ImageSource[];
@@ -26,18 +27,54 @@ export interface IImageMagnifierTypes {
26
27
  closeButtonContent?: React.ReactNode | undefined;
27
28
  overlay?: React.ReactNode | undefined;
28
29
  baseImageStyle?: React.CSSProperties | undefined;
29
- onMouseEnter?: (() => void) | undefined;
30
- onMouseLeave?: (() => void) | undefined;
31
- onClickImage?: (() => void) | undefined;
32
- onZoom?: (() => void) | undefined;
33
- onClose?: (() => void) | undefined;
34
- afterZoomImgLoaded?: (() => void) | undefined;
35
- afterZoomOut?: (() => void) | undefined;
36
- onBaseImageError?: (() => void) | undefined;
37
- onZoomImageError?: (() => void) | undefined;
38
- onDragStart?: (() => void) | undefined;
39
- onDragEnd?: (() => void) | undefined;
40
- onZoomedMouseMove?: (() => void) | undefined;
30
+ onMouseEnter?: (info: {
31
+ source: 'mouse' | 'touch';
32
+ }) => void;
33
+ onMouseLeave?: (info: {
34
+ source: 'mouse' | 'touch';
35
+ }) => void;
36
+ onClickImage?: (info: {
37
+ x: number;
38
+ y: number;
39
+ source: 'mouse' | 'touch';
40
+ }) => void;
41
+ onZoom?: (info: {
42
+ at: {
43
+ x: number;
44
+ y: number;
45
+ };
46
+ method: IInteractionTYpe;
47
+ }) => void;
48
+ onClose?: (info: {
49
+ triggeredBy: IInteractionTYpe;
50
+ }) => void;
51
+ afterZoomImgLoaded?: () => void;
52
+ afterZoomOut?: () => void;
53
+ onBaseImageError?: () => void;
54
+ onZoomImageError?: () => void;
55
+ onDragStart?: (info: {
56
+ start: {
57
+ x: number;
58
+ y: number;
59
+ };
60
+ source: 'mouse' | 'touch';
61
+ }) => void;
62
+ onDragEnd?: (info: {
63
+ velocity: {
64
+ vx: number;
65
+ vy: number;
66
+ };
67
+ final: {
68
+ x: number;
69
+ y: number;
70
+ };
71
+ }) => void;
72
+ onZoomedMouseMove?: (info: {
73
+ x: number;
74
+ y: number;
75
+ bounds: DOMRect;
76
+ }) => void;
77
+ disableMobile?: boolean;
41
78
  disableDrag?: boolean;
42
79
  disableInertia?: boolean;
43
80
  loadingPlaceholder?: React.ReactNode;
@@ -72,6 +109,12 @@ export type IImageTypes = {
72
109
  y: number;
73
110
  time: number;
74
111
  } | null;
112
+ rafId?: number | null;
113
+ lastMoveEvent?: {
114
+ x: number;
115
+ y: number;
116
+ bounds: DOMRect;
117
+ } | null;
75
118
  };
76
119
  export type IZoomImageTypes = {
77
120
  src: string;
@@ -81,7 +124,7 @@ export type IZoomImageTypes = {
81
124
  isZoomed: boolean;
82
125
  onLoad?: (e: React.SyntheticEvent<HTMLImageElement>) => void;
83
126
  onError?: () => void;
84
- onClose?: (e: React.MouseEvent) => void;
127
+ onClose?: (method: IInteractionTYpe) => void;
85
128
  onFadeOut?: (e: React.TransitionEvent<HTMLImageElement>) => void;
86
129
  closeButtonRef: React.RefObject<HTMLButtonElement>;
87
130
  onDragStart?: (e: React.MouseEvent | React.TouchEvent) => void | undefined;
@@ -131,4 +174,11 @@ export type InertiaOptions = {
131
174
  minVelocity?: number;
132
175
  onEnd?: () => void;
133
176
  };
177
+ export type ZoomFallbackBoundaryProps = {
178
+ fallback: React.ReactNode;
179
+ children: React.ReactNode;
180
+ };
181
+ export type ZoomFallbackBoundaryState = {
182
+ hasError: boolean;
183
+ };
134
184
  export {};
@@ -0,0 +1,6 @@
1
+ export declare const CloseIcon: ({ className }: {
2
+ className?: string;
3
+ }) => import("react/jsx-runtime").JSX.Element;
4
+ export declare const FallbackImage: ({ className }: {
5
+ className?: string;
6
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+ declare const ImageStatusPlaceholder: React.FC<{
3
+ isLoading?: boolean;
4
+ isError?: boolean;
5
+ loadingPlaceholder?: React.ReactNode;
6
+ errorPlaceholder?: React.ReactNode;
7
+ loadingTestId?: string;
8
+ errorTestId?: string;
9
+ }>;
10
+ export default ImageStatusPlaceholder;
package/dist/index.cjs CHANGED
@@ -30,17 +30,20 @@ function styleInject(css, ref) {
30
30
  }
31
31
  }
32
32
 
33
- var css_248z = ":root{--button-icon-color:#e4e4e4}.styles-module_c-point-focus__Glxyg{align-items:center;cursor:zoom-in;display:inline-block;display:flex;height:100%;justify-content:center;margin:0;overflow:hidden;position:relative;width:100%}.styles-module_c-point-focus__Glxyg[data-movetype=follow] .styles-module_c-point-focus__zoom-img__HP-hu{cursor:crosshair}.styles-module_c-point-focus__Glxyg[data-movetype=drag] .styles-module_c-point-focus__zoom-img__HP-hu{cursor:grab}.styles-module_c-point-focus__overlay__T8nCD{inset:0;pointer-events:none;position:absolute;z-index:1}.styles-module_c-point-focus__placeholder__dHGk6{align-items:center;display:flex;inset:0;justify-content:center;position:absolute;z-index:2}.styles-module_c-point-focus__img__jCS0m{-webkit-user-drag:none;aspect-ratio:4/3;display:block;height:100%;object-fit:contain;opacity:1;pointer-events:none;transition:opacity 0ms linear var(--fade-duration),visibility 0ms linear var(--fade-duration);user-select:none;visibility:visible;width:100%}.styles-module_c-point-focus__img__jCS0m[data-hidden=true]{opacity:0;visibility:hidden}.styles-module_c-point-focus__zoom-img__HP-hu{-webkit-user-drag:none;-webkit-touch-callout:none;display:block;left:0;max-width:none!important;opacity:0;pointer-events:none;position:absolute;top:0;transform:translate(0);user-select:none;visibility:hidden;width:auto!important;will-change:transform}.styles-module_c-point-focus__zoom-img__HP-hu[data-visible=true]{cursor:zoom-out;opacity:1;pointer-events:auto;touch-action:none;visibility:visible}.styles-module_c-point-focus__button__oCELz{align-items:center;appearance:none;background:transparent;border:none;cursor:pointer;display:flex;justify-content:center;opacity:0;outline:none;padding:.3rem;position:absolute;right:2%;text-decoration:none;top:2%;transform-origin:center center;visibility:hidden}.styles-module_c-point-focus__button__oCELz[data-visible=true]{opacity:1;visibility:visible}.styles-module_c-point-focus__button__oCELz svg{filter:drop-shadow(0 0 10px rgba(0,0,0,.7));height:1rem;transform-origin:center;transition:transform .2s ease-in-out;width:1rem}.styles-module_c-point-focus__button__oCELz svg path{fill:var(--button-icon-color)}.styles-module_c-point-focus__button__oCELz:hover svg{transform:scale(1.2)}.styles-module_c-point-focus__button__oCELz:active svg{transform:scale(1)}";
34
- var styles = {"c-point-focus":"styles-module_c-point-focus__Glxyg","c-point-focus__zoom-img":"styles-module_c-point-focus__zoom-img__HP-hu","c-point-focus__overlay":"styles-module_c-point-focus__overlay__T8nCD","c-point-focus__placeholder":"styles-module_c-point-focus__placeholder__dHGk6","c-point-focus__img":"styles-module_c-point-focus__img__jCS0m","c-point-focus__button":"styles-module_c-point-focus__button__oCELz"};
33
+ var css_248z = ":root{--button-icon-color:#1e1c1c;--loader-background:#19181891;--loader-color:#fff}.styles-module_c-point-focus__Glxyg{align-items:center;cursor:zoom-in;display:inline-block;display:flex;height:100%;justify-content:center;margin:0;overflow:hidden;position:relative;width:100%}.styles-module_c-point-focus__Glxyg[data-movetype=follow] .styles-module_c-point-focus__zoom-img__HP-hu{cursor:crosshair}.styles-module_c-point-focus__Glxyg[data-movetype=drag] .styles-module_c-point-focus__zoom-img__HP-hu{cursor:grab}.styles-module_c-point-focus__overlay__T8nCD{inset:0;pointer-events:none;position:absolute;z-index:1}.styles-module_c-point-focus__placeholder__dHGk6{align-items:center;background:var(--loader-background);display:flex;inset:0;justify-content:center;position:absolute;z-index:2}.styles-module_c-point-focus__img__jCS0m{-webkit-user-drag:none;aspect-ratio:4/3;display:block;height:100%;object-fit:contain;opacity:1;pointer-events:none;transition:opacity 0ms linear var(--fade-duration),visibility 0ms linear var(--fade-duration);user-select:none;visibility:visible;width:100%}.styles-module_c-point-focus__img__jCS0m[data-hidden=true]{opacity:0;visibility:hidden}.styles-module_c-point-focus__zoom-img__HP-hu{-webkit-user-drag:none;-webkit-touch-callout:none;display:block;left:0;max-width:none!important;opacity:0;pointer-events:none;position:absolute;top:0;transform:translate(0);user-select:none;visibility:hidden;width:auto!important;will-change:transform}.styles-module_c-point-focus__zoom-img__HP-hu[data-visible=true]{cursor:zoom-out;opacity:1;pointer-events:auto;touch-action:none;visibility:visible}.styles-module_c-point-focus__button__oCELz{align-items:center;appearance:none;background:transparent;border:none;cursor:pointer;display:flex;justify-content:center;opacity:0;outline:none;padding:.3rem;position:absolute;right:2%;text-decoration:none;top:2%;transform-origin:center center;visibility:hidden}.styles-module_c-point-focus__button__oCELz[data-visible=true]{opacity:1;visibility:visible}.styles-module_c-point-focus__button__oCELz svg{filter:drop-shadow(0 0 10px rgba(0,0,0,.7));height:1rem;transform-origin:center;transition:transform .2s ease-in-out;width:1rem}.styles-module_c-point-focus__button__oCELz svg path{fill:var(--button-icon-color)}.styles-module_c-point-focus__button__oCELz:hover svg{transform:scale(1.2)}.styles-module_c-point-focus__button__oCELz:active{appearance:none;background:transparent;border:none;outline:none}.styles-module_c-point-focus__button__oCELz:active svg{transform:scale(1)}.styles-module_c-point-focus__loader__AIAkB{display:inline-block;height:48px;position:relative;width:48px}.styles-module_c-point-focus__loader__AIAkB:after,.styles-module_c-point-focus__loader__AIAkB:before{animation:styles-module_animloader__7LeVS 2s linear infinite;border:2px solid var(--loader-color);border-radius:50%;box-sizing:border-box;content:\"\";height:48px;left:0;position:absolute;top:0;width:48px}.styles-module_c-point-focus__loader__AIAkB:after{animation-delay:1s}@keyframes styles-module_animloader__7LeVS{0%{opacity:1;transform:scale(0)}to{opacity:0;transform:scale(2)}}.styles-module_c-point-focus__Glxyg .styles-module_c-fp-fallback-image__sROkG{height:100%;inset:0;object-fit:cover;object-position:center center;position:absolute;width:100%;z-index:2}";
34
+ var styles = {"c-point-focus":"styles-module_c-point-focus__Glxyg","c-point-focus__zoom-img":"styles-module_c-point-focus__zoom-img__HP-hu","c-point-focus__overlay":"styles-module_c-point-focus__overlay__T8nCD","c-point-focus__placeholder":"styles-module_c-point-focus__placeholder__dHGk6","c-point-focus__img":"styles-module_c-point-focus__img__jCS0m","c-point-focus__button":"styles-module_c-point-focus__button__oCELz","c-point-focus__loader":"styles-module_c-point-focus__loader__AIAkB","animloader":"styles-module_animloader__7LeVS","c-fp-fallback-image":"styles-module_c-fp-fallback-image__sROkG"};
35
35
  styleInject(css_248z);
36
36
 
37
+ function isTouchEvent(e) {
38
+ return 'touches' in e || ('nativeEvent' in e && 'touches' in e.nativeEvent);
39
+ }
37
40
  // Defaulted values to 0 to make the functions SSR safe
38
- const getPageCoords = (e, fallbackElement) => {
39
- if ('touches' in e && e.touches.length > 0) {
41
+ function getPageCoords(e, fallbackElement) {
42
+ if ('touches' in e && e.touches && e.touches.length > 0) {
40
43
  return { x: e.touches[0].pageX, y: e.touches[0].pageY };
41
44
  }
42
45
  else if ('pageX' in e && 'pageY' in e) {
43
- return { x: e.pageX, y: e.pageY };
46
+ return { x: e.pageX, y: e.pageY }; // Could further narrow with type guards
44
47
  }
45
48
  else if (fallbackElement) {
46
49
  const rect = fallbackElement.getBoundingClientRect();
@@ -50,7 +53,7 @@ const getPageCoords = (e, fallbackElement) => {
50
53
  };
51
54
  }
52
55
  return { x: 0, y: 0 };
53
- };
56
+ }
54
57
  const getOffsets = (pageX, pageY, left, top) => {
55
58
  return {
56
59
  x: pageX - left,
@@ -102,6 +105,24 @@ const getDefaults = () => {
102
105
  lastDragCoords: { x: 0, y: 0, time: 0 },
103
106
  };
104
107
  };
108
+ class ZoomFallbackBoundary extends React.Component {
109
+ constructor(props) {
110
+ super(props);
111
+ this.state = { hasError: false };
112
+ }
113
+ static getDerivedStateFromError() {
114
+ return { hasError: true };
115
+ }
116
+ componentDidCatch(error, info) {
117
+ console.error('ZoomImage runtime error:', error, info);
118
+ }
119
+ render() {
120
+ if (this.state.hasError) {
121
+ return this.props.fallback ?? null;
122
+ }
123
+ return this.props.children;
124
+ }
125
+ }
105
126
 
106
127
  const applyMouseMove = (pageX, pageY, zoomContextRef, setLeft, setTop) => {
107
128
  const { left, top } = calculateDragPosition(pageX, pageY, zoomContextRef.current.offsets);
@@ -143,9 +164,16 @@ function applyDragMove(x, y, zoomContextRef, setLeft, setTop) {
143
164
  setTop(clamped.top);
144
165
  }
145
166
 
167
+ const CloseIcon = ({ className = 'c-zoom-close-button-icon' }) => (jsxRuntime.jsx("svg", { xmlns: 'http://www.w3.org/2000/svg', className: className, viewBox: '0 0 24 24', fill: 'none', children: jsxRuntime.jsx("path", { fillRule: 'evenodd', clipRule: 'evenodd', d: 'M5.29289 5.29289C5.68342 4.90237 6.31658 4.90237 6.70711 5.29289L12 10.5858L17.2929 5.29289C17.6834 4.90237 18.3166 4.90237 18.7071 5.29289C19.0976 5.68342 19.0976 6.31658 18.7071 6.70711L13.4142 12L18.7071 17.2929C19.0976 17.6834 19.0976 18.3166 18.7071 18.7071C18.3166 19.0976 17.6834 19.0976 17.2929 18.7071L12 13.4142L6.70711 18.7071C6.31658 19.0976 5.68342 19.0976 5.29289 18.7071C4.90237 18.3166 4.90237 17.6834 5.29289 17.2929L10.5858 12L5.29289 6.70711C4.90237 6.31658 4.90237 5.68342 5.29289 5.29289Z', fill: '#000' }) }));
168
+ const FallbackImage = ({ className = 'c-fp-fallback-image' }) => (jsxRuntime.jsxs("svg", { className: className, viewBox: '0 0 2153 1548', fill: 'none', xmlns: 'http://www.w3.org/2000/svg', children: [jsxRuntime.jsx("rect", { width: '100%', height: '100%', fill: 'white' }), jsxRuntime.jsx("path", { d: 'M762 504.5H1392C1425.97 504.5 1453.5 532.034 1453.5 566V982C1453.5 1015.97 1425.97 1043.5 1392 1043.5H762C728.034 1043.5 700.5 1015.97 700.5 982V566C700.5 532.034 728.034 504.5 762 504.5Z', fill: '#EBEEF0', stroke: '#8495A2', strokeWidth: '25' }), jsxRuntime.jsx("path", { d: 'M901.252 812.329C880.253 832.829 710.25 1012.23 707.251 1014.33C704.252 1016.43 728.751 1038.83 745.252 1038.83C761.752 1038.83 1381.75 1039.33 1388.25 1038.83C1394.75 1038.33 1443.75 1036.33 1445.75 981.832C1447.75 927.332 1445.75 946.332 1445.75 946.332C1445.75 946.332 1243.75 723.83 1225.75 705.829C1207.75 687.827 1194.25 681.832 1170.25 705.829C1146.25 729.826 1012.47 866.329 1012.47 866.329C1012.47 866.329 976.253 827.329 957.752 808.829C939.252 790.329 922.252 791.829 901.252 812.329Z', fill: '#CBD4DB' }), jsxRuntime.jsx("path", { d: 'M1056.25 912.329L1012.47 866.329M1012.47 866.329C1012.47 866.329 976.253 827.329 957.752 808.829C939.252 790.329 922.252 791.829 901.252 812.329C880.253 832.829 710.25 1012.23 707.251 1014.33C704.252 1016.43 728.751 1038.83 745.252 1038.83C761.752 1038.83 1381.75 1039.33 1388.25 1038.83C1394.75 1038.33 1443.75 1036.33 1445.75 981.832C1447.75 927.332 1445.75 946.332 1445.75 946.332C1445.75 946.332 1243.75 723.83 1225.75 705.829C1207.75 687.827 1194.25 681.832 1170.25 705.829C1146.25 729.826 1012.47 866.329 1012.47 866.329Z', stroke: '#8595A1', strokeWidth: '25', strokeLinecap: 'round' }), jsxRuntime.jsx("circle", { cx: '891.5', cy: '689.5', r: '60', fill: '#CBD4DA', stroke: '#8496A2', strokeWidth: '25' })] }));
169
+
146
170
  const BaseImage = ({ src, sources, baseImageStyle = {}, isZoomed, fadeDuration, baseImageClassName, alt, loadingPlaceholder, errorPlaceholder, onError, }) => {
147
171
  const [isLoading, setIsLoading] = React.useState(true);
148
172
  const [hasError, setHasError] = React.useState(false);
173
+ React.useEffect(() => {
174
+ setIsLoading(true);
175
+ setHasError(false);
176
+ }, [src]);
149
177
  const handleLoad = () => setIsLoading(false);
150
178
  const handleError = () => {
151
179
  setHasError(true);
@@ -157,14 +185,12 @@ const BaseImage = ({ src, sources, baseImageStyle = {}, isZoomed, fadeDuration,
157
185
  ...baseImageStyle,
158
186
  }), [isZoomed, fadeDuration, baseImageStyle]);
159
187
  const imageClass = [styles['c-point-focus__img'], baseImageClassName].filter(Boolean).join(' ');
160
- const imgElement = (jsxRuntime.jsx("img", { alt: alt, src: src, style: imageStyle, onLoad: handleLoad, onError: handleError, className: imageClass, "data-hidden": isZoomed, "data-testid": 'pf-base-image' }));
188
+ const imgElement = !hasError ? (jsxRuntime.jsx("img", { alt: alt, src: src, style: imageStyle, onLoad: handleLoad, onError: handleError, className: imageClass, "data-hidden": isZoomed, "data-testid": 'pf-base-image' })) : null;
161
189
  return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [sources && sources.length > 0 ? (jsxRuntime.jsxs("picture", { style: { width: '100%', height: '100%' }, children: [sources
162
190
  .filter(s => s.srcSet)
163
- .map((source, i) => (jsxRuntime.jsx("source", { ...source }, i))), imgElement] })) : (imgElement), isLoading && loadingPlaceholder && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-base-loading', children: loadingPlaceholder })), hasError && errorPlaceholder && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-base-error', children: errorPlaceholder }))] }));
191
+ .map((source, i) => (jsxRuntime.jsx("source", { ...source }, i))), imgElement] })) : (imgElement), isLoading && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], style: { zIndex: 0 }, "data-testid": 'pf-base-loading', children: loadingPlaceholder ?? jsxRuntime.jsx(FallbackImage, {}) })), hasError && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], style: { zIndex: 0 }, "data-testid": 'pf-base-error', children: errorPlaceholder ?? jsxRuntime.jsx(FallbackImage, {}) }))] }));
164
192
  };
165
193
 
166
- const CloseIcon = ({ className = 'c-zoom-close-button-icon' }) => (jsxRuntime.jsx("svg", { xmlns: 'http://www.w3.org/2000/svg', className: className, viewBox: '0 0 24 24', fill: 'none', children: jsxRuntime.jsx("path", { fillRule: 'evenodd', clipRule: 'evenodd', d: 'M5.29289 5.29289C5.68342 4.90237 6.31658 4.90237 6.70711 5.29289L12 10.5858L17.2929 5.29289C17.6834 4.90237 18.3166 4.90237 18.7071 5.29289C19.0976 5.68342 19.0976 6.31658 18.7071 6.70711L13.4142 12L18.7071 17.2929C19.0976 17.6834 19.0976 18.3166 18.7071 18.7071C18.3166 19.0976 17.6834 19.0976 17.2929 18.7071L12 13.4142L6.70711 18.7071C6.31658 19.0976 5.68342 19.0976 5.29289 18.7071C4.90237 18.3166 4.90237 17.6834 5.29289 17.2929L10.5858 12L5.29289 6.70711C4.90237 6.31658 4.90237 5.68342 5.29289 5.29289Z', fill: '#000' }) }));
167
-
168
194
  const ZoomImage = React.forwardRef(({ src, fadeDuration, top, left, isZoomed, onLoad, onDragStart, onDragEnd, onClose, onFadeOut, closeButtonRef, onTouchStart, onTouchEnd, zoomImageClassName, closeButtonClassName, alt, closeButtonAriaLabel, zoomImageAriaLabel, onKeyDown, closeButtonContent, onError, loadingPlaceholder, errorPlaceholder, }, ref) => {
169
195
  const [isLoading, setIsLoading] = React.useState(true);
170
196
  const [hasError, setHasError] = React.useState(false);
@@ -187,9 +213,9 @@ const ZoomImage = React.forwardRef(({ src, fadeDuration, top, left, isZoomed, on
187
213
  return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("img", { ref: ref, "data-testid": 'pf-zoom-image', "data-visible": isZoomed, className: zoomImgClass, style: {
188
214
  transform: `translate(${left}px, ${top}px)`,
189
215
  transition: `opacity ${fadeDuration}ms linear, visibility ${fadeDuration}ms linear`,
190
- }, src: src, onLoad: handleLoad, onError: handleError, onMouseDown: onDragStart, onMouseUp: onDragEnd, onTouchStart: onTouchStart, onTouchEnd: onTouchEnd, onTransitionEnd: onFadeOut, draggable: false, alt: `${alt} zoomed`, "aria-label": zoomImageAriaLabel, onKeyDown: onKeyDown, tabIndex: 0 }), hasError && errorPlaceholder && isZoomed && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-zoom-error', children: errorPlaceholder })), isLoading && !hasError && loadingPlaceholder && isZoomed && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-zoom-loading', children: loadingPlaceholder })), onClose && (jsxRuntime.jsx("button", { type: 'button', ref: closeButtonRef, title: 'Close Zoom', "data-testid": 'pf-close-button', "aria-label": closeButtonAriaLabel ?? 'Zoom out', "data-visible": isZoomed, "data-action-type": 'close', className: closeButtonClassName || styles['c-point-focus__button'], style: {
216
+ }, src: src, onLoad: handleLoad, onError: handleError, onMouseDown: onDragStart, onMouseUp: onDragEnd, onTouchStart: onTouchStart, onTouchEnd: onTouchEnd, onTransitionEnd: onFadeOut, draggable: false, alt: `${alt} zoomed`, "aria-label": zoomImageAriaLabel, onKeyDown: onKeyDown, tabIndex: 0 }), hasError && isZoomed && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-zoom-error', children: errorPlaceholder ?? jsxRuntime.jsx(FallbackImage, {}) })), isLoading && !hasError && isZoomed && (jsxRuntime.jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-zoom-loading', children: loadingPlaceholder ?? jsxRuntime.jsx("span", { className: styles['c-point-focus__loader'] }) })), onClose && (jsxRuntime.jsx("button", { type: 'button', ref: closeButtonRef, title: 'Close Zoom', "data-testid": 'pf-close-button', "aria-label": closeButtonAriaLabel ?? 'Zoom out', "data-visible": isZoomed, "data-action-type": 'close', className: closeButtonClassName || styles['c-point-focus__button'], style: {
191
217
  transition: `opacity ${fadeDuration}ms linear, visibility ${fadeDuration}ms linear`,
192
- }, onClick: onClose, onKeyDown: onKeyDown, children: closeButtonContent ?? jsxRuntime.jsx(CloseIcon, {}) }))] }));
218
+ }, onClick: () => onClose?.('click'), onKeyDown: onKeyDown, children: closeButtonContent ?? jsxRuntime.jsx(CloseIcon, {}) }))] }));
193
219
  });
194
220
  ZoomImage.displayName = 'ZoomImage';
195
221
 
@@ -222,7 +248,7 @@ function startInertia({ initialLeft, initialTop, velocity, setLeft, setTop, boun
222
248
  requestAnimationFrame(step);
223
249
  }
224
250
 
225
- const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, zoomPreload, fadeDuration = 150, moveType = 'follow', zoomType = 'click', baseImageStyle, hideCloseButton, containerClassName, baseImageClassName, zoomImageClassName, closeButtonClassName, onMouseEnter, onZoomedMouseMove, onMouseLeave, onClickImage, onZoom, onClose, afterZoomImgLoaded, afterZoomOut, onBaseImageError, onZoomImageError, onDragStart, onDragEnd, alt, containerAriaLabel, closeButtonAriaLabel, zoomImageAriaLabel, tabIndex = 0, closeButtonContent, overlay, disableDrag, disableInertia, loadingPlaceholder, errorPlaceholder, clickToZoomOut = false, externalZoomState, setExternalZoomState, }) => {
251
+ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, zoomPreload, fadeDuration = 150, moveType = 'follow', zoomType = 'click', baseImageStyle, hideCloseButton, containerClassName, baseImageClassName, zoomImageClassName, closeButtonClassName, onMouseEnter, onZoomedMouseMove, onMouseLeave, onClickImage, onZoom, onClose, afterZoomImgLoaded, afterZoomOut, onBaseImageError, onZoomImageError, onDragStart, onDragEnd, alt, containerAriaLabel, closeButtonAriaLabel, zoomImageAriaLabel, tabIndex = 0, closeButtonContent, overlay, disableDrag, disableInertia, loadingPlaceholder, errorPlaceholder, clickToZoomOut = false, externalZoomState, setExternalZoomState, disableMobile, }) => {
226
252
  const containerRef = React.useRef(null);
227
253
  const closeButtonRef = React.useRef(null);
228
254
  const zoomedImgRef = React.useRef(null);
@@ -243,10 +269,10 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
243
269
  setInternalZoom(val);
244
270
  }
245
271
  };
246
- const zoomIn = (pageX, pageY) => {
272
+ const zoomIn = (pageX, pageY, method) => {
247
273
  updateZoomState(true);
248
274
  initializeFollowZoomPosition(pageX, pageY, containerRef.current, zoomContextRef, setLeft, setTop);
249
- onZoom?.();
275
+ onZoom?.({ at: { x: pageX, y: pageY }, method: method });
250
276
  setTimeout(() => {
251
277
  closeButtonRef.current?.focus();
252
278
  }, 0);
@@ -265,26 +291,28 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
265
291
  setTop(0);
266
292
  updateZoomState(false);
267
293
  };
268
- const handleDragStart = (e) => {
269
- if (!isZoomed || disableDrag)
294
+ const handleDragStart = React.useCallback((e) => {
295
+ if (!isZoomed || disableDrag || (disableMobile && isTouchEvent(e)))
270
296
  return;
271
297
  const { x, y } = getPageCoords(e);
272
298
  setIsDragging(true);
273
299
  zoomContextRef.current.dragStartCoords = getOffsets(x, y, left, top);
274
- onDragStart?.();
275
- };
276
- const handleDragMove = (e) => {
300
+ onDragStart?.({
301
+ start: { x, y },
302
+ source: isTouchEvent(e) ? 'touch' : 'mouse',
303
+ });
304
+ }, [isZoomed, disableDrag, disableMobile, left, top, onDragStart]);
305
+ const handleDragMove = React.useCallback((e) => {
277
306
  if (!isDragging)
278
307
  return;
279
308
  const { x, y } = getPageCoords(e);
280
309
  applyDragMove(x, y, zoomContextRef, setLeft, setTop);
281
310
  if ('touches' in e)
282
311
  e.preventDefault?.();
283
- };
284
- const handleDragEnd = () => {
312
+ }, [isDragging, setLeft, setTop]);
313
+ const handleDragEnd = React.useCallback(() => {
285
314
  setIsDragging(false);
286
315
  zoomContextRef.current.dragStartCoords = { x: 0, y: 0 };
287
- onDragEnd?.();
288
316
  // Animation on end
289
317
  // Calculate velocity
290
318
  const last = zoomContextRef.current.lastDragCoords;
@@ -319,7 +347,11 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
319
347
  },
320
348
  });
321
349
  }
322
- };
350
+ onDragEnd?.({
351
+ velocity: { vx, vy },
352
+ final: { x: left, y: top },
353
+ });
354
+ }, [disableInertia, left, top, setLeft, setTop, onDragEnd]);
323
355
  const applyImageLoad = (el) => {
324
356
  const scaledDimensions = getScaledDimensions(el, zoomScale);
325
357
  zoomedImgRef.current = el;
@@ -333,73 +365,90 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
333
365
  zoomContextRef.current.onLoadCallback = null;
334
366
  }
335
367
  };
336
- const handleLoad = (e) => {
368
+ const handleLoad = React.useCallback((e) => {
337
369
  applyImageLoad(e.currentTarget);
338
370
  afterZoomImgLoaded?.();
339
- };
340
- const handleClose = (e) => {
341
- onClose?.();
371
+ }, [applyImageLoad, afterZoomImgLoaded]);
372
+ const handleClose = React.useCallback((method) => {
373
+ onClose?.({ triggeredBy: method });
342
374
  zoomOut();
343
- };
344
- const handleClick = (e) => {
345
- onClickImage?.();
375
+ }, [onClose, zoomOut]);
376
+ const handleClick = React.useCallback((e, method) => {
377
+ if (disableMobile && isTouchEvent(e))
378
+ return;
379
+ const { x, y } = getPageCoords(e);
380
+ onClickImage?.({ x, y, source: isTouchEvent(e) ? 'touch' : 'mouse' });
346
381
  if (zoomContextRef.current.wasDragging) {
347
382
  zoomContextRef.current.wasDragging = false;
348
383
  return;
349
384
  }
350
385
  if (isZoomed) {
351
386
  if (clickToZoomOut) {
352
- zoomOut();
387
+ handleClose(method);
353
388
  }
354
389
  return;
355
390
  } // is important to keep it to avoid issues when dragging the image with the animation
356
391
  if (zoomedImgRef.current) {
357
392
  applyImageLoad(zoomedImgRef.current);
358
- const { x, y } = getPageCoords(e);
359
- zoomIn(x, y);
393
+ zoomIn(x, y, method);
360
394
  }
361
395
  else {
362
- const { x, y } = getPageCoords(e);
363
- zoomContextRef.current.onLoadCallback = () => zoomIn(x, y);
396
+ zoomContextRef.current.onLoadCallback = () => zoomIn(x, y, method);
364
397
  }
365
- };
366
- const handleKeyDown = (e) => {
398
+ }, [disableMobile, isTouchEvent, getPageCoords, onClickImage, isZoomed, clickToZoomOut, handleClose, applyImageLoad, zoomIn]);
399
+ const handleKeyDown = React.useCallback((e) => {
367
400
  if ((e.key === 'Enter' || e.key === ' ') && !isZoomed) {
368
401
  e.preventDefault();
369
402
  const { x, y } = getPageCoords(e, containerRef.current);
370
- zoomIn(x, y);
403
+ zoomIn(x, y, 'keyboard');
371
404
  }
372
405
  if (e.key === 'Escape' && isZoomed) {
373
406
  e.preventDefault();
374
- zoomOut();
407
+ handleClose('keyboard');
375
408
  }
376
- };
377
- const handleZoomLayerKeyDown = (e) => {
409
+ }, [isZoomed, getPageCoords, zoomIn, handleClose]);
410
+ const handleZoomLayerKeyDown = React.useCallback((e) => {
378
411
  if (e.key === 'Escape' && isZoomed) {
379
412
  e.preventDefault();
380
- zoomOut();
413
+ handleClose('keyboard');
381
414
  }
382
- };
383
- const handleMouseEnter = (e) => {
415
+ }, [isZoomed, handleClose]);
416
+ const throttledZoomedMouseMove = React.useCallback((x, y, bounds) => {
417
+ zoomContextRef.current.lastMoveEvent = { x, y, bounds };
418
+ if (zoomContextRef.current.rafId == null) {
419
+ zoomContextRef.current.rafId = window.requestAnimationFrame(() => {
420
+ const evt = zoomContextRef.current.lastMoveEvent;
421
+ if (evt && onZoomedMouseMove) {
422
+ onZoomedMouseMove(evt);
423
+ }
424
+ zoomContextRef.current.rafId = null;
425
+ });
426
+ }
427
+ }, [onZoomedMouseMove]);
428
+ const handleMouseEnter = React.useCallback((e) => {
384
429
  setIsActive(true);
385
430
  setIsFading(false);
386
- onMouseEnter?.();
387
- if (zoomType === 'hover' && !isZoomed) {
388
- handleClick(e);
431
+ onMouseEnter?.({ source: isTouchEvent(e) ? 'touch' : 'mouse' });
432
+ if (zoomType === 'hover' && !isZoomed && !(disableMobile && isTouchEvent(e))) {
433
+ handleClick(e, 'hover');
389
434
  }
390
- };
391
- const handleMouseMove = (e) => {
392
- onZoomedMouseMove?.();
435
+ }, [setIsActive, setIsFading, onMouseEnter, zoomType, isZoomed, disableMobile, handleClick]);
436
+ const handleMouseMove = React.useCallback((e) => {
393
437
  if (moveType === 'follow' && isZoomed) {
394
- applyMouseMove(e.pageX, e.pageY, zoomContextRef, setLeft, setTop);
438
+ const { x, y } = getPageCoords(e);
439
+ const bounds = zoomContextRef.current.bounds;
440
+ if (!bounds || typeof bounds.left !== 'number')
441
+ return;
442
+ throttledZoomedMouseMove(x, y, zoomContextRef.current.bounds);
443
+ applyMouseMove(x, y, zoomContextRef, setLeft, setTop);
395
444
  }
396
- };
397
- const handleMouseLeave = (e) => {
398
- onMouseLeave?.();
399
- handleClose();
400
- };
401
- const handleFadeOut = (e, noTransition) => {
402
- if (noTransition || (e.propertyName === 'opacity' && containerRef?.current?.contains(e.target))) {
445
+ }, [moveType, isZoomed, throttledZoomedMouseMove, setLeft, setTop]);
446
+ const handleMouseLeave = React.useCallback((e) => {
447
+ onMouseLeave?.({ source: isTouchEvent(e) ? 'touch' : 'mouse' });
448
+ handleClose('hover');
449
+ }, [onMouseLeave, handleClose]);
450
+ const handleFadeOut = React.useCallback((e, noTransition) => {
451
+ if (noTransition || (e && 'propertyName' in e && e.propertyName === 'opacity' && containerRef?.current?.contains(e.target))) {
403
452
  if (!zoomPreload) {
404
453
  zoomedImgRef.current = null;
405
454
  zoomContextRef.current = getDefaults();
@@ -408,10 +457,16 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
408
457
  setIsFading(false);
409
458
  afterZoomOut?.();
410
459
  }
411
- };
460
+ }, [zoomPreload, setIsActive, setIsFading, afterZoomOut]);
412
461
  // possibly extra to be removed
413
462
  React.useEffect(() => {
414
463
  zoomContextRef.current = getDefaults();
464
+ return () => {
465
+ if (zoomContextRef.current.rafId != null) {
466
+ cancelAnimationFrame(zoomContextRef.current.rafId);
467
+ zoomContextRef.current.rafId = null;
468
+ }
469
+ };
415
470
  }, []);
416
471
  React.useEffect(() => {
417
472
  if (isDragging && !disableDrag) {
@@ -453,7 +508,11 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
453
508
  errorPlaceholder: errorPlaceholder,
454
509
  };
455
510
  const containerClass = [styles['c-point-focus'], containerClassName].filter(Boolean).join(' ');
456
- return (jsxRuntime.jsxs("figure", { role: 'group', ref: containerRef, tabIndex: tabIndex, "data-movetype": moveType, "data-testid": 'pf-image-magnifier-container', "aria-label": containerAriaLabel ?? 'Zoomable image', className: containerClass, style: { width: width, height: height }, onClick: handleClick, onKeyDown: handleKeyDown, onMouseEnter: handleMouseEnter, onMouseMove: isZoomed ? handleMouseMove : undefined, onMouseLeave: handleMouseLeave, children: [jsxRuntime.jsx(BaseImage, { src: src, alt: alt, sources: sources, width: width, height: height, baseImageStyle: baseImageStyle, baseImageClassName: baseImageClassName, fadeDuration: fadeDuration, isZoomed: isZoomed, onError: onBaseImageError }), isActive && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [overlay && (jsxRuntime.jsx("div", { className: styles['c-point-focus__overlay'], "data-testid": 'pf-overlay', children: overlay })), jsxRuntime.jsx(ZoomImage, { ref: zoomedImgRef, ...zoomImageProps }, isZoomed && clickToZoomOut ? 'zoomed' : 'unzoomed')] }))] }));
511
+ return (jsxRuntime.jsxs("figure", { role: 'group', ref: containerRef, tabIndex: tabIndex, "data-movetype": moveType, "data-testid": 'pf-image-magnifier-container', "aria-label": containerAriaLabel ?? 'Zoomable image', className: containerClass, style: { width: width, height: height }, onClick: e => handleClick(e, 'hover'), onTouchStart: e => {
512
+ if (disableMobile) {
513
+ e.preventDefault();
514
+ }
515
+ }, onKeyDown: handleKeyDown, onMouseEnter: handleMouseEnter, onMouseMove: isZoomed ? handleMouseMove : undefined, onMouseLeave: handleMouseLeave, children: [jsxRuntime.jsx(BaseImage, { src: src, alt: alt, sources: sources, width: width, height: height, baseImageStyle: baseImageStyle, baseImageClassName: baseImageClassName, fadeDuration: fadeDuration, isZoomed: isZoomed, onError: onBaseImageError }), isActive && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [overlay && (jsxRuntime.jsx("div", { className: styles['c-point-focus__overlay'], "data-testid": 'pf-overlay', children: overlay })), jsxRuntime.jsx(ZoomFallbackBoundary, { fallback: errorPlaceholder ?? jsxRuntime.jsx(FallbackImage, {}), children: jsxRuntime.jsx(ZoomImage, { ref: zoomedImgRef, ...zoomImageProps }, isZoomed && clickToZoomOut ? 'zoomed' : 'unzoomed') })] }))] }));
457
516
  };
458
517
 
459
518
  module.exports = ImageMagnifier;