ImageViewer drag-to-pan when zoomed in (WEB-1062)
Version: 0.5.0 · Type: ✨ Feature
Problem
After zooming in, there was no way to navigate to a specific area of the image — the image stayed centered with no pan support.
Changed Files
packages/design/src/components/ImageViewer/ImageViewer.tsxpackages/design/src/components/ImageViewer/__tests__/ImageViewer.test.tsx
Changes
- Pan state —
panRef(ref, not state) stores the current{ x, y }offset. Direct DOM writes during drag bypass React re-render, eliminating drag latency.isDraggingstate drives cursor anduserSelectstyle. - Trigger condition — drag only activates when
zoom > 1(isPannable). Cursor switches tograb/grabbing. - Event binding —
mousedownon<img>starts the drag;mousemove/mouseup/mouseleaveon the content layer prevent drag cancellation when the pointer moves off the image. - Boundary clamping — max pan is derived from
offsetWidth * zoomvswindow.innerWidthso the image never fully leaves the viewport. - Auto-reset — pan resets to
{ 0, 0 }on image switch, viewer close, or zoom-out to 1×. - Transform — non-zero pan uses
translate(x, y) scale(zoom); zero pan keepsscale(zoom)to preserve existing test assertions. - 5 new unit tests — grab cursor, pan update, zoom=1 guard, navigation reset, boundary clamping.