point-focus 1.0.0 → 1.0.3
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/dist/ImageMagnifier.d.ts +1 -1
- package/dist/ImageMagnifier.types.d.ts +64 -13
- package/dist/assets/svgCollection.d.ts +6 -0
- package/dist/components/ImageStatusPlaceholder.d.ts +10 -0
- package/dist/index.cjs +153 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +50 -13
- package/dist/index.js +153 -82
- package/dist/index.js.map +1 -1
- package/dist/utils/globalUtils.d.ts +26 -22
- package/package.json +20 -3
package/dist/index.js
CHANGED
|
@@ -28,17 +28,20 @@ function styleInject(css, ref) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
var css_248z = ":root{--button-icon-color:#
|
|
32
|
-
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"};
|
|
31
|
+
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}";
|
|
32
|
+
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"};
|
|
33
33
|
styleInject(css_248z);
|
|
34
34
|
|
|
35
|
+
function isTouchEvent(e) {
|
|
36
|
+
return 'touches' in e || ('nativeEvent' in e && 'touches' in e.nativeEvent);
|
|
37
|
+
}
|
|
35
38
|
// Defaulted values to 0 to make the functions SSR safe
|
|
36
|
-
|
|
37
|
-
if ('touches' in e && e.touches.length > 0) {
|
|
39
|
+
function getPageCoords(e, fallbackElement) {
|
|
40
|
+
if ('touches' in e && e.touches && e.touches.length > 0) {
|
|
38
41
|
return { x: e.touches[0].pageX, y: e.touches[0].pageY };
|
|
39
42
|
}
|
|
40
43
|
else if ('pageX' in e && 'pageY' in e) {
|
|
41
|
-
return { x: e.pageX, y: e.pageY };
|
|
44
|
+
return { x: e.pageX, y: e.pageY }; // Could further narrow with type guards
|
|
42
45
|
}
|
|
43
46
|
else if (fallbackElement) {
|
|
44
47
|
const rect = fallbackElement.getBoundingClientRect();
|
|
@@ -48,7 +51,7 @@ const getPageCoords = (e, fallbackElement) => {
|
|
|
48
51
|
};
|
|
49
52
|
}
|
|
50
53
|
return { x: 0, y: 0 };
|
|
51
|
-
}
|
|
54
|
+
}
|
|
52
55
|
const getOffsets = (pageX, pageY, left, top) => {
|
|
53
56
|
return {
|
|
54
57
|
x: pageX - left,
|
|
@@ -58,10 +61,12 @@ const getOffsets = (pageX, pageY, left, top) => {
|
|
|
58
61
|
const getBounds = (container) => {
|
|
59
62
|
return container ? container.getBoundingClientRect() : { width: 0, height: 0, left: 0, top: 0 };
|
|
60
63
|
};
|
|
61
|
-
const getRatios = (bounds,
|
|
64
|
+
const getRatios = (bounds, natural, zoomScale) => {
|
|
65
|
+
const scaledWidth = natural.width * zoomScale;
|
|
66
|
+
const scaledHeight = natural.height * zoomScale;
|
|
62
67
|
return {
|
|
63
|
-
x: (
|
|
64
|
-
y: (
|
|
68
|
+
x: (scaledWidth - bounds.width) / bounds.width,
|
|
69
|
+
y: (scaledHeight - bounds.height) / bounds.height,
|
|
65
70
|
};
|
|
66
71
|
};
|
|
67
72
|
function clampToBounds(left, top, bounds) {
|
|
@@ -76,15 +81,6 @@ function calculateDragPosition(pageX, pageY, offsets) {
|
|
|
76
81
|
top: pageY - offsets.y,
|
|
77
82
|
};
|
|
78
83
|
}
|
|
79
|
-
const getScaledDimensions = (zoomedImgRef, zoomScale) => {
|
|
80
|
-
if (!zoomedImgRef || !zoomedImgRef.naturalWidth || !zoomedImgRef.naturalHeight) {
|
|
81
|
-
return { width: 0, height: 0 };
|
|
82
|
-
}
|
|
83
|
-
return {
|
|
84
|
-
width: zoomedImgRef.naturalWidth * zoomScale,
|
|
85
|
-
height: zoomedImgRef.naturalHeight * zoomScale,
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
84
|
const getDefaults = () => {
|
|
89
85
|
const defaultCoordinates = { x: 0, y: 0 };
|
|
90
86
|
return {
|
|
@@ -100,6 +96,24 @@ const getDefaults = () => {
|
|
|
100
96
|
lastDragCoords: { x: 0, y: 0, time: 0 },
|
|
101
97
|
};
|
|
102
98
|
};
|
|
99
|
+
class ZoomFallbackBoundary extends React.Component {
|
|
100
|
+
constructor(props) {
|
|
101
|
+
super(props);
|
|
102
|
+
this.state = { hasError: false };
|
|
103
|
+
}
|
|
104
|
+
static getDerivedStateFromError() {
|
|
105
|
+
return { hasError: true };
|
|
106
|
+
}
|
|
107
|
+
componentDidCatch(error, info) {
|
|
108
|
+
console.error('ZoomImage runtime error:', error, info);
|
|
109
|
+
}
|
|
110
|
+
render() {
|
|
111
|
+
if (this.state.hasError) {
|
|
112
|
+
return this.props.fallback ?? null;
|
|
113
|
+
}
|
|
114
|
+
return this.props.children;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
103
117
|
|
|
104
118
|
const applyMouseMove = (pageX, pageY, zoomContextRef, setLeft, setTop) => {
|
|
105
119
|
const { left, top } = calculateDragPosition(pageX, pageY, zoomContextRef.current.offsets);
|
|
@@ -141,29 +155,45 @@ function applyDragMove(x, y, zoomContextRef, setLeft, setTop) {
|
|
|
141
155
|
setTop(clamped.top);
|
|
142
156
|
}
|
|
143
157
|
|
|
158
|
+
const CloseIcon = ({ className = 'c-zoom-close-button-icon' }) => (jsx("svg", { xmlns: 'http://www.w3.org/2000/svg', className: className, viewBox: '0 0 24 24', fill: 'none', children: 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' }) }));
|
|
159
|
+
const FallbackImage = ({ className = 'c-fp-fallback-image' }) => (jsxs("svg", { className: className, viewBox: '0 0 2153 1548', fill: 'none', xmlns: 'http://www.w3.org/2000/svg', children: [jsx("rect", { width: '100%', height: '100%', fill: 'white' }), 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' }), 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' }), 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' }), jsx("circle", { cx: '891.5', cy: '689.5', r: '60', fill: '#CBD4DA', stroke: '#8496A2', strokeWidth: '25' })] }));
|
|
160
|
+
|
|
144
161
|
const BaseImage = ({ src, sources, baseImageStyle = {}, isZoomed, fadeDuration, baseImageClassName, alt, loadingPlaceholder, errorPlaceholder, onError, }) => {
|
|
162
|
+
const imgRef = React.useRef(null);
|
|
145
163
|
const [isLoading, setIsLoading] = React.useState(true);
|
|
146
164
|
const [hasError, setHasError] = React.useState(false);
|
|
147
|
-
|
|
165
|
+
React.useEffect(() => {
|
|
166
|
+
setIsLoading(true);
|
|
167
|
+
setHasError(false);
|
|
168
|
+
}, [src]);
|
|
169
|
+
const handleLoad = () => {
|
|
170
|
+
console.log('Image loaded');
|
|
171
|
+
setIsLoading(false);
|
|
172
|
+
};
|
|
148
173
|
const handleError = () => {
|
|
149
174
|
setHasError(true);
|
|
150
175
|
setIsLoading(false);
|
|
176
|
+
console.error('Image has error');
|
|
151
177
|
onError?.();
|
|
152
178
|
};
|
|
179
|
+
React.useEffect(() => {
|
|
180
|
+
const img = imgRef.current;
|
|
181
|
+
if (img && img.complete && img.naturalWidth > 0) {
|
|
182
|
+
setIsLoading(false);
|
|
183
|
+
}
|
|
184
|
+
}, [src]);
|
|
153
185
|
const imageStyle = React.useMemo(() => ({
|
|
154
186
|
'--fade-duration': `${isZoomed ? fadeDuration : 0}ms`,
|
|
155
187
|
...baseImageStyle,
|
|
156
188
|
}), [isZoomed, fadeDuration, baseImageStyle]);
|
|
157
189
|
const imageClass = [styles['c-point-focus__img'], baseImageClassName].filter(Boolean).join(' ');
|
|
158
|
-
const imgElement = (jsx("img", { alt: alt, src: src, style: imageStyle, onLoad: handleLoad, onError: handleError, className: imageClass, "data-hidden": isZoomed, "data-testid": 'pf-base-image' }));
|
|
190
|
+
const imgElement = !hasError ? (jsx("img", { ref: imgRef, alt: alt, src: src, style: imageStyle, onLoad: handleLoad, onError: handleError, className: imageClass, "data-hidden": isZoomed, "data-testid": 'pf-base-image' })) : null;
|
|
159
191
|
return (jsxs(Fragment, { children: [sources && sources.length > 0 ? (jsxs("picture", { style: { width: '100%', height: '100%' }, children: [sources
|
|
160
192
|
.filter(s => s.srcSet)
|
|
161
|
-
.map((source, i) => (jsx("source", { ...source }, i))), imgElement] })) : (imgElement), isLoading &&
|
|
193
|
+
.map((source, i) => (jsx("source", { ...source }, i))), imgElement] })) : (imgElement), isLoading && !hasError && (jsx("div", { className: styles['c-point-focus__placeholder'], style: { zIndex: 0 }, "data-testid": 'pf-base-loading', children: loadingPlaceholder ?? jsx(FallbackImage, {}) })), hasError && (jsx("div", { className: styles['c-point-focus__placeholder'], style: { zIndex: 0 }, "data-testid": 'pf-base-error', children: errorPlaceholder ?? jsx(FallbackImage, {}) }))] }));
|
|
162
194
|
};
|
|
163
195
|
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
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) => {
|
|
196
|
+
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, zoomScale, }, ref) => {
|
|
167
197
|
const [isLoading, setIsLoading] = React.useState(true);
|
|
168
198
|
const [hasError, setHasError] = React.useState(false);
|
|
169
199
|
React.useEffect(() => {
|
|
@@ -183,11 +213,12 @@ const ZoomImage = React.forwardRef(({ src, fadeDuration, top, left, isZoomed, on
|
|
|
183
213
|
};
|
|
184
214
|
const zoomImgClass = [styles['c-point-focus__zoom-img'], zoomImageClassName].filter(Boolean).join(' ');
|
|
185
215
|
return (jsxs(Fragment, { children: [jsx("img", { ref: ref, "data-testid": 'pf-zoom-image', "data-visible": isZoomed, className: zoomImgClass, style: {
|
|
186
|
-
transform: `translate(${left}px, ${top}px)`,
|
|
216
|
+
transform: `translate(${left}px, ${top}px) scale(${zoomScale})`,
|
|
217
|
+
transformOrigin: 'top left',
|
|
187
218
|
transition: `opacity ${fadeDuration}ms linear, visibility ${fadeDuration}ms linear`,
|
|
188
|
-
}, 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 &&
|
|
219
|
+
}, 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 && (jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-zoom-error', children: errorPlaceholder ?? jsx(FallbackImage, {}) })), isLoading && !hasError && isZoomed && (jsx("div", { className: styles['c-point-focus__placeholder'], "data-testid": 'pf-zoom-loading', children: loadingPlaceholder ?? jsx("span", { className: styles['c-point-focus__loader'] }) })), onClose && (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: {
|
|
189
220
|
transition: `opacity ${fadeDuration}ms linear, visibility ${fadeDuration}ms linear`,
|
|
190
|
-
}, onClick: onClose, onKeyDown: onKeyDown, children: closeButtonContent ?? jsx(CloseIcon, {}) }))] }));
|
|
221
|
+
}, onClick: () => onClose?.('click'), onKeyDown: onKeyDown, children: closeButtonContent ?? jsx(CloseIcon, {}) }))] }));
|
|
191
222
|
});
|
|
192
223
|
ZoomImage.displayName = 'ZoomImage';
|
|
193
224
|
|
|
@@ -220,7 +251,7 @@ function startInertia({ initialLeft, initialTop, velocity, setLeft, setTop, boun
|
|
|
220
251
|
requestAnimationFrame(step);
|
|
221
252
|
}
|
|
222
253
|
|
|
223
|
-
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, }) => {
|
|
254
|
+
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, }) => {
|
|
224
255
|
const containerRef = React.useRef(null);
|
|
225
256
|
const closeButtonRef = React.useRef(null);
|
|
226
257
|
const zoomedImgRef = React.useRef(null);
|
|
@@ -233,6 +264,7 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
|
|
|
233
264
|
const [isFading, setIsFading] = React.useState(false);
|
|
234
265
|
const [left, setLeft] = React.useState(0);
|
|
235
266
|
const [top, setTop] = React.useState(0);
|
|
267
|
+
const safeZoomScale = Math.max(1, Math.min(zoomScale, 5));
|
|
236
268
|
const updateZoomState = (val) => {
|
|
237
269
|
if (isControlled) {
|
|
238
270
|
setExternalZoomState?.(val);
|
|
@@ -241,10 +273,10 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
|
|
|
241
273
|
setInternalZoom(val);
|
|
242
274
|
}
|
|
243
275
|
};
|
|
244
|
-
const zoomIn = (pageX, pageY) => {
|
|
276
|
+
const zoomIn = (pageX, pageY, method) => {
|
|
245
277
|
updateZoomState(true);
|
|
246
278
|
initializeFollowZoomPosition(pageX, pageY, containerRef.current, zoomContextRef, setLeft, setTop);
|
|
247
|
-
onZoom?.();
|
|
279
|
+
onZoom?.({ at: { x: pageX, y: pageY }, method: method });
|
|
248
280
|
setTimeout(() => {
|
|
249
281
|
closeButtonRef.current?.focus();
|
|
250
282
|
}, 0);
|
|
@@ -263,26 +295,28 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
|
|
|
263
295
|
setTop(0);
|
|
264
296
|
updateZoomState(false);
|
|
265
297
|
};
|
|
266
|
-
const handleDragStart = (e) => {
|
|
267
|
-
if (!isZoomed || disableDrag)
|
|
298
|
+
const handleDragStart = React.useCallback((e) => {
|
|
299
|
+
if (!isZoomed || disableDrag || (disableMobile && isTouchEvent(e)))
|
|
268
300
|
return;
|
|
269
301
|
const { x, y } = getPageCoords(e);
|
|
270
302
|
setIsDragging(true);
|
|
271
303
|
zoomContextRef.current.dragStartCoords = getOffsets(x, y, left, top);
|
|
272
|
-
onDragStart?.(
|
|
273
|
-
|
|
274
|
-
|
|
304
|
+
onDragStart?.({
|
|
305
|
+
start: { x, y },
|
|
306
|
+
source: isTouchEvent(e) ? 'touch' : 'mouse',
|
|
307
|
+
});
|
|
308
|
+
}, [isZoomed, disableDrag, disableMobile, left, top, onDragStart]);
|
|
309
|
+
const handleDragMove = React.useCallback((e) => {
|
|
275
310
|
if (!isDragging)
|
|
276
311
|
return;
|
|
277
312
|
const { x, y } = getPageCoords(e);
|
|
278
313
|
applyDragMove(x, y, zoomContextRef, setLeft, setTop);
|
|
279
314
|
if ('touches' in e)
|
|
280
315
|
e.preventDefault?.();
|
|
281
|
-
};
|
|
282
|
-
const handleDragEnd = () => {
|
|
316
|
+
}, [isDragging, setLeft, setTop]);
|
|
317
|
+
const handleDragEnd = React.useCallback(() => {
|
|
283
318
|
setIsDragging(false);
|
|
284
319
|
zoomContextRef.current.dragStartCoords = { x: 0, y: 0 };
|
|
285
|
-
onDragEnd?.();
|
|
286
320
|
// Animation on end
|
|
287
321
|
// Calculate velocity
|
|
288
322
|
const last = zoomContextRef.current.lastDragCoords;
|
|
@@ -317,87 +351,113 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
|
|
|
317
351
|
},
|
|
318
352
|
});
|
|
319
353
|
}
|
|
320
|
-
|
|
354
|
+
onDragEnd?.({
|
|
355
|
+
velocity: { vx, vy },
|
|
356
|
+
final: { x: left, y: top },
|
|
357
|
+
});
|
|
358
|
+
}, [disableInertia, left, top, setLeft, setTop, onDragEnd]);
|
|
321
359
|
const applyImageLoad = (el) => {
|
|
322
|
-
const
|
|
360
|
+
const naturalDimensions = {
|
|
361
|
+
width: el.naturalWidth,
|
|
362
|
+
height: el.naturalHeight,
|
|
363
|
+
};
|
|
364
|
+
const bounds = getBounds(containerRef.current);
|
|
323
365
|
zoomedImgRef.current = el;
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
zoomContextRef.current.
|
|
366
|
+
zoomContextRef.current.scaledDimensions = {
|
|
367
|
+
width: naturalDimensions.width * safeZoomScale,
|
|
368
|
+
height: naturalDimensions.height * safeZoomScale,
|
|
369
|
+
};
|
|
370
|
+
zoomContextRef.current.bounds = bounds;
|
|
371
|
+
zoomContextRef.current.ratios = getRatios(bounds, naturalDimensions, safeZoomScale);
|
|
329
372
|
if (zoomContextRef.current.onLoadCallback) {
|
|
330
373
|
zoomContextRef.current.onLoadCallback();
|
|
331
374
|
zoomContextRef.current.onLoadCallback = null;
|
|
332
375
|
}
|
|
333
376
|
};
|
|
334
|
-
const handleLoad = (e) => {
|
|
377
|
+
const handleLoad = React.useCallback((e) => {
|
|
335
378
|
applyImageLoad(e.currentTarget);
|
|
336
379
|
afterZoomImgLoaded?.();
|
|
337
|
-
};
|
|
338
|
-
const handleClose = (
|
|
339
|
-
onClose?.();
|
|
380
|
+
}, [applyImageLoad, afterZoomImgLoaded]);
|
|
381
|
+
const handleClose = React.useCallback((method) => {
|
|
382
|
+
onClose?.({ triggeredBy: method });
|
|
340
383
|
zoomOut();
|
|
341
|
-
};
|
|
342
|
-
const handleClick = (e) => {
|
|
343
|
-
|
|
384
|
+
}, [onClose, zoomOut]);
|
|
385
|
+
const handleClick = React.useCallback((e, method) => {
|
|
386
|
+
if (disableMobile && isTouchEvent(e))
|
|
387
|
+
return;
|
|
388
|
+
const { x, y } = getPageCoords(e);
|
|
389
|
+
onClickImage?.({ x, y, source: isTouchEvent(e) ? 'touch' : 'mouse' });
|
|
344
390
|
if (zoomContextRef.current.wasDragging) {
|
|
345
391
|
zoomContextRef.current.wasDragging = false;
|
|
346
392
|
return;
|
|
347
393
|
}
|
|
348
394
|
if (isZoomed) {
|
|
349
395
|
if (clickToZoomOut) {
|
|
350
|
-
|
|
396
|
+
handleClose(method);
|
|
351
397
|
}
|
|
352
398
|
return;
|
|
353
399
|
} // is important to keep it to avoid issues when dragging the image with the animation
|
|
354
400
|
if (zoomedImgRef.current) {
|
|
355
401
|
applyImageLoad(zoomedImgRef.current);
|
|
356
|
-
|
|
357
|
-
zoomIn(x, y);
|
|
402
|
+
zoomIn(x, y, method);
|
|
358
403
|
}
|
|
359
404
|
else {
|
|
360
|
-
|
|
361
|
-
zoomContextRef.current.onLoadCallback = () => zoomIn(x, y);
|
|
405
|
+
zoomContextRef.current.onLoadCallback = () => zoomIn(x, y, method);
|
|
362
406
|
}
|
|
363
|
-
};
|
|
364
|
-
const handleKeyDown = (e) => {
|
|
407
|
+
}, [disableMobile, isTouchEvent, getPageCoords, onClickImage, isZoomed, clickToZoomOut, handleClose, applyImageLoad, zoomIn]);
|
|
408
|
+
const handleKeyDown = React.useCallback((e) => {
|
|
365
409
|
if ((e.key === 'Enter' || e.key === ' ') && !isZoomed) {
|
|
366
410
|
e.preventDefault();
|
|
367
411
|
const { x, y } = getPageCoords(e, containerRef.current);
|
|
368
|
-
zoomIn(x, y);
|
|
412
|
+
zoomIn(x, y, 'keyboard');
|
|
369
413
|
}
|
|
370
414
|
if (e.key === 'Escape' && isZoomed) {
|
|
371
415
|
e.preventDefault();
|
|
372
|
-
|
|
416
|
+
handleClose('keyboard');
|
|
373
417
|
}
|
|
374
|
-
};
|
|
375
|
-
const handleZoomLayerKeyDown = (e) => {
|
|
418
|
+
}, [isZoomed, getPageCoords, zoomIn, handleClose]);
|
|
419
|
+
const handleZoomLayerKeyDown = React.useCallback((e) => {
|
|
376
420
|
if (e.key === 'Escape' && isZoomed) {
|
|
377
421
|
e.preventDefault();
|
|
378
|
-
|
|
422
|
+
handleClose('keyboard');
|
|
379
423
|
}
|
|
380
|
-
};
|
|
381
|
-
const
|
|
424
|
+
}, [isZoomed, handleClose]);
|
|
425
|
+
const throttledZoomedMouseMove = React.useCallback((x, y, bounds) => {
|
|
426
|
+
zoomContextRef.current.lastMoveEvent = { x, y, bounds };
|
|
427
|
+
if (zoomContextRef.current.rafId == null) {
|
|
428
|
+
zoomContextRef.current.rafId = window.requestAnimationFrame(() => {
|
|
429
|
+
const evt = zoomContextRef.current.lastMoveEvent;
|
|
430
|
+
if (evt && onZoomedMouseMove) {
|
|
431
|
+
onZoomedMouseMove(evt);
|
|
432
|
+
}
|
|
433
|
+
zoomContextRef.current.rafId = null;
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}, [onZoomedMouseMove]);
|
|
437
|
+
const handleMouseEnter = React.useCallback((e) => {
|
|
382
438
|
setIsActive(true);
|
|
383
439
|
setIsFading(false);
|
|
384
|
-
onMouseEnter?.();
|
|
385
|
-
if (zoomType === 'hover' && !isZoomed) {
|
|
386
|
-
handleClick(e);
|
|
440
|
+
onMouseEnter?.({ source: isTouchEvent(e) ? 'touch' : 'mouse' });
|
|
441
|
+
if (zoomType === 'hover' && !isZoomed && !(disableMobile && isTouchEvent(e))) {
|
|
442
|
+
handleClick(e, 'hover');
|
|
387
443
|
}
|
|
388
|
-
};
|
|
389
|
-
const handleMouseMove = (e) => {
|
|
390
|
-
onZoomedMouseMove?.();
|
|
444
|
+
}, [setIsActive, setIsFading, onMouseEnter, zoomType, isZoomed, disableMobile, handleClick]);
|
|
445
|
+
const handleMouseMove = React.useCallback((e) => {
|
|
391
446
|
if (moveType === 'follow' && isZoomed) {
|
|
392
|
-
|
|
447
|
+
const { x, y } = getPageCoords(e);
|
|
448
|
+
const bounds = zoomContextRef.current.bounds;
|
|
449
|
+
if (!bounds || typeof bounds.left !== 'number')
|
|
450
|
+
return;
|
|
451
|
+
throttledZoomedMouseMove(x, y, zoomContextRef.current.bounds);
|
|
452
|
+
applyMouseMove(x, y, zoomContextRef, setLeft, setTop);
|
|
393
453
|
}
|
|
394
|
-
};
|
|
395
|
-
const handleMouseLeave = (e) => {
|
|
396
|
-
onMouseLeave?.();
|
|
397
|
-
handleClose();
|
|
398
|
-
};
|
|
399
|
-
const handleFadeOut = (e, noTransition) => {
|
|
400
|
-
if (noTransition || (e.propertyName === 'opacity' && containerRef?.current?.contains(e.target))) {
|
|
454
|
+
}, [moveType, isZoomed, throttledZoomedMouseMove, setLeft, setTop]);
|
|
455
|
+
const handleMouseLeave = React.useCallback((e) => {
|
|
456
|
+
onMouseLeave?.({ source: isTouchEvent(e) ? 'touch' : 'mouse' });
|
|
457
|
+
handleClose('hover');
|
|
458
|
+
}, [onMouseLeave, handleClose]);
|
|
459
|
+
const handleFadeOut = React.useCallback((e, noTransition) => {
|
|
460
|
+
if (noTransition || (e && 'propertyName' in e && e.propertyName === 'opacity' && containerRef?.current?.contains(e.target))) {
|
|
401
461
|
if (!zoomPreload) {
|
|
402
462
|
zoomedImgRef.current = null;
|
|
403
463
|
zoomContextRef.current = getDefaults();
|
|
@@ -406,10 +466,16 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
|
|
|
406
466
|
setIsFading(false);
|
|
407
467
|
afterZoomOut?.();
|
|
408
468
|
}
|
|
409
|
-
};
|
|
469
|
+
}, [zoomPreload, setIsActive, setIsFading, afterZoomOut]);
|
|
410
470
|
// possibly extra to be removed
|
|
411
471
|
React.useEffect(() => {
|
|
412
472
|
zoomContextRef.current = getDefaults();
|
|
473
|
+
return () => {
|
|
474
|
+
if (zoomContextRef.current.rafId != null) {
|
|
475
|
+
cancelAnimationFrame(zoomContextRef.current.rafId);
|
|
476
|
+
zoomContextRef.current.rafId = null;
|
|
477
|
+
}
|
|
478
|
+
};
|
|
413
479
|
}, []);
|
|
414
480
|
React.useEffect(() => {
|
|
415
481
|
if (isDragging && !disableDrag) {
|
|
@@ -449,9 +515,14 @@ const ImageMagnifier = ({ src, sources, width, height, zoomSrc, zoomScale = 1, z
|
|
|
449
515
|
closeButtonContent: closeButtonContent,
|
|
450
516
|
loadingPlaceholder: loadingPlaceholder,
|
|
451
517
|
errorPlaceholder: errorPlaceholder,
|
|
518
|
+
zoomScale: safeZoomScale,
|
|
452
519
|
};
|
|
453
520
|
const containerClass = [styles['c-point-focus'], containerClassName].filter(Boolean).join(' ');
|
|
454
|
-
return (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:
|
|
521
|
+
return (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 => {
|
|
522
|
+
if (disableMobile) {
|
|
523
|
+
e.preventDefault();
|
|
524
|
+
}
|
|
525
|
+
}, onKeyDown: handleKeyDown, onMouseEnter: handleMouseEnter, onMouseMove: isZoomed ? handleMouseMove : undefined, onMouseLeave: handleMouseLeave, children: [jsx(BaseImage, { src: src, alt: alt, sources: sources, width: width, height: height, baseImageStyle: baseImageStyle, baseImageClassName: baseImageClassName, fadeDuration: fadeDuration, isZoomed: isZoomed, onError: onBaseImageError }), isActive && (jsxs(Fragment, { children: [overlay && (jsx("div", { className: styles['c-point-focus__overlay'], "data-testid": 'pf-overlay', children: overlay })), jsx(ZoomFallbackBoundary, { fallback: errorPlaceholder ?? jsx(FallbackImage, {}), children: jsx(ZoomImage, { ref: zoomedImgRef, ...zoomImageProps }, isZoomed && clickToZoomOut ? 'zoomed' : 'unzoomed') })] }))] }));
|
|
455
526
|
};
|
|
456
527
|
|
|
457
528
|
export { ImageMagnifier as default };
|