1- import { MinusIcon , PlusIcon , RotateCcwIcon } from "lucide-react" ;
21import { useCallback , useEffect , useLayoutEffect , useRef , useState } from "react" ;
3- import { Button } from "../ui/button" ;
42
53const MAX_ZOOM = 8 ;
64
@@ -26,8 +24,9 @@ export function ZoomableImage({
2624 null ,
2725 ) ;
2826 const dragRef = useRef < { x : number ; y : number ; left : number ; top : number } | null > ( null ) ;
27+ const suppressClickRef = useRef ( false ) ;
2928 const [ dragging , setDragging ] = useState ( false ) ;
30- const maxHeight = Math . max ( 1 , Math . min ( windowSize . height * 0.8 , windowSize . height - 120 ) ) ;
29+ const maxHeight = Math . max ( 1 , Math . min ( windowSize . height * 0.86 , windowSize . height - 80 ) ) ;
3130 const fit = Math . min (
3231 1 ,
3332 ( windowSize . width * 0.92 ) / ( naturalSize . width || 1 ) ,
@@ -76,12 +75,12 @@ export function ZoomableImage({
7675 const viewport = viewportRef . current ;
7776 if ( ! viewport ) return ;
7877 const wheel = ( event : WheelEvent ) => {
79- if ( ! event . ctrlKey && ! event . metaKey ) return ;
78+ if ( event . deltaY === 0 ) return ;
8079 event . preventDefault ( ) ;
8180 const delta =
8281 event . deltaY *
8382 ( event . deltaMode === 1 ? 16 : event . deltaMode === 2 ? viewport . clientHeight : 1 ) ;
84- changeZoom ( zoomRef . current * Math . exp ( - delta * 0.01 ) , {
83+ changeZoom ( zoomRef . current * Math . exp ( - delta * ( event . ctrlKey ? 0.01 : 0.002 ) ) , {
8584 x : event . clientX ,
8685 y : event . clientY ,
8786 } ) ;
@@ -91,25 +90,31 @@ export function ZoomableImage({
9190 } , [ changeZoom ] ) ;
9291
9392 return (
94- < div className = "flex min-w-0 flex-col items-center gap-2 " >
93+ < div className = "min-w-0" >
9594 < div
9695 ref = { viewportRef }
9796 role = "region"
9897 aria-label = { `${ name } , zoomable image` }
98+ aria-description = "Click to zoom in or return to fit. Scroll to zoom, drag to pan. Use Enter to toggle zoom, plus or minus to zoom, and 0 to fit."
9999 tabIndex = { 0 }
100100 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"
101101 style = { {
102102 width : width || undefined ,
103103 height : height || undefined ,
104104 maxHeight,
105- cursor : zoom > 1 ? ( dragging ? "grabbing" : "grab" ) : "default " ,
105+ cursor : zoom > 1 ? ( dragging ? "grabbing" : "grab" ) : "zoom-in " ,
106106 } }
107- onDoubleClick = { ( event ) => {
107+ onClick = { ( event ) => {
108+ // Pointer capture also produces a click after dragging; leave the image zoomed.
109+ if ( suppressClickRef . current || event . detail > 1 ) return ;
108110 changeZoom ( zoomRef . current > 1 ? 1 : 2 , { x : event . clientX , y : event . clientY } ) ;
109111 } }
110112 onKeyDown = { ( event ) => {
111113 if ( event . ctrlKey || event . metaKey || event . altKey ) return ;
112- if ( event . key === "+" || event . key === "=" ) {
114+ if ( event . key === "Enter" || event . key === " " ) {
115+ event . preventDefault ( ) ;
116+ if ( ! event . repeat ) changeZoom ( zoomRef . current > 1 ? 1 : 2 ) ;
117+ } else if ( event . key === "+" || event . key === "=" ) {
113118 event . preventDefault ( ) ;
114119 changeZoom ( zoomRef . current * 1.5 ) ;
115120 } else if ( event . key === "-" ) {
@@ -124,6 +129,7 @@ export function ZoomableImage({
124129 }
125130 } }
126131 onPointerDown = { ( event ) => {
132+ suppressClickRef . current = false ;
127133 if ( event . pointerType !== "mouse" || event . button !== 0 || zoomRef . current <= 1 ) return ;
128134 const viewport = event . currentTarget ;
129135 const bounds = viewport . getBoundingClientRect ( ) ;
@@ -144,6 +150,9 @@ export function ZoomableImage({
144150 onPointerMove = { ( event ) => {
145151 const drag = dragRef . current ;
146152 if ( ! drag ) return ;
153+ if ( Math . hypot ( event . clientX - drag . x , event . clientY - drag . y ) > 4 ) {
154+ suppressClickRef . current = true ;
155+ }
147156 event . currentTarget . scrollLeft = drag . left - ( event . clientX - drag . x ) ;
148157 event . currentTarget . scrollTop = drag . top - ( event . clientY - drag . y ) ;
149158 } }
@@ -174,42 +183,9 @@ export function ZoomableImage({
174183 onError = { onError }
175184 />
176185 </ div >
177- < div
178- role = "group"
179- aria-label = "Image zoom"
180- className = "flex items-center gap-1 rounded-full bg-black/70 p-1 text-white"
181- >
182- < Button
183- size = "icon-xs"
184- variant = "overlay"
185- aria-label = "Zoom out"
186- disabled = { zoom <= 1 || ! naturalSize . width }
187- onClick = { ( ) => changeZoom ( zoomRef . current / 1.5 ) }
188- >
189- < MinusIcon />
190- </ Button >
191- < span className = "min-w-12 text-center text-xs tabular-nums" aria-live = "polite" >
192- { Math . round ( zoom * 100 ) } %
193- </ span >
194- < Button
195- size = "icon-xs"
196- variant = "overlay"
197- aria-label = "Zoom in"
198- disabled = { zoom >= MAX_ZOOM || ! naturalSize . width }
199- onClick = { ( ) => changeZoom ( zoomRef . current * 1.5 ) }
200- >
201- < PlusIcon />
202- </ Button >
203- < Button
204- size = "icon-xs"
205- variant = "overlay"
206- aria-label = "Reset zoom to fit"
207- disabled = { zoom <= 1 }
208- onClick = { ( ) => changeZoom ( 1 ) }
209- >
210- < RotateCcwIcon />
211- </ Button >
212- </ div >
186+ < span className = "sr-only" aria-live = "polite" >
187+ { Math . round ( zoom * 100 ) } % zoom
188+ </ span >
213189 </ div >
214190 ) ;
215191}
0 commit comments