Image viewer with zoom from 50% to 400%, panning, 90° rotation, reset and fit. Zoom follows the cursor: the point under the pointer stays in place. Works with the mouse wheel, trackpad and two-finger pinch, and has an inline mode and a fullscreen dialog. Zoom and panning need JavaScript: the component ships about 4 kB of its own code and no libraries. The fullscreen mode is a native <dialog>, so the browser handles focus, Esc and the inert page behind it.
Browser Support
Displays the modal dialog
Animates the initial appearance
Locks page scrolling while open
Styles the viewer backdrop
Preview
Scroll or pinch to zoom, drag to pan, double-click to zoom in or back. Focus the image and try +, -, 0, R and the arrow keys.

Installation
Usage
import { ImageViewer } from "@/components/ui/image-viewer"
export default function Example() {
return (
<ImageViewer
src="/images/floor-plan.png"
alt="Floor plan of the second floor"
className="h-[480px]"
/>
)
}Examples
Fullscreen dialog
ImageViewerDialog opens the viewer in a native modal <dialog>. Pass a single element as children: its click opens the dialog. Escape closes it, focus moves to the image on open and returns to the trigger on close.
import { Button } from "@/components/ui/button"
import { ImageViewerDialog } from "@/components/ui/image-viewer"
<ImageViewerDialog src="/images/poster.png" alt="Concert poster">
<Button type="button">
<img src="/images/poster-small.png" alt="Concert poster" />
</Button>
</ImageViewerDialog>Zoom limits
minZoom and maxZoom are relative to the fitted size. Here the image cannot be smaller than the frame and the buttons step by 50%.

<ImageViewer
src="/images/map.png"
alt="City map"
minZoom={1}
maxZoom={3}
zoomStep={0.5}
className="h-72"
/>Without toolbar
showControls={false} hides the toolbar. Gestures and keyboard shortcuts keep working.

<ImageViewer src="/images/poster.png" alt="Poster" showControls={false} />Translated labels
Every button label and the screen reader hint come from the labels prop.
<ImageViewer
src="/images/plan.png"
alt="Plan"
labels={{
viewer: "Visor de imágenes",
zoomIn: "Acercar",
zoomOut: "Alejar",
rotate: "Girar",
fit: "Ajustar",
reset: "Restablecer",
}}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| src | string | - | Image URL |
| alt | string | - | Alternative text, also part of the viewer label |
| minZoom | number | 0.5 | Smallest zoom relative to the fitted size |
| maxZoom | number | 4 | Largest zoom relative to the fitted size |
| zoomStep | number | 0.25 | Zoom change for the buttons and + / - keys |
| showControls | boolean | true | Show the toolbar |
| toolbarEnd | ReactNode | - | Extra content at the end of the toolbar |
| imageClassName | string | - | Classes for the <img> element |
| labels | Partial<ImageViewerLabels> | English | Button labels, hint and error text |
| className | string | - | Classes for the root. Default height is h-96 |
ImageViewerDialog
Accepts all ImageViewer props except className and toolbarEnd, plus:
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | - | Controlled open state |
| defaultOpen | boolean | false | Initial open state |
| onOpenChange | (open: boolean) => void | - | Called when the dialog opens or closes |
| children | ReactNode | - | Trigger element. A single element gets an onClick that opens the dialog |
| title | string | alt | Dialog title, alt by default |
| className | string | - | Classes for the dialog content |
Features
- Zoom to the cursor: wheel, pinch and double-click keep the point under the pointer still
- Pan limits: the image cannot be dragged out of the frame
- Rotation: 90° steps, the image is refitted for the new orientation
- Keyboard: + and - zoom, 0 fits, R rotates (Shift+R back), arrows pan, Shift speeds them up
- Accessibility: the viewport is a labelled region with a hidden hint, every button has a label
- Reduced motion: button and key transitions are turned off