Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 6,869 Bytes
a8fb296 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | import React, {
useCallback,
useEffect,
useId,
useRef,
useState,
} from 'react';
import {createPortal} from 'react-dom';
import {
TransformWrapper,
TransformComponent,
type ReactZoomPanPinchRef,
} from 'react-zoom-pan-pinch';
import styles from './styles.module.css';
export interface ZoomableImageProps {
src: string;
alt: string;
title?: string;
/** Height of the inline preview frame (CSS). Default 360px. */
previewHeight?: string;
}
/**
* Inline preview of a large diagram/PNG with an **Open & zoom** control that
* opens a fullscreen lightbox (pan + wheel zoom via react-zoom-pan-pinch).
* Preview fills the card width and crops from the top so tall diagrams stay readable.
*/
export default function ZoomableImage({
src,
alt,
title,
previewHeight = '360px',
}: ZoomableImageProps): JSX.Element {
const [open, setOpen] = useState(false);
const [portalReady, setPortalReady] = useState(false);
// Start low so Fit can shrink below 1×; locked to fit scale after first fit.
const [minScale, setMinScale] = useState(0.05);
const titleId = useId();
const label = title || alt;
const imgRef = useRef<HTMLImageElement | null>(null);
const apiRef = useRef<ReactZoomPanPinchRef | null>(null);
const close = useCallback(() => setOpen(false), []);
useEffect(() => {
setPortalReady(true);
}, []);
useEffect(() => {
if (!open) return;
setMinScale(0.05);
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') close();
};
const prevOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
window.addEventListener('keydown', onKey);
return () => {
document.body.style.overflow = prevOverflow;
window.removeEventListener('keydown', onKey);
};
}, [open, close]);
const lockMinToFitScale = useCallback((scale: number) => {
if (!Number.isFinite(scale) || scale <= 0) return;
// Floor at fit — no zoom-out past the full-diagram view.
setMinScale(Number(scale.toFixed(4)));
}, []);
const fitToView = useCallback(
(animate = true) => {
const api = apiRef.current;
const img = imgRef.current;
if (!api || !img) return;
// zoomToElement scales the element into the TransformComponent viewport.
try {
api.zoomToElement(
img,
undefined,
animate ? 200 : 0,
'easeOut',
);
const applyFloor = () => {
const scale = api.state.scale;
lockMinToFitScale(scale);
};
if (animate) {
window.setTimeout(applyFloor, 220);
} else {
applyFloor();
}
} catch {
/* image or stage not ready */
}
},
[lockMinToFitScale],
);
const lightbox =
open && portalReady
? createPortal(
<div
className={styles.overlay}
role="dialog"
aria-modal="true"
aria-labelledby={titleId}
>
<TransformWrapper
initialScale={1}
minScale={minScale}
maxScale={8}
centerOnInit={false}
// Library default step is 0.015 with smooth=true → step*deltaY per
// tick (often ~1.5). We want finer grain for diagrams.
smooth
wheel={{step: 0.006}}
doubleClick={{mode: 'toggle', step: 0.5}}
panning={{velocityDisabled: true}}
// Default zoomAnimation.size (0.4) lets wheel go minScale-0.4 —
// that is what produced the tiny corner thumbnail.
zoomAnimation={{size: 0, disabled: true}}
disablePadding
limitToBounds={false}
onInit={(ref) => {
apiRef.current = ref;
requestAnimationFrame(() => fitToView(false));
}}
>
{({zoomIn, zoomOut}) => (
<>
<div className={styles.toolbar}>
<p className={styles.toolbarTitle} id={titleId}>
{label}
</p>
<div className={styles.toolbarActions}>
<button
type="button"
className={styles.toolBtn}
onClick={() => zoomIn(0.12)}
>
Zoom in
</button>
<button
type="button"
className={styles.toolBtn}
onClick={() => zoomOut(0.12)}
>
Zoom out
</button>
<button
type="button"
className={styles.toolBtn}
onClick={() => fitToView(true)}
>
Fit
</button>
<button
type="button"
className={styles.toolBtnPrimary}
onClick={close}
>
Close
</button>
</div>
</div>
<div className={styles.stage}>
<TransformComponent
wrapperClass={styles.stageTransform}
contentClass={styles.stageContent}
>
<img
ref={imgRef}
className={styles.fullImg}
src={src}
alt={alt}
draggable={false}
onLoad={() => fitToView(false)}
/>
</TransformComponent>
</div>
<p className={styles.lightboxHint}>
Scroll to zoom · drag to pan · Esc to close
</p>
</>
)}
</TransformWrapper>
</div>,
document.body,
)
: null;
return (
<div className={styles.wrap}>
<div className={styles.header}>
{title ? <p className={styles.title}>{title}</p> : <span />}
<button
type="button"
className={styles.openBtn}
onClick={() => setOpen(true)}
aria-haspopup="dialog"
>
Open & zoom
</button>
</div>
<div className={styles.previewFrame} style={{height: previewHeight}}>
<img
className={styles.preview}
src={src}
alt={alt}
loading="lazy"
decoding="async"
/>
<span className={styles.previewFade} aria-hidden="true" />
</div>
{lightbox}
</div>
);
}
|