|
| 1 | +import { MinusIcon, PlusIcon, RotateCcwIcon } from "lucide-react"; |
| 2 | +import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react"; |
| 3 | +import { Button } from "../ui/button"; |
| 4 | + |
| 5 | +const MAX_ZOOM = 8; |
| 6 | + |
| 7 | +/** Keeps zoomed pixels in a native scroll area so wheels and touch can explore the whole image. */ |
| 8 | +export function ZoomableImage({ |
| 9 | + src, |
| 10 | + name, |
| 11 | + onError, |
| 12 | +}: { |
| 13 | + src: string; |
| 14 | + name: string; |
| 15 | + onError: () => void; |
| 16 | +}) { |
| 17 | + const viewportRef = useRef<HTMLDivElement>(null); |
| 18 | + const [naturalSize, setNaturalSize] = useState({ width: 0, height: 0 }); |
| 19 | + const [windowSize, setWindowSize] = useState(() => ({ |
| 20 | + width: window.innerWidth, |
| 21 | + height: window.innerHeight, |
| 22 | + })); |
| 23 | + const [zoom, setZoom] = useState(1); |
| 24 | + const zoomRef = useRef(1); |
| 25 | + const anchorRef = useRef<{ x: number; y: number; viewportX: number; viewportY: number } | null>( |
| 26 | + null, |
| 27 | + ); |
| 28 | + const dragRef = useRef<{ x: number; y: number; left: number; top: number } | null>(null); |
| 29 | + const [dragging, setDragging] = useState(false); |
| 30 | + const maxHeight = Math.max(1, Math.min(windowSize.height * 0.8, windowSize.height - 120)); |
| 31 | + const fit = Math.min( |
| 32 | + 1, |
| 33 | + (windowSize.width * 0.92) / (naturalSize.width || 1), |
| 34 | + maxHeight / (naturalSize.height || 1), |
| 35 | + ); |
| 36 | + const width = naturalSize.width * fit * zoom; |
| 37 | + const height = naturalSize.height * fit * zoom; |
| 38 | + |
| 39 | + const changeZoom = useCallback((next: number, point?: { x: number; y: number }) => { |
| 40 | + const viewport = viewportRef.current; |
| 41 | + const previous = zoomRef.current; |
| 42 | + const clamped = Math.min(MAX_ZOOM, Math.max(1, next)); |
| 43 | + if (!viewport || previous === clamped) return; |
| 44 | + const bounds = viewport.getBoundingClientRect(); |
| 45 | + const x = point ? point.x - bounds.left : viewport.clientWidth / 2; |
| 46 | + const y = point ? point.y - bounds.top : viewport.clientHeight / 2; |
| 47 | + anchorRef.current = { |
| 48 | + x: (viewport.scrollLeft + x) / previous, |
| 49 | + y: (viewport.scrollTop + y) / previous, |
| 50 | + viewportX: x / viewport.clientWidth, |
| 51 | + viewportY: y / viewport.clientHeight, |
| 52 | + }; |
| 53 | + zoomRef.current = clamped; |
| 54 | + setZoom(clamped); |
| 55 | + }, []); |
| 56 | + |
| 57 | + useLayoutEffect(() => { |
| 58 | + const viewport = viewportRef.current; |
| 59 | + const anchor = anchorRef.current; |
| 60 | + if (!viewport || !anchor) return; |
| 61 | + viewport.scrollLeft = anchor.x * zoom - anchor.viewportX * viewport.clientWidth; |
| 62 | + viewport.scrollTop = anchor.y * zoom - anchor.viewportY * viewport.clientHeight; |
| 63 | + anchorRef.current = null; |
| 64 | + }, [zoom]); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + const resize = () => { |
| 68 | + setWindowSize({ width: window.innerWidth, height: window.innerHeight }); |
| 69 | + changeZoom(1); |
| 70 | + }; |
| 71 | + window.addEventListener("resize", resize); |
| 72 | + return () => window.removeEventListener("resize", resize); |
| 73 | + }, [changeZoom]); |
| 74 | + |
| 75 | + useEffect(() => { |
| 76 | + const viewport = viewportRef.current; |
| 77 | + if (!viewport) return; |
| 78 | + const wheel = (event: WheelEvent) => { |
| 79 | + if (!event.ctrlKey && !event.metaKey) return; |
| 80 | + event.preventDefault(); |
| 81 | + const delta = |
| 82 | + event.deltaY * |
| 83 | + (event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? viewport.clientHeight : 1); |
| 84 | + changeZoom(zoomRef.current * Math.exp(-delta * 0.01), { |
| 85 | + x: event.clientX, |
| 86 | + y: event.clientY, |
| 87 | + }); |
| 88 | + }; |
| 89 | + viewport.addEventListener("wheel", wheel, { passive: false }); |
| 90 | + return () => viewport.removeEventListener("wheel", wheel); |
| 91 | + }, [changeZoom]); |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="flex min-w-0 flex-col items-center gap-2"> |
| 95 | + <div |
| 96 | + ref={viewportRef} |
| 97 | + role="region" |
| 98 | + aria-label={`${name}, zoomable image`} |
| 99 | + tabIndex={0} |
| 100 | + className="max-w-[92vw] overflow-auto overscroll-contain rounded-lg bg-background shadow-2xl ring-1 ring-border/70 outline-none focus-visible:ring-2 focus-visible:ring-ring" |
| 101 | + style={{ |
| 102 | + width: width || undefined, |
| 103 | + height: height || undefined, |
| 104 | + maxHeight, |
| 105 | + cursor: zoom > 1 ? (dragging ? "grabbing" : "grab") : "default", |
| 106 | + }} |
| 107 | + onDoubleClick={(event) => { |
| 108 | + changeZoom(zoomRef.current > 1 ? 1 : 2, { x: event.clientX, y: event.clientY }); |
| 109 | + }} |
| 110 | + onKeyDown={(event) => { |
| 111 | + if (event.key === "+" || event.key === "=") { |
| 112 | + event.preventDefault(); |
| 113 | + changeZoom(zoomRef.current * 1.5); |
| 114 | + } else if (event.key === "-") { |
| 115 | + event.preventDefault(); |
| 116 | + changeZoom(zoomRef.current / 1.5); |
| 117 | + } else if (event.key === "0") { |
| 118 | + event.preventDefault(); |
| 119 | + changeZoom(1); |
| 120 | + } else if (zoomRef.current > 1 && event.key.startsWith("Arrow")) { |
| 121 | + // Leave native keyboard scrolling enabled without navigating the gallery. |
| 122 | + event.stopPropagation(); |
| 123 | + } |
| 124 | + }} |
| 125 | + onPointerDown={(event) => { |
| 126 | + if (event.pointerType !== "mouse" || event.button !== 0 || zoomRef.current <= 1) return; |
| 127 | + const viewport = event.currentTarget; |
| 128 | + const bounds = viewport.getBoundingClientRect(); |
| 129 | + if ( |
| 130 | + event.clientX - bounds.left >= viewport.clientWidth || |
| 131 | + event.clientY - bounds.top >= viewport.clientHeight |
| 132 | + ) |
| 133 | + return; |
| 134 | + dragRef.current = { |
| 135 | + x: event.clientX, |
| 136 | + y: event.clientY, |
| 137 | + left: viewport.scrollLeft, |
| 138 | + top: viewport.scrollTop, |
| 139 | + }; |
| 140 | + viewport.setPointerCapture(event.pointerId); |
| 141 | + setDragging(true); |
| 142 | + }} |
| 143 | + onPointerMove={(event) => { |
| 144 | + const drag = dragRef.current; |
| 145 | + if (!drag) return; |
| 146 | + event.currentTarget.scrollLeft = drag.left - (event.clientX - drag.x); |
| 147 | + event.currentTarget.scrollTop = drag.top - (event.clientY - drag.y); |
| 148 | + }} |
| 149 | + onPointerUp={(event) => { |
| 150 | + if (event.currentTarget.hasPointerCapture(event.pointerId)) { |
| 151 | + event.currentTarget.releasePointerCapture(event.pointerId); |
| 152 | + } |
| 153 | + dragRef.current = null; |
| 154 | + setDragging(false); |
| 155 | + }} |
| 156 | + onLostPointerCapture={() => { |
| 157 | + dragRef.current = null; |
| 158 | + setDragging(false); |
| 159 | + }} |
| 160 | + > |
| 161 | + <img |
| 162 | + src={src} |
| 163 | + alt={name} |
| 164 | + draggable={false} |
| 165 | + className="block max-w-none select-none" |
| 166 | + style={naturalSize.width ? { width, height } : { maxWidth: "92vw", maxHeight }} |
| 167 | + onLoad={(event) => { |
| 168 | + setNaturalSize({ |
| 169 | + width: event.currentTarget.naturalWidth, |
| 170 | + height: event.currentTarget.naturalHeight, |
| 171 | + }); |
| 172 | + }} |
| 173 | + onError={onError} |
| 174 | + /> |
| 175 | + </div> |
| 176 | + <div |
| 177 | + role="group" |
| 178 | + aria-label="Image zoom" |
| 179 | + className="flex items-center gap-1 rounded-full bg-black/70 p-1 text-white" |
| 180 | + > |
| 181 | + <Button |
| 182 | + size="icon-xs" |
| 183 | + variant="overlay" |
| 184 | + aria-label="Zoom out" |
| 185 | + disabled={zoom <= 1 || !naturalSize.width} |
| 186 | + onClick={() => changeZoom(zoomRef.current / 1.5)} |
| 187 | + > |
| 188 | + <MinusIcon /> |
| 189 | + </Button> |
| 190 | + <span className="min-w-12 text-center text-xs tabular-nums" aria-live="polite"> |
| 191 | + {Math.round(zoom * 100)}% |
| 192 | + </span> |
| 193 | + <Button |
| 194 | + size="icon-xs" |
| 195 | + variant="overlay" |
| 196 | + aria-label="Zoom in" |
| 197 | + disabled={zoom >= MAX_ZOOM || !naturalSize.width} |
| 198 | + onClick={() => changeZoom(zoomRef.current * 1.5)} |
| 199 | + > |
| 200 | + <PlusIcon /> |
| 201 | + </Button> |
| 202 | + <Button |
| 203 | + size="icon-xs" |
| 204 | + variant="overlay" |
| 205 | + aria-label="Reset zoom to fit" |
| 206 | + disabled={zoom <= 1} |
| 207 | + onClick={() => changeZoom(1)} |
| 208 | + > |
| 209 | + <RotateCcwIcon /> |
| 210 | + </Button> |
| 211 | + </div> |
| 212 | + </div> |
| 213 | + ); |
| 214 | +} |
0 commit comments